为了账号安全,请及时绑定邮箱和手机立即绑定

JQuery 滚动速度请为瞬时

JQuery 滚动速度请为瞬时

海绵宝宝撒 2023-10-14 18:12:18
我希望我的 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>
查看完整描述

3 回答

?
慕工程0101907

TA贡献1887条经验 获得超5个赞

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



查看完整回答
反对 回复 2023-10-14
?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

更改该参数并没有改变我的东西。我遵循了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})


查看完整回答
反对 回复 2023-10-14
?
繁花不似锦

TA贡献1851条经验 获得超4个赞

将 '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>


查看完整回答
反对 回复 2023-10-14
  • 3 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信