单击javascript顺畅滚动 [英] javascript smooth scroll on click

查看:88
本文介绍了单击javascript顺畅滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此链接:

<a class="" onclick="location.href='#top'" href="superContent">superContent</a>

一次做两件事:


  1. 将用户跳转到页面顶部

  2. 执行此其他(无关)ajax加载功能

一切都很好,除了我试图弄清楚如何让它更顺畅地滚动到顶部。我已经尝试添加.scroll将它附加到我的jquery scrollTo插件,但没有任何反应,这可能与我正在使用javascript onclick这一事实有关,而href属性则完全不同。

Everything works great, except I'm trying to figure out how to get it to scroll to the top more smoothly. I've tried adding .scroll to attach it to my jquery scrollTo plugin, but nothing happens, which probably has something to do with the fact that I'm using javascript onclick, while the href attribute does something else entirely.

有没有办法将动画平滑滚动附加到onclick =location.href ='#top'?

Is there a way to attach animated smooth-scrolling to onclick="location.href='#top'" ?

推荐答案

试试这个,它会动画 scrollTop()函数。

Try this, it animates the scrollTop() function.

设置link的ID:

<a id="link">link</a>

要滚动的jquery:

jquery to scroll:

$('#link').click(function(e){
  var $target = $('html,body');
  $target.animate({scrollTop: $target.height()}, 500);
});

这篇关于单击javascript顺畅滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆