0

虽然我一直在试图弄清楚如何巧妙地显示很长的导航链接,但我遇到了这个jQuery IOS Drill Down Menu插件(来源文章:jQuery Wiki Menu)。我已经尝试将它作为一个小部件实现到我的解决方案中,但我认为问题在于它的一些已弃用的方法,例如 .left 和 .right。

我目前正在使用 jquery 1.9.0 和 jQuery UI 1.10.0。

我在编写小部件/升级它们方面并没有真正的经验,因此我们将不胜感激任何帮助,因为它似乎是一个用于显示复杂菜单的非常有用的小部件。

CSS 片段:

    .ios-style, .ios-style ul, .ios-style ol { background: #fff; height: 200px; padding: 2px; width: 260px; }
    .ios-style { overflow-x: hidden; overflow-y: auto; }
    .ios-style::-webkit-scrollbar { width: 5px; height: 5px; }
    .ios-style::-webkit-scrollbar-thumb { background: rgba(128, 128, 128, 0.6); border-radius: 4px; }
    .ios-style ul, .ios-style ol { overflow-y: visible; border: none; }
    .ios-style.ui-menu-icons .ui-menu-item a { position: inherit; }
    .ios-style .ui-menu-item a { cursor: pointer; outline: none; }

jQuery 小部件代码:

<script type="text/javascript">
    $.widget( "ui.iosMenu", {
        options: {
            backText:      'Back',
            slideDuration: 400,
            slideEasing:   'linear'
        },

        _insertBackButtons: function() {
            this.element.find( 'li ul, li ol' ).prepend(
                $( '<li>' +
                     '  <span class="ui-icon ui-icon-carat-1-w"></span>' +
                     '  <a href="#menu-back" class="ios-menu-back-link">' +
                                this.options.backText +
                     '  </a>' +
                     '</li>'
            ) );
            return this;
        },

        _create: function( options ) {
            var iosMenu = this;

            iosMenu
                ._insertBackButtons()
                .element
                    .addClass( 'ios-style' )
                    .menu({
                        // When a submenu shows up, place it just to the right
                        // of the current menu. Later, we'll slide it into view.
                        position: {
                            my: 'left top',
                            at: 'right top',
                            of: iosMenu.element
                        }
                    });

            var menu = iosMenu.element.data( 'menu' );

            // Override menu#select to account for nesting and back buttons:
            menu.select = function( event ) {
                if ( menu.active && menu.active.find( '> .ios-menu-back-link' ).length ) {
                    // if you selected "back", go back:
                    menu.focus( event, menu.active );
                    if ( menu.left( event ) ) {
                        event.stopImmediatePropagation();
                    }
                    event.preventDefault();
                } else if ( menu.active && menu.active.find( '> ul' ).length ) {
                    // if you selected something with children, show the children:
                    menu.focus( event, menu.active );
                    if ( menu.right( event ) ) {
                        event.stopImmediatePropagation();
                    }
                    event.preventDefault();
                } else {
                    menu._trigger( 'select', event, { item: menu.active } );
                }
            };

            // Override menu#left to enable sliding behavior:
            menu.left = function( event ) {
                var newItem = this.active && this.active.parents( 'li:not(.ui-menubar-item) ').first(),
                        self        = this,
                        parent;
                if ( newItem && newItem.length ) {
                  newItem.find( '> a' ).addClass( 'ui-state-focus' ).removeClass( 'ui-state-active' );
                    parent = this.active.parent();
                    parent
                        .attr( 'aria-hidden', 'true' )
                        .attr( 'aria-expanded', 'false' )
                        .animate({
                            left: self.element.css( 'width' )
                        }, iosMenu.options.slideDuration, iosMenu.options.slideEasing, function() {
                            parent.hide();
                            self.focus( event, newItem );
                        })
                    return true;
                } else if ( event && event.which === $.ui.keyCode.ESCAPE ) {
                    // #left gets called both for left-arrow and escape. If it's the
                    // latter and we're at the top, fire a "close" event:
                    self._trigger( 'close', event );
                }
            };

            // Override menu#_open to enable sliding behavior:
            var menuOpenWithoutSliding = menu._open;
            menu._open = function ( submenu ) {
                menuOpenWithoutSliding.call( this, submenu );
                submenu.animate({
                    left: 0
                }, iosMenu.options.slideDuration, iosMenu.options.slideEasing);
            };

            // Override menu#_startOpening so that hovering doesn't
            // initiate the sliding:
            menu._startOpening = function() {
                clearTimeout( this.timer );
            }
        },

        destroy: function() {
          var menu = this.element && this.element.data( 'menu' );
            menu && menu.destroy();
        }
    });

    $(function() {
        var list    = $( '#breakfast-menu' );
        var firstLI = list.find( 'li' ).first();
        list
            .iosMenu()
            .focus()
            .menu( 'focus', {}, firstLI )
            .bind( 'menuselect', function( event, ui ) {
                $('#log').append( '<li>' + $(ui.item).text() + '</li>' );
            });
    });
</script>

运行时错误

    var menu = iosMenu.element.data( 'menu' );

在萤火虫中引发“菜单”未定义错误。

4

3 回答 3

1

也遇到了同样的错误。我花了很长时间才找到罪魁祸首,但它是这样的:

jquery.ui.menu.js

添加

this.mouseHandled = false;

折叠所有和扩展功能模糊功能(within _create)替换为 1.9.0 版本

jquery.ui.widget.js

添加

$.data( element, this.widgetName, this ); 

发挥_createWidget作用

jquery-ui.iosmenu.js

添加

menu.active = menu.active || $( event.target ).closest( ".ui-menu-item" );

menu.selec

希望这可以帮助

于 2013-02-15T08:37:35.997 回答
1

我不使用其他更改。我想使用 jquery UI 的 CDN 版本,所以我没有搞砸这些。这是我的兼容版本。

$.widget( "ui.iosMenu", {
    options: {
        backText:      'Back',
        slideDuration: 200,
        slideEasing:   'linear'
    },

    _insertBackButtons: function() {
        this.element.find( 'li ul, li ol' ).prepend(
            $( '<li class="ui-menu-item ios-menu-back-link" role="presentation">' +
                 '  <a href="#menu-back" class="ui-corner-role" tabindex="-1" role="menuitem" aria-haspopup="true" >' +
                '   <span class="ui-menu-icon ui-icon ui-icon-carat-1-w"></span>' +
                            this.options.backText +
                 '  </a>' +
                 '</li>'
        ) );
        return this;
    },

    _create: function( options ) {
        var iosMenu = this;

        iosMenu
            ._insertBackButtons()
            .element
                .addClass( 'ios-style' )
                .menu({
                    // When a submenu shows up, place it just to the right
                    // of the current menu. Later, we'll slide it into view.
                    position: {
                        my: 'left top',
                        at: 'right top',
                        of: iosMenu.element
                    }
                });

        var menu = iosMenu.element.data( 'uiMenu' );

        // Override menu#select to account for nesting and back buttons:
        menu.select = function( event ) {
            //menu.active = menu.active || $( event.target ).closest( ".ui-menu-item" ); //new random line
            if ( menu.active && menu.active.find('a').attr("href") == "#menu-back" ) {
                // if you selected "back", go back:
                menu.focus( event, menu.active );
                menu.collapse( event );
                /*
                if ( menu.collapse( event ) ) {
                    event.stopImmediatePropagation();
                }
                event.preventDefault();*/
            } else if ( menu.active && menu.active.find( '> ul' ).length ) {
                // if you selected something with children, show the children:
                menu.focus( event, menu.active );
                menu.expand( event );
                /*
                if ( menu.expand( event ) ) {
                    event.stopImmediatePropagation();
                }
                event.preventDefault();*/
            } else {
                menu._trigger( 'select', event, { item: menu.active } );
            }
        };
        /*
        // Override menu#expand to add return true:
        menu.expand = function( event ) {
            var newItem = this.active &&
                this.active
                    .children( ".ui-menu " )
                    .children( ".ui-menu-item" )
                    .first();

            if ( newItem && newItem.length ) {
                this._open( newItem.parent() );

                // Delay so Firefox will not hide activedescendant change in expanding submenu from AT
                this._delay(function() {
                    this.focus( event, newItem );
                });
                return true;
            }
        };*/

        // Override menu#collapse to enable sliding behavior:
        menu.collapse = function( event ) {
            var newItem = this.active && this.active.parents( 'li:not(.ui-menubar-item) ').first(),
                    self        = this,
                    parent;
            if ( newItem && newItem.length ) {
              newItem.find( '> a' ).addClass( 'ui-state-focus' ).removeClass( 'ui-state-active' );
                parent = this.active.parent();
                parent
                    .attr( 'aria-hidden', 'true' )
                    .attr( 'aria-expanded', 'false' )
                    .animate({
                        left: self.element.css( 'width' )
                    }, iosMenu.options.slideDuration, iosMenu.options.slideEasing, function() {
                        parent.hide();
                        self.focus( event, newItem );
                    })
                //return true;
            } else if ( event && event.which === $.ui.keyCode.ESCAPE ) {
                // #left gets called both for left-arrow and escape. If it's the
                // latter and we're at the top, fire a "close" event:
                self._trigger( 'close', event );
            }
        };




        // Override menu#_open to enable sliding behavior:
        var menuOpenWithoutSliding = menu._open;
        menu._open = function ( submenu ) {
            menuOpenWithoutSliding.call( this, submenu );

            submenu.animate({
                left: 0,
                height: menu.element[0].clientHeight - 4 ,
                width: menu.element[0].clientWidth 
            }, iosMenu.options.slideDuration, iosMenu.options.slideEasing);
        };

        // Override menu#_startOpening so that hovering doesn't
        // initiate the sliding:
        menu._startOpening = function( submenu ) {
            clearTimeout( this.timer );
            // Don't open if already open fixes a Firefox bug that caused a .5 pixel
            // shift in the submenu position when mousing over the carat icon
            if ( submenu.attr( "aria-hidden" ) !== "true" ) {
                return;
            }
        }
    },

    destroy: function() {
      var menu = this.element && this.element.data( 'uiMenu' );
        menu && menu.destroy();
    }
});
于 2013-02-20T03:03:09.350 回答
0

如果您需要在单页应用程序中使用此代码并且必须避免使用“href="menu-back",请改用 rel="" 属性。

 $.widget( "ui.iosMenu", {
    options: {
        backText:      'Back',
        slideDuration: 200,
        slideEasing:   'linear'
    },

    _insertBackButtons: function() {
        this.element.find( 'li ul, li ol' ).prepend(
        $( '<li class="ui-menu-item ios-menu-back-link" role="presentation">' +
                 '  <a rel="#menu-back" class="ui-corner-role" tabindex="-1" role="menuitem" aria-haspopup="true" >' +
                '   <span class="ui-menu-icon ui-icon ui-icon-carat-1-w"></span>' +
                            this.options.backText +
                 '  </a>' +
                 '</li>'
        ) );
        return this;
    },

    _create: function( options ) {
        var iosMenu = this;

        iosMenu
            ._insertBackButtons()
            .element
                .addClass( 'ios-style' )
                .menu({
                    // When a submenu shows up, place it just to the right
                    // of the current menu. Later, we'll slide it into view.
                    position: {
                        my: 'left top',
                        at: 'right top',
                        of: iosMenu.element
                    }
                });

        var menu = iosMenu.element.data( 'uiMenu' );

        // Override menu#select to account for nesting and back buttons:
        menu.select = function( event ) {

             if ( menu.active && menu.active.find('a').attr("rel") == "#menu-back" ) {   
                // if you selected "back", go back:
                menu.focus( event, menu.active );
                menu.collapse( event );

            } else if ( menu.active && menu.active.find( '> ul' ).length ) {
                // if you selected something with children, show the children:
                menu.focus( event, menu.active );
                menu.expand( event );

            } else {
                menu._trigger( 'select', event, { item: menu.active } );
            }
        };


        // Override menu#collapse to enable sliding behavior:
        menu.collapse = function( event ) {
            var newItem = this.active && this.active.parents( 'li:not(.ui-menubar-item) ').first(),
                    self        = this,
                    parent;
            if ( newItem && newItem.length ) {
              newItem.find( '> a' ).addClass( 'ui-state-focus' ).removeClass( 'ui-state-active' );
                parent = this.active.parent();
                parent
                    .attr( 'aria-hidden', 'true' )
                    .attr( 'aria-expanded', 'false' )
                    .animate({
                        left: self.element.css( 'width' )
                    }, iosMenu.options.slideDuration, iosMenu.options.slideEasing, function() {
                        parent.hide();
                        self.focus( event, newItem );
                    })
                //return true;
            } else if ( event && event.which === $.ui.keyCode.ESCAPE ) {

                self._trigger( 'close', event );
            }
        };

        // Override menu#_open to enable sliding behavior:
        var menuOpenWithoutSliding = menu._open;
        menu._open = function ( submenu ) {
            menuOpenWithoutSliding.call( this, submenu );

            submenu.animate({
                left: 0,
                height: menu.element[0].clientHeight - 4 ,
                width: menu.element[0].clientWidth 
            }, iosMenu.options.slideDuration, iosMenu.options.slideEasing);
        };

        // Override menu#_startOpening so that hovering doesn't
        // initiate the sliding:
        menu._startOpening = function( submenu ) {
            clearTimeout( this.timer );
            // Don't open if already open fixes a Firefox bug that caused a .5 pixel
            // shift in the submenu position when mousing over the carat icon
            if ( submenu.attr( "aria-hidden" ) !== "true" ) {
                return;
            }
        }
    },

    destroy: function() {
      var menu = this.element && this.element.data( 'uiMenu' );
        menu && menu.destroy();
    }
});

更新:为了支持 jQuery 2.1.0,我不得不改变这一行......

var menu = iosMenu.element.data( 'uiMenu' );

进入这个

var menu = iosMenu.element.data( 'ui-menu' );

我还加载了 jquery-migrate-1.2.1.min.js 以防万一其他人有类似的问题。

于 2013-12-04T22:12:22.007 回答