我正在实现滑动滚动功能,以显示/隐藏这样的滚动固定徽标...
var setLogo = function() {
$('.moveable').each(function() {
$(this).css('top',
$('.default').offset().top -
$(this).closest('.container').offset().top
);
});
};
$(document).scroll(function() {
setLogo();
});
setLogo();
img {
width: 200px;
}
.container {
overflow: hidden;
position: relative;
min-height: 600px;
padding: 1em;
}
.dark {
background: #333;
}
.light {
background: #fff;
}
.gradient {
background: #15EA24;
}
.default {
position: fixed;
}
.moveable {
position: absolute;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container dark">
<img src="https://www.marcoguglie.it/Codepen/SlipScrollEffect/img/skateboard-light.svg" class="default">
</div>
<div class="container light">
<img src="https://www.marcoguglie.it/Codepen/SlipScrollEffect/img/skateboard-dark.svg" class="moveable">
</div>
<div class="container gradient">
<img src="https://www.marcoguglie.it/Codepen/SlipScrollEffect/img/skateboard-gradient.svg" class="moveable">
</div>
除Microsoft Edge之外,在我尝试过的每种浏览器中,一切看起来都很不错。
滚动时,徽标会跳来跳去,通常看起来很生涩。
有人知道为什么会发生这种情况以及是否有解决方法吗?
答案 0 :(得分:1)
一个想法,不是使用Javascript,而是使用纯CSS。
下面是一个例子。
诀窍是定位固定的图像,然后裁剪它们。我们需要clip容器的原因是,固定位置将忽略溢出,但不会忽略clip。
更新:刚刚在Edge中测试过此功能,就会发现是什么原因造成的。
img {
width: 200px;
}
.container {
position: relative;
height: 600px;
}
.clipper {
position: absolute;
clip: rect(0, auto, auto, 0);
width: 100%;
height: 100%;
}
.dark {
background: #333;
}
.light {
background: #fff;
}
.gradient {
background: #15EA24;
}
.moveable {
position: fixed;
top: 0;
left: 0;
z-index: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container dark">
<div class="clipper">
<img src="https://www.marcoguglie.it/Codepen/SlipScrollEffect/img/skateboard-light.svg" class="moveable image1">
</div>
</div>
<div class="container light">
<div class="clipper">
<img src="https://www.marcoguglie.it/Codepen/SlipScrollEffect/img/skateboard-dark.svg" class="moveable image2">
</div>
</div>
<div class="container gradient">
<div class="clipper">
<img src="https://www.marcoguglie.it/Codepen/SlipScrollEffect/img/skateboard-gradient.svg" class="moveable image3">
</div>
</div>
答案 1 :(得分:1)
我的解决方案不使用Javascript,将图像发送到后台并设置background-attachment: fixed;
可以解决问题:
img {
width: 200px;
}
.container {
overflow: hidden;
position: relative;
min-height: 600px;
}
.dark {
background: #333;
}
.light {
background: #fff;
}
.gradient {
background: #15EA24;
}
.img {
background-attachment: fixed;
background-repeat: no-repeat;
background-size: contain;
width: 100%;
height: 100%;
position: absolute;
}
.img1 {
background-image: url('https://www.marcoguglie.it/Codepen/SlipScrollEffect/img/skateboard-light.svg');
}
.img2 {
background-image: url('https://www.marcoguglie.it/Codepen/SlipScrollEffect/img/skateboard-dark.svg');
}
.img3 {
background-image: url('https://www.marcoguglie.it/Codepen/SlipScrollEffect/img/skateboard-gradient.svg');
}
<div class="container dark">
<div src="" class="default img img1"></div>
</div>
<div class="container light">
<div src="https://www.marcoguglie.it/Codepen/SlipScrollEffect/img/skateboard-dark.svg" class="moveable img img2"></div>
</div>
<div class="container gradient">
<div src="https://www.marcoguglie.it/Codepen/SlipScrollEffect/img/skateboard-dark.svg" class="moveable img img3"></div>
</div>
希望获得帮助。
答案 2 :(得分:-1)