[英]How to scroll to a specific div when the mouse is scrolled with jQuery?
I have this code right now which scrolls to the correct place when a button is clicked: 我现在有了这段代码,单击按钮时它会滚动到正确的位置:
$("#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");
});
What I want to do now is I want the page to scroll to the next div when the scroll wheel is moved down/up. 我现在想做的是,当向下/向上滚动滚轮时,我希望页面滚动到下一个div。 I've been searching google for a while now and all that comes up is stuff about smooth scrolling which is not what I want. 我一直在搜索google一段时间,出现的所有内容都是关于平滑滚动的,这不是我想要的。 Any help would be great, thanks. 任何帮助将是巨大的,谢谢。
Checkout this answer and it's comments, and the MDN page on the wheel event . 查看此答案及其注释以及wheel event的MDN页面 。
Looks like you can bind to the event like this: 看起来您可以像这样绑定到事件:
$(window).on('wheel', function(event){
But that might be overwhelming as it's each "click" of the mousewheel. 但这可能是压倒性的,因为它是鼠标滚轮的每次“单击”。
See this fiddle . 看到这个小提琴 。 Only tested in Chrome. 仅在Chrome中测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.