虽然我一直想弄清楚如何整齐地显示很长的导航链接,但我遇到了这个jQuery IOS Drill Down Menu插件(源文章:jQuery Wiki Menu)。我已经尝试将它作为一个小部件实现到我的解决方案中,但我认为问题出现在一些被弃用的方法中,例如.left和.right。
我目前正在使用jquery 1.9.0和jQuery UI 1.10.0。
我在编写小部件/升级它们时并不是很有经验,所以对此的任何帮助都会非常受欢迎,因为它看起来像是一个非常有用的小部件来显示复杂的菜单。
CSS Snipet:
.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' );
在firebug中引发'菜单'未定义错误。
答案 0 :(得分: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
t
希望这有帮助
答案 1 :(得分:1)
我不使用其他更改。我想使用CDN版本的jquery UI,所以我没有搞乱那些。这是我的兼容版本。
$.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();
}
});
答案 2 :(得分: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,以防其他人遇到类似的问题。