1、实现滚动贴合优化滚动效果
图1 滚动贴合
尝试使用 CSS-Scroll-Snap(CSS Scroll Snap - CSS(层叠样式表) | MDN),但使用鼠标滚动时,会出现连续滚动,触控板效果不错但鼠标效果不好,所以放弃使用这一方法。
推荐使用:Swiper3|Swiper中文网
ps:
使用 swiper 时出现一个小问题:无法触发鼠标的点击、拖动事件,查找文档后发现将 simulateTouch 设置为 false 即可。
2、实现视差滚动效果
图2 视差滚动效果
利用 background-attachment 设置背景图像是否固定或者随着页面的其余部分滚动
代码:
HTML 部分:
<div class="a-text">1</div>
<div class="a-img1">2</div>
<div class="a-text">3</div>
<div class="a-img2">4</div>
<div class="a-text">5</div>
<div class="a-img3">6</div>
<div class="a-text">7</div>
CSS 部分:
<style>
div {
height: 100vh;
background: rgba(0, 0, 0, .7);
color: #fff;
line-height: 100vh;
text-align: center;
font-size: 20vh;
}
.a-img1, .a-img2, .a-img3{
background-attachment: fixed;
background-size: cover;
background-position: center center;
}
.a-img1 {
background-image: url(https://images.pexels.com/photos/1097491/pexels-photo-1097491.jpeg);
}
.a-img2 {
background-image: url(https://images.pexels.com/photos/2437299/pexels-photo-2437299.jpeg);
}
.a-img3 {
background-image: url(https://images.pexels.com/photos/1005417/pexels-photo-1005417.jpeg);
}
</style>