jQuery - 没有动画的 ScrollTop

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10143706/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:52:49  来源:igfitidea点击:

jQuery - ScrollTop without animation

jqueryanimationscrollscrolltop

提问by user970727

How can I use the scrolltop without an animation

如何在没有动画的情况下使用滚动条

This code works:

此代码有效:

var offTop = $('#box').offset().top;
offTop  = offTop-43;
$('#mainCt').animate({scrollTop: '+=' + offTop + 'px'}, 400);

And here are my (not working solutions):

这是我的(不起作用的解决方案):

$("#mainCt").scrollTop('+=' + offTop + 'px');                 // doesn't work
$("#mainCt").scrollTop('+='+offTop);                          // doesn't work
hhh = setTimeout(' $("#mainCt").scrollTop('+offTop+');',800); // doesn't work

DEMO
http://jsfiddle.net/DNNFF/9/

演示
http://jsfiddle.net/DNNFF/9/

采纳答案by chepe263

maybe if you don't want an animation or anything fancy just use an anchor

也许如果您不想要动画或任何花哨的东西,请使用锚点

<a name="top"></a>

Place it where you need to scroll

把它放在你需要滚动的地方

and in your function where you are calling use

并在您调用 use 的函数中

document.location.href="#top";

You could also create a function to append the anchor before the element, do the document.locationthing and later remove that anchor.

您还可以创建一个函数来在元素之前附加锚点,执行此操作document.location然后删除该锚点。

http://jsfiddle.net/fSrxr/1/

http://jsfiddle.net/fSrxr/1/

回答by Rory McCrossan

Try this:

尝试这个:

var offTop = $('#box').offset().top - 43;
$('#mainCt').scrollTop(offTop);

The scrollTopproperty accepts just an integer, no suffixes or units required.

scrollTop属性只接受一个整数,不需要后缀或单位。

回答by Jeroen Flamman

Skip jQuery. Just use JavaScript:

跳过jQuery。只需使用 JavaScript:

window.scroll(0, 0);

回答by Mrigesh Raj Shrestha

Why not use it with less duration. I fiddled it and it had no animation. SInce there will be no time to see the animation, there will be no animation at all.

为什么不以较短的时间使用它。我摆弄它,它没有动画。既然来不及看动画,也就没有动画了。

var offTop = $('#box').offset().top;
offTop  = offTop-43;
$('#mainCt').animate({scrollTop: '+=' + offTop + 'px'}, 50); //50 added here as duartion

回答by Mr_DeLeTeD

Can't you play with the duration ?

你不能玩持续时间吗?

var offTop = $('#box').offset().top;
offTop  = offTop-43;
$('#mainCt').delay('800').animate({scrollTop: '+=' + offTop + 'px'}, 1);

回答by Mr_DeLeTeD

var offTop = $('#box').offset().top;
$(window).scrollTop(parseInt(offTop))