Renpengpeng 2017-10-12 14:26 采纳率: 100%
浏览 1709
已采纳

请问为什么js滚动滚动条到顶部之后就不能动了?

第二次发生过这种情况,js或者jquery使用animate滚动滚动条到顶部或者任意一个
位置,滚动条就好像被锁定一样,动也动不了,用过stop(),没有效果,求解,下面是代码!

<html>
<head>
<meta charset="utf-8">
<script src="j.js"></script>
<style>
* {
    padding:0;
    margin:0;
    border:0;
}
.one,.two,.three {
    display:block;
}
.one {
    width:1px;
    height:1px;
    background-color:#f00;
}
.tow {
    background-color:#00f;
}
.three {
    background-color:#cecece;
}
</style>
</head>
<body>
<!-- jquery自写滚动屏幕 -->
<div class="one">one</div>
<div class="two">two</div>
<div class="three"></div>
<script>
    $(document).ready(function(){

    //获取当前文档的宽 高
        nowWidth = $(window).width();
        nowHeight = $(window).height();
        //获取大的背景div
        one = $(".one");
        two = $(".two");
        three = $(".three");
        <!-- alert(typeof(nowHeight)); -->
        //开始设置宽度高度为当前屏幕高度

        one.css({"height":nowHeight,"width":nowWidth});
        two.css({"height":nowHeight,"width":nowWidth});
        three.css({"height":nowHeight,"width":nowWidth});

        //开始监听滚动栏
        //准备获取文档
        var ht = $("document,html,body");
        $(document).scroll(function(){
            //获取当前滚动条距离文档高度
            var top = $(document).scrollTop();
            //如果高度 > 1  移动到第二屏 针对第一屏
            if(top > 2 && top < nowHeight){
                //设置滚动条高度为第一屏高度
                ht.animate({"scrollTop":nowHeight},1000,function(){
                    stop(true,true);
                });
            }
            if(top > nowHeight + 2 && top < (nowHeight * 2)){
                ht.animate({"scrollTop":nowHeight *2},1000);
            }
        })

    })


</script>
</body>
</html>

  • 写回答

1条回答

  • jigsaw 2017-10-13 01:13
    关注
    1. ht.animate({"scrollTop":nowHeight},1000) 这里的scrollTop 会再次调用scroll 滚动事件 2.逻辑不对 if 判断这里逻辑不对

    建议:声明变量时,最好加上var

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突