返回顶部js

backToTop.js:

(function () {
    var $backToTopEle = $('<div class="backToTop"></div>').appendTo($("body"))
        .click(function () {
            $("html, body").animate({ scrollTop: 0 }, 120);
        }), $backToTopFun = function () {
            var st = $(document).scrollTop(), winh = $(window).height();
            (st > 0) ? $backToTopEle.show() : $backToTopEle.hide();
            //IE6下的定位
            if (!window.XMLHttpRequest) {
                $backToTopEle.css("top", st + winh - 166);
            }
        };
    $(window).bind("scroll", $backToTopFun);
    $(function () { $backToTopFun(); });
})();

样式设置:

.backToTop {
    display: none;
    background:url(../images/go_top.gif);
    background-repeat:no-repeat;
    background-position:0px 0px;
    width:50px;
    height:50px;
    color: #fff;
    font-size: 12px;
    text-align: center;
    position: fixed;
    _position: absolute;
    right: 2px;
    bottom: 2px;
    _bottom: "auto";
    cursor: pointer;
    opacity: .6;
    filter: Alpha(opacity=60);
}
.backToTop:hover
{
    background:url(../images/go_top.gif);
    background-repeat:no-repeat;
    background-position:0px -100px;
}
原文地址:https://www.cnblogs.com/vinsonLu/p/4049869.html