JQuery 滚动速度请为瞬时

我希望我的 JQuery 滚动速度是瞬时的(不平滑/不快,但像经典的锚 href="#" 一样瞬时)。这似乎与队列有关,但我该如何更改我的脚本?感谢您的帮助。

https://jsfiddle.net/7f1Ldeqr/

<div style="height:3000px">

<a href="#" id="link">Down</a>

<a name="here" style="position:relative; top:2000px;"></a>

</div>


<script src='http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.js'></script>

<script>

function scrolling(aid){

var aTag = $("a[name='"+ aid +"']");

$('html,body').animate({scrollTop: aTag.offset().top},'fast');}

$("#link").click(function() {

scrolling('here');});

</script>


海绵宝宝撒
浏览 79回答 3
3回答

慕工程0101907

不使用快速,而是将其替换为仅使用 0 的 0.2 之类的值似乎不起作用,因此接近 0 的超小值就可以解决问题。

江户川乱折腾

更改该参数并没有改变我的东西。我遵循了QuantumPuter关于scrollIntoView的建议并且它起作用了(最终不需要JQuery。我添加了术语“href =“#openmenu”onclick =“window.location.hash ='#menu1'””来证明我们可以结合其他东西放在一起并使滚动仍然有效)。https://jsfiddle.net/7k1s6t80/<div style="height:3000px"><a id="forscroll" href="#openmenu" onclick="window.location.hash = '#menu1'">Down</a><a id="here" style="position:absolute; top: 2000px;"></a></div><script>const target = document.getElementById('here'),button = document.getElementById('forscroll');button.addEventListener('click',function(){target.scrollIntoView({block: 'start',behavior:'instant',inline:'start'});});</script>另外,为了回答我的问题,使 JQuery 滚动即时(就像我想的那样与队列有关)=>$('html,body').animate({scrollTop: e.offset().top},{queue: false,duration: 0})

繁花不似锦

将 'fast' 替换为 0。第二个参数是持续时间(以毫秒为单位)<script>function scrolling(aid){var aTag = $("a[name='"+ aid +"']");$('html,body').animate({scrollTop: aTag.offset().top},0);}$("#link").click(function() {scrolling('here');});</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript