在 jQuery 中滚动到顶部按钮

scroll to top button in jquery

本文关键字:顶部 按钮 滚动 jQuery      更新时间:2023-09-26

我的页面上有div.triangle 不透明度 0
我希望一旦点击
页面底部,它就会淡入不透明度 .95然后,我希望它在再次
单击$(".Triangle")后滚动到$(".container")的顶部到目前为止,我已经有了这个,我想除了事件之外,我大部分都做对了?

   <script type="text/javascript">
      $(document).ready(function(){
          $(".container").scroll(function(){
              var currentPosition = $(document).scrollTop(); 
              var totalHeight = $(document).offsetHeight;
              var visibleHeight = $(document).clientHeight;
                  if (visibleHeight + currentPosition >= totalHeight){
                      $(".triangle").fadeTo("slow",.95);
                  }
          });
          $(".triangle").click(function(){
              $(".container").animate({scrollTop:0}, 'slow');
              $(".triangle").fadeTo("slow",0);
          });
      });
   </script>

试试这个:

  $(document).ready(function(){
      var bottom = ($(window).outerHeight() - $(window).height()) - 50; // 50 pixel to the bottom of the page; 
      $(window).scroll(function(){
          if ($(window).scrollTop() >= bottom ) {
                  $(".triangle").fadeTo("slow",.95);
             } else {
                  $(".triangle").fadeOut("slow");
             }
      });
      $(".triangle").click(function(){
          $('html, body').animate({scrollTop:0}, 'slow');
          $(".triangle").fadeOut("slow");
      });
  });