css 滾動視差 之 水波紋效果


核心屬性: background-attachment

這個屬性就牛逼了, 它可以定義背景圖片是相對視口固定,

還是隨着視口滾動, 加上這個屬性網頁瞬間就從屌絲變成

高大上。

我們來看個例子:

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:

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;
}

代碼很簡單,讓視口出現滾動條,

然后它是這樣的:

很普通的一個滾動效果, 然后我們把注釋去掉,

就是加上這句話:

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);
}

注釋已經寫的很詳細了, 自己動手寫寫才能發現問題哦

我們來看看最終效果:

 

可在下方評論大聲說出你的看法 ↓↓👇

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM