简体   繁体   English

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

[英]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 eventMDN页面

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.

相关问题 当正在滚动的内容位于设置为溢出的固定div内时,如何在jquery中执行滚动命令:滚动 - How to execute a scroll command in jquery when what's being scrolled is within a fixed div set to overflow: scroll 鼠标滚动时如何关闭文档滚动-纯JavaScript - How to turn off document scroll when mouse scrolled - Pure javascript 将页面滚动到固定级别后,如何向上滚动特定的div? - How to scroll a specific div up after the page is scrolled to a fixed level? jQuery-如何使按钮滚动到下一个div,但如果向上滚动则响应 - JQuery - How to make a button scroll to next div, but respond if scrolled up 如何让jQuery在鼠标滚动时隐藏div? - How to get jQuery to hide a div on mouse scroll? 当没有其他div滚动时滚动div - Scroll div when no other div is scrolled 如何检测用户何时滚动到 jQuery 中的 div 底部? - How to detect when a user has scrolled to the bottom of a div in jQuery? 如何在其他不可滚动的div上滚动滚动可滚动的div? - How to scroll a scrollable div when scrolled on other, non-scrollable div? 如何通过jquery将HTML内容附加到底部时将div保持滚动到底部,但隐藏滚动条? - How to keep a div scrolled to the bottom as HTML content is appended to it via jquery, but hide the scroll bar? 当div A随动量滚动时,是否可以滚动div B? - Is there a way to scroll div B when div A is scrolled with momentum?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM