gpt4 book ai didi

javascript - 到达屏幕上的某个点后使 div 停止

转载 作者:行者123 更新时间:2023-11-28 00:41:57 29 4
gpt4 key购买 nike

我试图让 div“框”停止沿屏幕移动,并在到达 div“横幅”的末尾后停留:

这怎么可能?如果您注意到我的代码中的任何错误,也将不胜感激。

$(function(){
$('.banner').hide().slideDown(800);
$('.banner h1').hide().show().addClass('animateText')
$('.box').hide()
var $win = $(window);

$win.on("scroll", function () {
var top = $win.scrollTop();
$('.banner h1').css({'transform': 'rotate(' + top + 'deg)'})

if (top > 150){
$('.banner h1').css({'opacity':0})
$('.banner p').slideDown(800);
$('.banner h2').css({'left': top*1.2 + 'px'})
$('.box').slideDown(600)
$('.box').css({'transform': 'translateY(' + top/2 + 'px)'})

} else if(top < 400){
$('.banner h1').css({'opacity':1})
$('.banner p').slideUp(800);
$('.banner h2').css({'left': '-400px', 'transition':'600ms'})
$('.box').slideUp(1000);

}

//kinda like this:
if(top>$('.banner').outerHeight()){
$('.box').stopRightWhereYouAre()
}
});
});
body{
margin:0;
padding: 0;
font-family: arial;
}
.banner{
background: linear-gradient(to left, black 50%, white 50%);
height: 100vh;
}

.banner h1{
text-align: center;
font-size: 300px;
padding-top: 250px;
margin: 0;
font-weight: bold;
background: linear-gradient(to right, black 50%, white 50%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}

.animateText{
animation: slide 2000ms ease-in-out;
}

@keyframes slide{
0%{
margin-left: -800px
}
50%{
font-size: 350px;
}
100%{
font-size: 300px;
text-align: center;
}
}

.banner p{
color:white;
width: 450px;
margin-left: 60%;
margin-top: -17%;
font-size: 1.5em;
display: none;
}

h2{
font-weight: bold;
font-size: 100px;
color:black;
top:250px;
left:-400px;
position: absolute;

}

.box{
background-color: black;
height: 60vh;
width: 40%;
margin-top: -400px;
padding-top: 100px;
}

.h{
margin-bottom: 300vh
}

.box p{
color:white;
width: 400px;
font-size: 1.5em;
margin: 0;
margin-left: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="banner">
<h1>HELLO</h1>
<div class="rotate"><h2>DESIGN</h2></div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="box">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="h"></div>

最佳答案

$(function(){
$('.banner').hide().slideDown(800);
$('.banner h1').hide().show().addClass('animateText')
$('.box').hide()
var $win = $(window);

$win.on("scroll", function () {
var top = $win.scrollTop();
$('.banner h1').css({'transform': 'rotate(' + top + 'deg)'})

if (top > 150 && top<$('.banner').outerHeight()){
$('.banner h1').css({'opacity':0})
$('.banner p').slideDown(800);
var lf = top*1.2 < (window.innerWidth - $('.banner')[0].offsetWidth) ? top*1.2 : (window.innerWidth - $('.banner')[0].offsetWidth);
$('.banner h2').css({'left': lf + 'px'})
$('.box').slideDown(600);
var ty = ($('.banner')[0].offsetHeight - top*1.2) <= 0 ? 0 : ($('.banner')[0].offsetHeight - top*1.2)
$('.box').css({'transform': 'translateY(' + ty + 'px)'})

} else if(top < 400 && top<$('.banner').outerHeight()){
$('.banner h1').css({'opacity':1})
$('.banner p').slideUp(800);
$('.banner h2').css({'left': '-400px', 'transition':'600ms'})
$('.box').slideUp(1000);

}

});
});
body{
margin:0;
padding: 0;
font-family: arial;
}
.banner{
background: linear-gradient(to left, black 50%, white 50%);
height: 100vh;
}

.banner h1{
text-align: center;
font-size: 300px;
padding-top: 250px;
margin: 0;
font-weight: bold;
background: linear-gradient(to right, black 50%, white 50%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}

.animateText{
animation: slide 2000ms ease-in-out;
}

@keyframes slide{
0%{
margin-left: -800px
}
50%{
font-size: 350px;
}
100%{
font-size: 300px;
text-align: center;
}
}

.banner p{
color:white;
width: 450px;
margin-left: 60%;
margin-top: -17%;
font-size: 1.5em;
display: none;
}

h2{
font-weight: bold;
font-size: 100px;
color:black;
top:250px;
left:-400px;
position: absolute;

}

.box{
background-color: black;
height: 60vh;
width: 40%;
/*margin-top: -400px;*/
padding-top: 100px;
}

.h{
margin-bottom: 300vh
}

.box p{
color:white;
width: 400px;
font-size: 1.5em;
margin: 0;
margin-left: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="banner">
<h1>HELLO</h1>
<div class="rotate"><h2>DESIGN</h2></div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="box">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="h"></div>

关于javascript - 到达屏幕上的某个点后使 div 停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52946561/

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