網頁返回頂部之animate方法

點擊返回頂部按鈕,頁面滑動形式回到頂部!html

HTML代碼:this

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

 

CSS代碼:ip

#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}it

 

JavaScript代碼:io

//點擊回到頂部function

  $("#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);
          }
      });
相關文章
相關標籤/搜索