当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js实现返回顶部效果

js实现返回顶部效果

2019年03月22日  | 移动技术网IT编程  | 我要评论

话不多说,请看代码:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>js点击按钮到页面最底部/返回页面顶部代码</title>
<style type="text/css">
 #back-to-top{ position: fixed; bottom: 50px; right: 10%; }
</style>
</head>
<body>
<div style="height: 3000px;"></div>
<div id="back-to-top">
 <a href="#top" rel="external nofollow" >返回顶部</a>
</div>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
 <script type="text/javascript">
 $(document).ready(function(){
  //首先将#back-to-top隐藏
  $("#back-to-top").hide();
  //当滚动条的位置处于距顶部100像素以下时,跳转链接出现,否则消失
  $(function () {
    $(window).scroll(function(){
    if ($(window).scrolltop()>100){
    $("#back-to-top").fadein(100);
    }
    else
    {
    $("#back-to-top").fadeout(100);
    }
    });
    //当点击跳转链接后,回到页面顶部位置
    $("#back-to-top").click(function(){
    $('body,html').animate({scrolltop:0},"speed");
    return false;
    });
    });
    });
 </script>
</body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网