欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js返回顶部实例分享

程序员文章站 2023-01-14 14:01:12
话不多说,请看实例 1、html结构
2、css样式 .r...

话不多说,请看实例

1、html结构

<div class="return_top"></div>

2、css样式

.return_top{ 
  width: 50px; 
  height: 50px; 
  background: url(../images/lanren.gif) no-repeat center #ff8d16; 
  position:fixed; 
  right: 30px; 
  bottom: 30px; 
  display: none; 
  cursor: pointer; 
  z-index: 99;
}

3、js效果

<script>
   $(function(){
    $(window).scroll(function(){
      var topdistance=$(window).scrolltop();  //获取鼠标在本窗口现有状态下移动的高度
      if(topdistance>100){  //如果移动高度大于100px,顶部图标单单显示出,如果移动高度小于等于100,顶部图标不显示
        $('.return_top').fadein(800);
      }else{
        $('.return_top').fadeout(800);
      }
    });
    $('.return_top').on('click',function(){
      $('html,body').animate({scrolltop:0},800); //必须用$('html,body')选择,不然没效果
    })
  });
</script>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!