我在使用GSAP的ScrollTrigger时遇到了一个问题。
我在不同的视口宽度中设置了固定元素的高度,它在调整窗口大小时工作得很好。
但现在我想通过单击按钮来更改固定元素的高度。我使用了ScrollTrigger.refresh();,但是什么也没有发生。
有人能告诉我怎么修吗?
ScrollTrigger.create({
trigger: '.box',
pin: true,
start: 'top center',
end: () => `+=${$('.h-500').height() - $('.box').height()}`,
markers: true,
id: "box",
onRefreshInit: () => {
},
onRefresh: () => {
}
});
function resizeBox(){
$('.box').css('height', '50px');
ScrollTrigger.refresh();
}body{
margin: 0;
}
.space {
width: 100%;
}
.h-500 {
height: 500px;
background: yellow;
}
.h-1000 {
height: 1000px;
}
.box {
width: 100px;
height: 100px;
background: red;
}
button {
position: fixed;
top: 0;
right: 0;
z-index: 999;
font-size: 20px;
}
@media only screen and (max-width: 600px) {
.box {
height: 5vw;
}
}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg.com/gsap@3/dist/ScrollTrigger.min.js"></script>
<script src="https://unpkg.co/gsap@3/dist/gsap.min.js"></script>
<div class="space h-1000"></div>
<div class="space h-500">
<div class="box"></div>
</div>
<div class="space h-1000"></div>
<button onclick="resizeBox()">resizeBox</button>
发布于 2020-11-04 03:12:26
这在某种程度上是一个棘手的情况,因为您正在调整要固定的相同元素的大小。ScrollTrigger会自动缓存尺寸等,所以当刷新发生时,ScrollTrigger会清除所有内联样式(恰好清除您设置的新值),然后恢复到缓存状态。(我们(GreenSock)还没有确定这是不是一个bug :)
至于现在如何解决这种情况,你可以在盒子周围创建一个空容器,并将其固定住:
<div class="pin-container">
<div class="box"></div>
</div>trigger: ".pin-container"https://stackoverflow.com/questions/64662190
复制相似问题