繁体   English   中英

使用jQuery滚动鼠标时如何滚动到特定的div?

[英]How to scroll to a specific div when the mouse is scrolled with jQuery?

我现在有了这段代码,单击按钮时它会滚动到正确的位置:

$("#skills").click(function() {
    $('html, body').animate({scrollTop: $("#skills_page").offset().top}, 500);
    $(this).addClass("toggled_alt");
    $("#skills_id").css("background-color", "#646464");
    $("#contact_id, #home_id, #about_id").css("background-color", "transparent");
    $("#home, #contact, #about").removeClass("toggled");
    $("#home, #contact, #about").removeClass("toggled_alt");
});

我现在想做的是,当向下/向上滚动滚轮时,我希望页面滚动到下一个div。 我一直在搜索google一段时间,出现的所有内容都是关于平滑滚动的,这不是我想要的。 任何帮助将是巨大的,谢谢。

查看此答案及其注释以及wheel eventMDN页面

看起来您可以像这样绑定到事件:

$(window).on('wheel', function(event){

但这可能是压倒性的,因为它是鼠标滚轮的每次“单击”。

看到这个小提琴 仅在Chrome中测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM