您的位置:首页 > Web前端 > JQuery

利用JQuery实现从底部回到顶部的功能

2016-12-26 22:50 399 查看
今天给大家介绍一下如何利用JQuery实现从任意地方返回顶部的功能,现在大部分网站因为篇幅比较长,所以都会设置这么一个按钮,利用这个按钮可以实现从底部返回顶部的功能。废话不多说,下面就给出这么一个例子介绍一下,它是怎么实现的。

实现方法一:

<footer>
<p class="ft-copyright">强仔仔 Design by Linzhiqaing 蜀ICP备11002373号-1</p>
<div id="tbox">
<div id="log_id" style="float:left;position: relative;margin-left: -400px;margin-bottom:-5px;display: none">
<img src="images/weixing-ma.jpg">
</div>
<div style="float:right;"><a id="togbook" href="#顶部的一个ID"></a></div>
<div style="float:left"><a id="gotop"></a></div>
</div>
</footer>


这第一个方法比较简单,就是在超链接中写上“#顶部的ID”就可以实现回到顶部的功能了,不过如果是这种方法的话就不会出现那种滑动的效果,交互性不太好。

下面在介绍一种可以实现滑动的回到顶部功能,并且可以自己设置滑动的速度和回到顶部的位置等。推荐大家用这种模式实现回到顶部的功能,下面来实现的例子。

实现方法二:

<footer>
<p class="ft-copyright">强仔仔 Design by Linzhiqaing 蜀ICP备11002373号-1</p>
<div id="tbox">
<div id="log_id" style="float:left;position: relative;margin-left: -400px;margin-bottom:-5px;display: none">
<img src="images/weixing-ma.jpg">
</div>
<div style="float:right;"><a id="togbook" href="#"></a></div>
<div style="float:left"><a id="gotop"></a></div>
</div>
</footer>

<script type="text/javascript">
$("#gotop").click(function () {
var speed=200;//滑动的速度
$('body,html').animate({ scrollTop: 0 }, speed);
return false;
});
</script>


通过写一段js代码就可以实现回到顶部的功能了,是不是特别简单啊。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: