Javascript 滚动jquery问题。如何自动滚动 onclick 到一个 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5903614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scrolling jquery question. How to auto scroll onclick to a div
提问by TaylorMac
Just wondering if its possible to scroll to a certain div's position on a page onclick. I can't seem to find anywhere on the internet where this is documented.
只是想知道是否可以滚动到页面上的某个 div 位置 onclick。我似乎无法在互联网上找到任何记录了这一点的地方。
My idea is to click a button, and it will take you to the position of that div on the page, possible by the div ID.
我的想法是点击一个按钮,它会带你到页面上那个 div 的位置,可能通过 div ID。
Regards,
问候,
Taylor
泰勒
回答by Chad
Im not sure when you want the event to fire so here is the generic version
我不确定您希望事件何时触发,所以这里是通用版本
$(selector).click(function() {
//scroll to div
});
If you want a <button/>
with an id of myButton
that when clicked will cause you to scroll to a <div/>
with and id of myDiv
over the course of half a second:
如果你想要一个<button/>
带有 id 的myButton
点击时会导致你在半秒的过程中滚动到<div/>
带有 id 的myDiv
:
$('#myButton').click(function() {
//optionally remove the 500 (which is time in milliseconds) of the
//scrolling animation to remove the animation and make it instant
$.scrollTo($('#myDiv'), 500);
});
Using the jQuery ScrollToplugin.
使用 jQuery ScrollTo插件。
回答by James Montagne
If you don't need an animated scroll just use this:
如果您不需要动画滚动,请使用以下命令:
<a href="#myID">click this</a>