jquery实现返回顶部

$(function() {
     //先将#back-top隐藏
     $('#back-top').hide();
     //当滚动条的垂直位置距顶部100像素一下时,跳转链接出现,否则消失
     $(window).scroll(function() {
         if ($(window).scrollTop() > 100) {
             $('#back-top').fadeIn(1000);
         } else {
             $("#back-top").fadeOut(1000);
         }
     });
     //点击跳转链接,滚动条跳到0的位置,页面移动速度是1000
     $("#back-top").click(function() {
         $('body').animate({
             scrollTop: '0'
         }, 1000);
         return false; //防止默认事件行为
     })
 })
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>返回顶部</title>
    <script src="scripts/jquery-1.7.1.min.js"></script>
</head>
<body>
<script type="text/javascript"> 
$(function(){
var toppos = 0;
$(".back_top_index").hide();
$(document).scroll(
  function(){
    toppos=$(document).scrollTop();
    var oClient =$(window).height();//当前窗口可视区域高度
    if(toppos>oClient){
      $(".back_top_index").fadeIn();  
    }else if(toppos==0){
      $(".back_top_index").fadeOut(); 
    }
  }
)
$(".back_top_index").click(function(){
    $("html,body").animate({scrollTop:0})
  })
   
})
</script> 
<div style="height:2000px;">3423424</div>
<div class="back_top_index">1</div>
<style>
.back_top_index{width: 50px;height: 50px;position: fixed;bottom: 10px;right: 10px;background-color: red;}
</style>
</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容