网页返回顶部之animate方法

点击返回顶部按钮,页面滑动形式回到顶部!

HTML代码:

<div id="ctop">回到<br>顶部</div>

 

CSS代码:

#ctop{width:45px; height:45px; position: fixed; left:50%; bottom:120px; border:solid 1px #ccc; background: #fff; margin-left: 600px; text-align: center; line-height: 22px;cursor: pointer; display: none;font-size: 12px; color: #999}

 

JavaScript代码:

//点击回到顶部

  $("#ctop").click(function(){
        $("html,body").animate({scrollTop:'0px'},300)
  })
//判断元素在网页下滑多少像素时显示
      $(window).bind('scroll',function(){
          var len=$(this).scrollTop()
          if(len>=500){
               $('#ctop').fadeIn(500);
          }else{
               $('#ctop').fadeOut(200);
          }
      });
posted @ 2016-11-21 17:33  知乎其微  阅读(472)  评论(1编辑  收藏  举报

你要相信今天的积累将是以后的一大笔财富!

ohmygirl@2014