核心屬性: background-attachmentcss
這個屬性就牛逼了, 它能夠定義背景圖片是相對視口固定,html
仍是隨着視口滾動, 加上這個屬性網頁瞬間就從屌絲變成node
高大上。app
咱們來看個例子:函數
html:動畫
<div class="attach view"></div> <div class="text view">I WANT FLY</div> <div class="attach view"></div> <div class="text view">I WANT FLY</div> <div class="attach view"></div> <div class="text view">I WANT FLY</div> <div class="attach view"></div>
css:url
body{ background-color: #cccccc; margin: 0; padding: 0; } .attach{ background-image: url("./img/1.jpg"); background-size: cover; /*background-attachment: fixed;*/ background-position: center center; } .view{ height: 100vh; } .text{ font-size: 45px; text-align: center; line-height: 100vh; color: #ffffff; }
代碼很簡單,讓視口出現滾動條,spa
而後它是這樣的:3d
很普通的一個滾動效果, 而後咱們把註釋去掉,code
就是加上這句話:
background-attachment: fixed;
華麗變身:
因爲它相對視口固定, 看起來就好像只有一個背景同樣。
咱們就用這個屬性來製做水波紋效果:
波紋效果其實就是動態生成幾個div 疊加在一塊兒, 而且背景圖片同樣
它們的寬高逐漸變大, 透明度逐漸變爲0, 而且每一個div有delay, 效果
結束後remove掉,而且屢次點擊產生的波紋的層級愈來愈高才不會被前面的波紋覆蓋;
HTML:
<div class="water-wave"> </div>
CSS:
/*總體的背景*/ .water-wave { /*佔滿整個屏幕*/ position: relative; height: 100vh; width: 100vw; background-image: url("./img/1.jpg"); background-size: cover; background-position: center center; background-attachment: fixed; overflow: hidden; cursor: pointer; } /*存放波紋的一個自適應的正方形*/ .wave-container { position: absolute; /* vmin = 視口width > 視口height ? height : width*/ width: 80vmin; height: 80vmin; } .center{ position: relative; width: 100%; height: 100%; /*背景鋪滿*/ background-size: cover; } .wave { /*每一個波紋居中*/ position: absolute; top: calc((100% - 10vmin) / 2); left: calc((100% - 10vmin) / 2); width: 10vmin; height: 10vmin; border-radius: 50%; /*開啓3d加速*/ transform: translate3d(0, 0, 0); background-image: url("./img/1.jpg"); background-position: center center; background-attachment: fixed; /*全部屬性變化過渡200ms*/ transition: all .2s; } /*每一個波紋的動畫延遲不同, size由大變小再 變爲100%這樣效果更逼真*/ .wave1 { /*forwards停留在動畫的最後一幀*/ animation: move 1s ease-out .1s forwards; background-size: 106%; z-index: 10; } .wave2 { animation: move 1s ease-out .15s forwards; background-size: 102%; z-index: 20; } .wave3 { animation: move 1s ease-out .25s forwards; background-size: 104%; z-index: 30; } .wave4 { animation: move 1s ease-out .4s forwards; background-size: 100%; z-index: 40; } @keyframes move { 0% { top: calc((100% - 10vmin) / 2); left: calc((100% - 10vmin) / 2); width: 10vmin; height: 10vmin; opacity: 1; } /*動畫過程當中不能讓opacity漸變,否則沒有水波紋的效果*/ /*可是這樣又會產生一點小抖動, 不過不影響效果*/ /*你也能夠註釋掉看看*/ 99% { opacity: 1; } 100% { top: calc((100% - 40vmin) / 2); left: calc((100% - 40vmin) / 2); width: 40vmin; height: 40vmin; opacity: 0; } }
JS: 我儘可能每行都寫註釋
const container = document.getElementsByClassName('water-wave'); // 取父級 const number = 4; // 自定義產生幾個水波紋 let index = 0; // 定義每次點擊產生的波紋的層級 const containerWidth = document.body.clientHeight > document.body.clientWidth ? document.body.clientWidth * 0.8 / 2 : document.body.clientHeight * 0.8 / 2; // 取包裹波紋的正方形的半個寬 這是爲了計算點擊時正方形的位置 container[0].addEventListener('click', (e) => { // 傳入事件, 父級, 波紋數, 層級 addWave(e, container[0], number, index++) }, false); // 註冊點擊事件 // 點擊觸發 function addWave(e, parentNode, number, index) { // 渲染完波紋後插入父級, 傳入波紋數, 點擊的座標x, y ,層級 parentNode.appendChild(renderWave(number, e.pageX, e.pageY, index)); // 移除每次點擊產生的波紋, // index是用來識別每次點擊的波紋至關於惟一的ID removeWave(parentNode, index); } // 渲染波紋的函數 function renderWave(number, x, y, z) { let childrenNode = ''; // 建立一個父級div元素用來包裹波紋 let childrenContainer = document.createElement('div'); // 添加一個class用來標記,方便刪除 childrenContainer.classList.add(`remove${z}`); // 循環產生波紋 for (let i = 0; i < number; i++) { childrenNode += `<div class='wave wave${i + 1}'></div>` } // 波紋放進div裏 childrenContainer.innerHTML = `<div class='wave-container' style='left:${x - containerWidth}px;top:${y - containerWidth}px;z-index:${z}'> <div class="center"> ${childrenNode} </div> </div>`; // 返回這個div return childrenContainer; } function removeWave(parentNode, index) { // 延遲3秒刪除波紋 setTimeout(() => { const node = document.getElementsByClassName(`remove${index}`)[0]; parentNode.removeChild(node); }, 3000); }
註釋已經寫的很詳細了, 本身動手寫寫才能發現問題哦
咱們來看看最終效果:
可在下方評論大聲說出你的見解 ↓↓👇