網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
使用background-attachment實(shí)現(xiàn)視差滾動(dòng)、水波
作者:@我不認(rèn)識(shí)你 更新時(shí)間: 2022-02-12 編程語(yǔ)言基本內(nèi)容
background-attachment 設(shè)置背景圖像是否固定或者隨著頁(yè)面的其余部分滾動(dòng)。
這個(gè)屬性存在瀏覽器兼容問題。
屬性值:
- scroll 背景圖片隨著頁(yè)面的滾動(dòng)而滾動(dòng),這是默認(rèn)的。
- fixed 背景圖片不會(huì)隨著頁(yè)面的滾動(dòng)而滾動(dòng)。
- local 背景圖片會(huì)隨著元素內(nèi)容的滾動(dòng)而滾動(dòng)。
視差滾動(dòng)
可以實(shí)現(xiàn)簡(jiǎn)單的視差滾動(dòng)效果,類似于 QQ官網(wǎng) 的效果。
參考文章 滾動(dòng)視差?CSS 不在話下
<div class="container">
<div class="content">1</div>
<div class="g-img1"></div>
<div class="content">2</div>
<div class="g-img2"></div>
</div>
.container{
width: 100%;
height: 100%;
overflow: auto;
}
.content{
width: 100%;
height: 100%;
align-items: center;
}
.g-img1 {
width: 100%;
height: 100%;
background-image: url('../image/11.webp');
background-attachment: fixed;
background-size: 100% 100%;
background-repeat:no-repeat;
}
.g-img2 {
width: 100%;
height: 100%;
background-image: url('../image/22.png');
background-attachment: fixed;
background-size: 100% 100%;
background-repeat:no-repeat;
}
效果
水波效果
原理是基于上面文章的,這里是對(duì)這個(gè)效果的簡(jiǎn)單學(xué)習(xí)。
原理
水波效果的原理其是用六個(gè) div 疊加在一起,讓6個(gè)圓依次變大。需要注意的地方就是:
要設(shè)置層級(jí),通過延時(shí)函數(shù)讓6個(gè)圓依次變大、層級(jí)越低的圓越早出來。
forwards
:當(dāng)動(dòng)畫完成后,保持最后一個(gè)屬性值(在最后一個(gè)關(guān)鍵幀中定義)。
<div class="main">
<div class="wave wave5"></div>
<div class="wave wave4"></div>
<div class="wave wave3"></div>
<div class="wave wave2"></div>
<div class="wave wave1"></div>
<div class="wave wave0"></div>
</div>
.main {
width: 100%;
height: 100%;
position: relative;
}
.wave {
// 繪制一個(gè)圓,并讓圓居中
position: absolute;
top: calc((100% - 30px) / 2);
left: calc((100% - 30px) / 2);
width: 30px;
height: 30px;
border-radius: 50%;
}
.wave0 {
background: #f00;
z-index: 2;
background-size: auto 106%;
animation: w 1s forwards;
}
.wave1 {
background: #d00;
z-index: 3;
animation: w 1s forwards;
animation-delay: 0.2s;
}
.wave2 {
background: #b00;
z-index: 4;
animation: w 1s forwards;
animation-delay: 0.4s;
}
.wave3 {
background: #900;
z-index: 5;
animation: w 1s forwards;
animation-delay: 0.6s;
}
.wave4 {
background: #700;
z-index: 6;
animation: w 1s forwards;
animation-delay: 0.8s;
}
.wave5 {
background: #500;
z-index: 7;
animation: w 1s forwards;
animation-delay: 1s;
}
@keyframes w {
0% {
top: calc((100% - 30px) / 2);
left: calc((100% - 30px) / 2);
width: 30px;
height: 30px;
}
100% {
top: calc((100% - 200px) / 2);
left: calc((100% - 200px) / 2);
width: 200px;
height: 200px;
}
}
效果
優(yōu)化版
在上面的基礎(chǔ)上,將背景色替換為背景圖片,并將背景設(shè)為固定:background-attachment:fixed;
<div class="main">
<div class="wave wave5"></div>
<div class="wave wave4"></div>
<div class="wave wave3"></div>
<div class="wave wave2"></div>
<div class="wave wave1"></div>
<div class="wave wave0"></div>
</div>
.main {
width: 100%;
height: 100%;
position: relative;
}
.wave{
position:absolute;
top:calc((100% - 30px)/2);
left:calc((100% - 30px)/2);
width:30px;
height:30px;
border-radius:50%;
background:url('../image/22.png');
background-attachment:fixed;
background-position:center center;
}
.wave0{
z-index:2;
background-size:auto 106%;
animation:w 1s forwards;
}
.wave1{
z-index:3;
background-size:auto 102%;
animation:w 1s .2s forwards;
}
.wave2{
z-index:4;
background-size:auto 104%;
animation:w 1s .4s forwards;
}
.wave3{
z-index:5;
background-size:auto 101%;
animation:w 1s .5s forwards;
}
.wave4{
z-index:6;
background-size:auto 102%;
animation:w 1s .8s forwards;
}
.wave5{
z-index:7;
background-size:auto 100%;
animation:w 1s 1s forwards;
}
@keyframes w{
0%{
top:calc((100% - 30px)/2);
left:calc((100% - 30px)/2);
width:30px;
height:30px;
}
100%{
top:calc((100% - 350px)/2);
left:calc((100% - 350px)/2);
width:350px;
height:350px;
}
}
原文鏈接:https://blog.csdn.net/weixin_41897680/article/details/122805916
相關(guān)推薦
- 2022-02-16 IDEA 無法彈出模態(tài)框 HTTP 錯(cuò)誤: 狀態(tài)代碼 404,net::ERR_HTTP_RESPO
- 2022-11-09 grep正則表達(dá)式匹配中括號(hào)的方法實(shí)例_正則表達(dá)式
- 2023-04-11 C#圖片處理如何生成縮略圖的實(shí)現(xiàn)_C#教程
- 2023-07-16 spring boot 多環(huán)境配置
- 2023-07-07 根據(jù)文件后綴名稱獲取contentType,其中Minio上傳文件會(huì)用到contentType
- 2023-03-11 CefSharp過濾圖片RequestHandler問題_C#教程
- 2022-06-11 C#實(shí)現(xiàn)DataTable轉(zhuǎn)TXT、CSV文件_C#教程
- 2022-08-21 Mac下python包管理工具pip的安裝_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支