学习笔记--jquery 页面滚动返回顶部

在html页面加入<meta name="toTop" content="true">即可.

$(function () {
    if ($("meta[name=toTop]").attr("content") == "true") {
      $("<div id='toTop'><img src='1.png'></div>").appendTo('body');
      $("#toTop").css({
         '50px',
        height: '50px',
        bottom: '10px',
        right: '15px',
        position: 'fixed',
        cursor: 'pointer',
        zIndex: '999999'
      });
      if ($(this).scrollTop() == 0) {
        $("#toTop").hide();
      }
      $(window).scroll(function (event) {
        /* Act on the event */
        if ($(this).scrollTop() == 0) {
          $("#toTop").hide();
        }
        if ($(this).scrollTop() != 0) {
          $("#toTop").show();
        }
      });
      $("#toTop").click(function (event) {
        /* Act on the event */
        $("html,body").animate({
              scrollTop: "0px"
            },
            666
        )
      });
    }
  });
原文地址:https://www.cnblogs.com/mrxia/p/4546212.html