当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQuery实现返回顶部按钮和scroll滚动功能[带动画效果]

jQuery实现返回顶部按钮和scroll滚动功能[带动画效果]

2017年12月12日  | 移动技术网IT编程  | 我要评论

jquery脚本:

 <script type="text/javascript">
     $(function() {
       var scrolldiv = document.createelement('div');
       $(scrolldiv).attr('id', 'totop').html('^ 返回顶部').appendto('body');
       $(window).scroll(function() {
         if ($(this).scrolltop() != 0) {
           $('#totop').fadein();
         } else {
           $('#totop').fadeout();
         }
       });
       $('#totop').click(function() {
         $('body,html').animate({ scrolltop: 0 }, 800);
       })
     });
   </script>

css样式:

<style type="text/css">
     #totop
 {
       width: 100px;
       z-index: 10;
       border: 1px solid #333;
       background: #121212;
       text-align: center;
      padding: 5px;
      position: fixed;
       bottom: 0px;
       right: 0px;
       cursor: pointer;
      display: none;
      color: #fff;
       text-transform: lowercase;
       font-size: 0.9em;
 }
  </style>

带有iframe框架的滚动操作:

<script type="text/javascript">
 <!--
     $().ready(function() {
       $('<div id="return_old_tips" class="btn_return_old" style="position:' + ($.browser.msie ? "absolute" : "fixed") + ';" onclick="return_old({$feedback_flag},\'{$current_url}\')">返回旧版</div>').appendto($("body"));
       if ($.browser.msie) {
        $("#return_old_tips").css("top", 200);
      }
      if ($.browser.msie) {
        top.document.body.onscroll = function() {
          var f = 200 + (top.document.documentelement.scrolltop || top.document.body.scrolltop);
          if (f > parseint($("body").height(), 10)) {
            f = parseint($("body").height(), 10);
          }
           $("#return_old_tips").css({
            top: f,
            left: 0
           });
        }
         top.document.body.onresize = top.document.body.onscroll;
      } else {
        $(window.parent.document).scroll(function() {
           var f = 200 + (top.document.documentelement.scrolltop || top.document.body.scrolltop);
          if (f > parseint($("body").height(), 10)) {
            f = parseint($("body").height(), 10);
          }
          $("#return_old_tips").css({
            top: f,
             left: 0
           });
        }).resize(function() {
           var f = 200 + (top.document.documentelement.scrolltop || top.document.body.scrolltop);
           if (f > parseint($("body").height(), 10)) {
            f = parseint($("body").height(), 10);
          }
          $("#return_old_tips").css({
            top: f,
             left: 0
           });
         });
       }
     });
 //-->
   </script>

以上所述是小编给大家介绍的jquery实现返回顶部按钮和scroll滚动功能[带动画效果],希望对大家有所帮助

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

相关文章:

验证码:
移动技术网