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

来源:互联网 发布:2016网络电影排行榜 编辑:程序博客网 时间:2024/05/18 00:15

今天给大家介绍一下如何利用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代码就可以实现回到顶部的功能了,是不是特别简单啊。
如博客内容有问题或有疑义,请及时提出,不甚感谢。本人QQ:208017534

1 0
原创粉丝点击