gpt4 book ai didi

jquery - 使用jquery的窗口滚动效果

转载 作者:行者123 更新时间:2023-11-28 06:55:55 25 4
gpt4 key购买 nike

我正在使用 ID 为 first、second 和 third 的三个 div。在我的第一个 div 中,我调用了三个类 d,c,e。我也在我的第二个 div 中调用这个类。当我的页面向下滚动并且看到我的第三个 div 时,所有这些我称为 d、c、e 的类都应该被删除。我假设这些应该通过 jquery 或通过 jquery 动画效果来完成。谁能建议我如何从我的 div 中删除所有这些类。我的类(class)包含 .png 文件。如何通过jquery编写窗口滚动效果的代码并删除所有这些类。

HTML 我的页面 jQuery 视差插件演示

</head>

<body>

<div id="first" >
<div class="d" >
<div class="c">
<div class="e"></div>



This is my first div to display image.


</div>
</div>

</div>
</div>
<div id="second">
</div>
<div id="third">
</div>

</body>
</html>

CSS

 @charset "utf-8";
#first, #second{
width: 100%;
}
#first{

background:url('images/rc1.jpg') 50% 0 no-repeat fixed;
color: white;
height: 600px;
margin: 0 auto;
padding: 160px 0 0 0;
padding: 0;
perspective: 1px;

}
#first .d{
margin: auto;
position:relative;
width:=218px;
height: 73px;
left: auto;
top: 10%;
background: url('images/Logo.png') top left;
background-repeat: no-repeat;
background-position-x:50px;

}
#first .c{
margin: auto;
position:relative;
width: =774px;
height: 209px;
left: auto;
top: 120%;
/*width:=137px;
height:88px;*/
background: url('images/big_text.png');
background-repeat: no-repeat;
background-position-x:339px;
}
#first .e{
margin: auto;
position:relative;
width: =127px;
height: 37px;
left:auto;
top: 180%;
background: url('images/scroll_text.png');
background-repeat: no-repeat;
background-position-x: 680px;
}

最佳答案

这将使用窗口滚动和第三个元素位置来完成这个技巧

$(window).scroll(function (event) {

var elemTop = $("#third").offset().top;
var elemBottom = elemTop + $("#third").height();

var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();

// element in window viewport remove the classes
if(elemTop <= docViewBottom && $("#first").find('.d').length > 0)
{
console.log("no more classes for the first div");
$("#first").find('.d, .c, .e').removeClass();
}
// element is not more in window viewport and the divisions have no class then
// add these classes
else if((elemTop > docViewBottom || elemBottom < docViewTop)
&& $("#first").find('.d').length == 0)
{
console.log("classes back to first div");
$("#first div:eq(0)").addClass("d");
$("#first div:eq(1)").addClass("c");
$("#first div:eq(2)").addClass("e");
}

});
@charset "utf-8";
#first, #second{
width: 100%;
}
#first{

background:url('images/rc1.jpg') 50% 0 no-repeat fixed;
color: white;
height: 600px;
margin: 0 auto;
padding: 160px 0 0 0;
padding: 0;
perspective: 1px;
border: 1px solid red;

}

#second{

background:url('images/rc1.jpg') 50% 0 no-repeat fixed;
color: white;
height: 600px;
margin: 0 auto;
padding: 160px 0 0 0;
padding: 0;
perspective: 1px;
border: 1px solid blue;

}

#third{

background:url('images/rc1.jpg') 50% 0 no-repeat fixed;
color: white;
height: 600px;
margin: 0 auto;
padding: 160px 0 0 0;
padding: 0;
perspective: 1px;
border: 1px solid yellow;

}

#first .d{
margin: auto;
position:relative;
width:218px;
height: 73px;
left: auto;
top: 10%;
background: url('images/Logo.png') top left;
background-repeat: no-repeat;
background-position-x:50px;

}
#first .c{
margin: auto;
position:relative;
width: 774px;
height: 209px;
left: auto;
top: 120%;
/*width:=137px;
height:88px;*/
background: url('images/big_text.png');
background-repeat: no-repeat;
background-position-x:339px;
}
#first .e{
margin: auto;
position:relative;
width: 127px;
height: 37px;
left:auto;
top: 180%;
background: url('images/scroll_text.png');
background-repeat: no-repeat;
background-position-x: 680px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="first" >
<div class="d" >
<div class="c">
<div class="e"></div>



This is my first div to display image.


</div>
</div>

</div>

<div id="second">
</div>
<div id="third">
</div>

关于jquery - 使用jquery的窗口滚动效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33514113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com