JS 回到顶端 back to top

CSS:
.backtoTop2{
    height: 40px;
    width: 50px;
    position: fixed;     
    padding-top: 8px;
    right: 100px;
    z-index: 9999;
    background: red;
    cursor: pointer;
    text-align: center;
}

JS:
$(document).ready(function(){
    $(window).scroll( function() {                    // change scroll event
        var height = $(window).height(),          //get window height
              top = $(document).scrollTop();       //get scroll top
        if(top > height ){                                    //more than height to show
            $("#backToTop2").fadeIn(300).css({
                top: height-80
            });
        }
        if(top < height ){                                   //less than height to disappear
            $("#backToTop2").fadeOut(200);
        }
    });
    /*back to top click*/
    $('#backToTop2').click(function(){
        $('html, body').animate({                     // Animation effects --> move
            scrollTop: 0
        }, 500);
    });
});
  
HTML:
<divid="backToTop2"class="backtoTop2"></div>

  

posted @ 2017-10-02 11:03  笑逗逗  阅读(1988)  评论(0编辑  收藏  举报