如何利用CSS實現(xiàn)視差滾動和抖動效果
發(fā)布時間:2024-09-13 15:56:20 作者:lilly呀
我要評論
通過CSS3和JS,實現(xiàn)前端炫酷的視差滾動和抖動效果,本文詳細介紹了相關CSS樣式和JS腳本,通過監(jiān)聽滾動條位置并調(diào)整元素的top屬性來達到視差效果,當?shù)竭_特定位置時,利用CSS動畫屬性添加抖動效果,示例代碼和效果預覽清晰展示如何操作
背景: 前端的設計效果,越來越炫酷,而這些炫酷的效果,利用css3的動畫效果和js就可以實現(xiàn),簡單的代碼就能實現(xiàn)非常炫酷的效果。原理: 利用 js監(jiān)控scrollTop的位置,通過 top定位圖片的位置,實現(xiàn)視差的滾動;當滾動到目標位置時,通過css3的animation屬性,實現(xiàn)抖動效果。
一、預覽效果

相關素材照片:


二、相關代碼
style部分:
<style>
.index-module--nftBox--3H_AP {
height: 200vh;
background-repeat: no-repeat;
background-size: cover;
background-position: 50%;
width: 100%;
}
.index-module--moto--3sEm1 {
position: absolute;
left: 0;
top: -400px; /*汽車模型初始化位置定點*/
background-image: url(https://resource.nreal.cn/web/images/home/motuo.png);/*汽車圖片*/
transition: .5s;
-webkit-transform: translate(0);
transform: translate(0);
}
.index-module--moto--3sEm1.is-active{ /*汽車模型的抖動效果*/
animation: jump .1s ease-in-out 5 alternate;
}
@keyframes jump {/*汽車模型的抖動效果*/
from { top: -30px; }
to { top: 6px }
}
.index-module--moto--3sEm1, .index-module--nftBox--3H_AP {
height: 200vh;
background-repeat: no-repeat;
background-size: cover;
background-position: 50%;
width: 100%;
}
.index-module--title--rSrVs {
opacity: 1;
width: 80%;
margin: 0 auto;
}
.index-module--titleBox--1VS2L {
height: 50vh;
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: 0;
left: 50%;
-webkit-transform: translate(-50%);
transform: translate(-50%);
}
.hoZaHW {
animation-name: ewofWB;
animation-duration: 300ms;
animation-iteration-count: infinite;
transform-origin: center center;
animation-play-state: running;
}
/* @keyframes ewofWB {
0% { left:0px; top:0px;}
25% { left:200px; top:0px;}
50% { left:200px; top:200px;}
75% { left:0px; top:200px;}
100% { left:0px; top:0px;}
} */
.index-module--nftTitle--20OyY {
width: 100%;
max-width: 800px;
}
.index-module--duang--5jpDr {
-webkit-animation: index-module--circleP--3P-V7 1.3s;
animation: index-module--circleP--3P-V7 1.3s;
-webkit-transform: translateY(400px);
transform: translateY(400px);
}
.index-module--moto--3sEm1 {
position: absolute;
left: 0;
-webkit-transform: translate(0);
transform: translate(0);
}
</style>html部分:
<div id="layout-container" class="content">
<div class="index-module--nftBox--3H_AP" style="background-image:url(https://resource.nreal.cn/web/images/home/motuobg.jpg)">
<div class="index-module--nftBox--3H_AP"></div>
<div class="index-module--moto--3sEm1" id="scrollup"></div>
<div class="index-module--title--rSrVs index-module--titleBox--1VS2L">
<div dur="300" class="sc-bdVaJa hoZaHW">
<img src="https://resource.nreal.cn/web/images/home/title.png" class="index-module--nftTitle--20OyY">
</div>
</div>
</div>
</div>js部分:
<script type="text/javascript">
window.onscroll= function(){
//變量t是滾動條滾動時,距離頂部的距離
var t = document.documentElement.scrollTop||document.body.scrollTop;
var scrollup = document.getElementById('scrollup');
//當滾動到距離頂部200px時
if(t>=200){ scrollup.style.backgroundImage="url(https://resource.nreal.cn/web/images/home/motuo.png)";
scrollup.style.top=6+"px";
if(scrollup.style.top == '6px'){
document.getElementById("scrollup").classList.add("is-active")
}
}else{//恢復正常 scrollup.style.backgroundImage="url(https://resource.nreal.cn/web/images/home/motuo.png)";
scrollup.style.top=-400+"px";
document.getElementById("scrollup").classList.remove("is-active")
}
}
</script>到此這篇關于利用CSS實現(xiàn)視差滾動和抖動效果的文章就介紹到這了,更多相關css視差滾動內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持腳本之家!
相關文章
- 在網(wǎng)頁設計中,視差滾動效果可以為用戶帶來沉浸式的瀏覽體驗,這篇文章主要為大家詳細介紹了如何使用CSS完成視差滾動效果,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨2024-02-28
響應式剪貼蒙版視差滾動效果是一種在網(wǎng)頁設計中常用的視覺效果技術,本文就來介紹一下CSS實現(xiàn)剪切蒙版視差滾動效果,具有一定的參考價值,感興趣的可以了解一下2024-02-27
這篇文章主要介紹了CSS如何完成視差滾動效果,幫助大家更好的理解和學習使用CSS,感興趣的朋友可以了解下2021-04-27- 這篇文章主要介紹了CSS視差滾動效果的相關資料,需要的朋友可以參考下2016-01-28

