除了chrome之外,jquery scrolltotop在浏览器中不起作用?

时间:2014-10-16 12:12:58

标签: javascript jquery html google-chrome

我创建了一个在jquery中滚动到顶部的函数。但除了chrome之外,它在浏览器中不起作用。请帮忙。

样式部分:

  <style>
  #gototop {
  cursor: pointer;
  bottom:100px;
  position:fixed;
  right:0;
  z-index:90000000;
  }
  </style>

Html部分:

<div id="gototop"><img src="gototop.png"></div>

脚本部分:

<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
(function(){

    var pstatic = $(window).width()/4;

    $('#gototop').fadeOut(05);

    $(window).scroll(function(){
        var pcurrent = $(this).scrollTop();
        (pcurrent <= pstatic) ? $('#gototop').fadeOut(500) : $('#gototop').fadeIn(500);
    })

    $("#gototop").on("click",function(){
            $('body').animate({scrollTop:0}, 1000, 'swing');
    });

})();
</script>

1 个答案:

答案 0 :(得分:0)

您必须使用bodyhtml标记上的动画功能,以确保它适用于所有浏览器。

$("#gototop").on("click",function(){
    $('body,html').animate({scrollTop:0}, 1000, 'swing');
});
相关问题