页面较长时,使用返回顶部按钮比较方便,在电商中必备操作。下面自己制作一个js文件,totop.js,把它直接引用到需要的网页中即可。

$(function () {
    $("body").append($("<div id='totop'></div>"));
    $("#totop").hide();
    $(window).scroll(function () {
        if ($(window).scrollTop() > 100) {
            $("#totop").fadeIn(500);
        }
        else {
            $("#totop").fadeOut(500);
        }
    });
    $("#totop").click(function () {
        $('body,html').animate({ scrollTop: 0 }, 100);
        return false;
    });
    window.onload = function () {
        $(window).scroll();
    };
});

 css:

/*回到顶部*/
#totop{
    background:url(/themes/default/images/top_bg.png) no-repeat left top;
    width:40px ;height:40px;
    position:fixed;
    left:50%;bottom:20%;
    margin-left:605px;
}
#totop:hover{
    background:url(/themes/default/images/top_bg.png) no-repeat left -40px;
    cursor:pointer
}

 

相关文章:

  • 2022-12-23
  • 2021-07-31
  • 2022-12-23
  • 2021-05-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-04
猜你喜欢
  • 2022-12-23
  • 2021-08-16
  • 2022-02-04
  • 2022-01-21
  • 2022-02-12
  • 2022-03-07
相关资源
相似解决方案