1. 程式人生 > >JS 回到頂端 back to top

JS 回到頂端 back to top

cti mov chang -a height inter posit app 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>

  

JS 回到頂端 back to top