有了jQuery手风琴控件,当它离开屏幕时,我如何让它滚动到我选择的项目?
在以下情况下:
手风琴是否有滚动到第二项的选项?
发布于 2010-09-01 20:19:42
您可以尝试使用scrollTo jQuery插件。它让你做这样的事情:
$.scrollTo('div#foo'); // scroll the browser window so div#foo is in view
$('div#foo').('#bar'); // scroll within div#foo so #bar is in view
将ScrollTo()
绑定到accordionactivate
事件,如下所示:
$('#youraccordion').bind('accordionactivate', function(event, ui) {
/* In here, ui.newHeader = the newly active header as a jQ object
ui.newContent = the newly active content area */
$( ui.newHeader ).ScrollTo(); // or ui.newContent, if you prefer
});
何时触发accordionactivate
事件?
激活面板后触发(动画完成后)。如果以前折叠过手风琴,那么
ui.oldHeader
和ui.oldPanel
将是空的jQuery对象。如果手风琴正在折叠,ui.newHeader
和ui.newPanel
将是空的jQuery对象。
参考资料:jQuery UI手风琴
发布于 2014-10-15 08:20:02
它对我有效并经过测试,
$( "#accordion" ).accordion({
heightStyle: "content",
collapsible: true,
active: false,
activate: function( event, ui ) {
if(!$.isEmptyObject(ui.newHeader.offset())) {
$('html:not(:animated), body:not(:animated)').animate({ scrollTop: ui.newHeader.offset().top }, 'slow');
}
}
});
http://jsfiddle.net/ilyasnone/aqw613em/
发布于 2012-03-05 18:05:47
由于我希望折叠为真,所以我添加了一个if测试,以取消正在折叠的所有项的错误。我也不希望标题完全位于页面的顶部,因此我将scrollTop位置降低了100:
$(document).ready(function() {
$(".ui-accordion").bind("accordionchange", function(event, ui) {
if ($(ui.newHeader).offset() != null) {
ui.newHeader, // $ object, activated header
$("html, body").animate({scrollTop: ($(ui.newHeader).offset().top)-100}, 500);
}
});
});
https://stackoverflow.com/questions/3621161
复制相似问题