弄了好久,实在没办法了,请大神帮忙看下,出在什么问题上

qingwadaxia_1 2020-09-15 08:39:36
<!DOCTYPE html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</title>


</head>
<style>
* {
padding: 0;
margin: 0;
}

body {
width: 98%;
height: 15000px;
margin: 0 auto;
}

.head {
width: 100%;
height: 70px;
background: black;
}

.head .head-right {
float: right;
width: 50%;
height: 30px;
line-height: 30px;
margin-top: 20px;
}

.head .head-right ul li {
float: left;
width: 20%;
color: #fff;
}

.tab1 {
width: 25%;
float: left;
background: #ccc;
height: 36px;
}

.div1 {
height: 36px;
width: 100%;
display: none;
}

.div1.show {
display: block;
}

.fixeds {
position: fixed;
top: 0;
}

.fixeds1 {
position: fixed;
top: 70px;
}
</style>

<body>

<div style="height:100px;">

</div>

<div class="head">
<div class="head-right">
<ul>
<li class="show">按钮1</li>
<li>按钮2</li>
<li>按钮3</li>
<li>按钮4</li>
<li>按钮5</li>
</ul>
</div>
</div>

<div class="div1 show">
<div class="tab1">
tab1
</div>

<div class="tab1">
tab2
</div>

<div class="tab1">
tab3
</div>

<div class="tab1">
tab4
</div>
</div>

<div class="div1">
<div class="tab1 show">
tab5
</div>

<div class="tab1">
tab6
</div>

<div class="tab1">
tab7
</div>

<div class="tab1">
tab8
</div>
</div>

<div class="div1">
<div class="tab1 show">
tab9
</div>

<div class="tab1">
tab10
</div>

<div class="tab1">
tab11
</div>

<div class="tab1">
tab12
</div>
</div>

<div class="div1">
<div class="tab1 show">
tab13
</div>

<div class="tab1">
tab14
</div>

<div class="tab1">
tab15
</div>

<div class="tab1">
tab16
</div>
</div>
<div class="div1">
<div class="tab1 show">
tab17
</div>

<div class="tab1">
tab18
</div>

<div class="tab1">
tab19
</div>

<div class="tab1">
tab20
</div>
</div>

<script>
var headRli = document.querySelector(".head-right").querySelectorAll("li");
var headh = document.querySelector(".head");
var div1 = document.querySelectorAll(".div1");

for (let i = 0; i < headRli.length; i++) {
headRli[i].index = i;
headRli[i].onclick = function() {
for (let i = 0; i < headRli.length; i++) {
div1[i].style.display = "none";
}
div1[this.index].style.display = "block";
}
}

var twoHeight = headh.offsetHeight + div1.offsetHeight;

window.onscroll = function() {
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

if (scrollTop > twoHeight - 10) {
headh.classList.add("fixeds");
// div1.classList.add("fixeds1");
for (let i = 0; i < div1.length; i++) {
div1[i].classList.add("fixeds1");
}
} else {
headh.classList.remove("fixeds");
// div1.classList.remove("fixeds1");
for (let i = 0; i < div1.length; i++) {
div1[i].classList.remove("fixeds1");
}
}
}
</script>
</body>

</html>

上面是下拉滚动一定位置之后,head 和div1 两块固定吸顶在顶部,本身代码没什么问题,现在问题是点击按钮1~5 切换对应div1 之后,再下拉,吸顶效果就没有了,难点就在于,div1 是一堆数组都需要吸顶,切换之后就不能吸顶了,我现在用for循环,弄的两块都不吸顶了,也没报错,请大神帮忙看看,
...全文
1822 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qingwadaxia_1 2020-09-17
  • 打赏
  • 举报
回复
引用 6 楼 天际的海浪 的回复:
div1是数组,数组没有.offsetHeight属性。 你可以div1[0].offsetHeight这样获取。 但div1[0].offsetHeight获取的是第一个div1的高度。 如果你每个div1的高度不一样就需要在window.onscroll事件内获取当前显示的div1的高度。
真的是点醒,感谢,自己真的找了好久
qingwadaxia_1 2020-09-16
  • 打赏
  • 举报
回复
引用 1 楼 天际的海浪 的回复:

        var headRli = document.querySelector(".head-right").querySelectorAll("li");
        var headh = document.querySelector(".head");
        var div1 = document.querySelectorAll(".div1");

        for (let i = 0; i < headRli.length; i++) {
            headRli[i].index = i;
            headRli[i].onclick = function() {
                for (let i = 0; i < headRli.length; i++) {
                    div1[i].classList.remove("show");
                }
                div1[this.index].classList.add("show");
            }
        }

        window.onscroll = function() {
            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            var twoHeight = headh.offsetHeight + document.querySelector(".div1.show").offsetHeight;

            if (scrollTop > twoHeight - 10) {
                headh.classList.add("fixeds");
                // div1.classList.add("fixeds1");
                for (let i = 0; i < div1.length; i++) {
                    div1[i].classList.add("fixeds1");
                }
            } else {
                headh.classList.remove("fixeds");
                // div1.classList.remove("fixeds1");
                for (let i = 0; i < div1.length; i++) {
                    div1[i].classList.remove("fixeds1");
                }
            }
        }


大神,你好,请问我是存在什么问题呢,
chinaskysun 2020-09-16
  • 打赏
  • 举报
回复
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; 改为 var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop 试试看
尚稻山 2020-09-16
  • 打赏
  • 举报
回复
我这试了一下你代码,没发现有你说的效果啊。。。
天际的海浪 2020-09-16
  • 打赏
  • 举报
回复
div1是数组,数组没有.offsetHeight属性。 你可以div1[0].offsetHeight这样获取。 但div1[0].offsetHeight获取的是第一个div1的高度。 如果你每个div1的高度不一样就需要在window.onscroll事件内获取当前显示的div1的高度。
天际的海浪 2020-09-15
  • 打赏
  • 举报
回复

        var headRli = document.querySelector(".head-right").querySelectorAll("li");
        var headh = document.querySelector(".head");
        var div1 = document.querySelectorAll(".div1");

        for (let i = 0; i < headRli.length; i++) {
            headRli[i].index = i;
            headRli[i].onclick = function() {
                for (let i = 0; i < headRli.length; i++) {
                    div1[i].classList.remove("show");
                }
                div1[this.index].classList.add("show");
            }
        }

        window.onscroll = function() {
            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            var twoHeight = headh.offsetHeight + document.querySelector(".div1.show").offsetHeight;

            if (scrollTop > twoHeight - 10) {
                headh.classList.add("fixeds");
                // div1.classList.add("fixeds1");
                for (let i = 0; i < div1.length; i++) {
                    div1[i].classList.add("fixeds1");
                }
            } else {
                headh.classList.remove("fixeds");
                // div1.classList.remove("fixeds1");
                for (let i = 0; i < div1.length; i++) {
                    div1[i].classList.remove("fixeds1");
                }
            }
        }


87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧