使用CSS怎么實(shí)現(xiàn)一個(gè)波浪效果

本篇文章為大家展示了使用CSS怎么實(shí)現(xiàn)一個(gè)波浪效果,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

成都創(chuàng)新互聯(lián)公司主營(yíng)玉州網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP軟件開發(fā),玉州h5微信小程序搭建,玉州網(wǎng)站營(yíng)銷推廣歡迎玉州等地區(qū)企業(yè)咨詢

css是什么意思

css是一種用來(lái)表現(xiàn)HTML或XML等文件樣式的計(jì)算機(jī)語(yǔ)言,主要是用來(lái)設(shè)計(jì)網(wǎng)頁(yè)的樣式,使網(wǎng)頁(yè)更加美化。它也是一種定義樣式結(jié)構(gòu)如字體、顏色、位置等的語(yǔ)言,并且css樣式可以直接存儲(chǔ)于HTML網(wǎng)頁(yè)或者單獨(dú)的樣式單文件中,而樣式規(guī)則的優(yōu)先級(jí)由css根據(jù)這個(gè)層次結(jié)構(gòu)決定,從而實(shí)現(xiàn)級(jí)聯(lián)效果,發(fā)展至今,css不僅能裝飾網(wǎng)頁(yè),也可以配合各種腳本對(duì)于網(wǎng)頁(yè)進(jìn)行格式化。

SVG 實(shí)現(xiàn)波浪效果

借助 SVG ,是很容易畫出三次貝塞爾曲線的。

使用CSS怎么實(shí)現(xiàn)一個(gè)波浪效果

<svg width="200px" height="200px" version="1.1" xmlns="http://www.w3.org/2000/svg">
    <text class="liquidFillGaugeText" text-anchor="middle" font-size="42px" transform="translate(100,120)" style="fill: #000">50.0%</text>
    <!-- Wave -->
    <g id="wave">
    <path id="wave-2" fill="rgba(154, 205, 50, .8)" d="M 0 100 C 133.633 85.12 51.54 116.327 200 100 A 95 95 0 0 1 0 100 Z">
    <animate dur="5s" repeatCount="indefinite" attributeName="d" attributeType="XML" values="M0 100 C90 28, 92 179, 200 100 A95 95 0 0 1 0 100 Z;
    M0 100 C145 100, 41 100, 200 100 A95 95 0 0 1 0 100 Z;
    M0 100 C90 28, 92 179, 200 100 A95 95 0 0 1 0 100 Z"></animate>
    </path>
    </g>
    <circle cx="100" cy="100" r="80" stroke-width="10" stroke="white" fill="transparent"></circle>
    <circle cx="100" cy="100" r="90" stroke-width="20" stroke="yellowgreen" fill="none" class="percentage-pie-svg"></circle>
</svg>

畫出三次貝塞爾曲線的核心在于 <path id="wave-2" fill="rgba(154, 205, 50, .8)" d="M 0 100 C 133.633 85.12 51.54 116.327 200 100 A 95 95 0 0 1 0 100 Z">這一段。感興趣的可以自行去研究研究

canvas 實(shí)現(xiàn)波浪效果

使用 canvas 實(shí)現(xiàn)波浪效果的原理與 SVG 一樣,都是利用路徑繪制出三次貝塞爾曲線并賦予動(dòng)畫效果。

使用CSS怎么實(shí)現(xiàn)一個(gè)波浪效果

$(function() {
    let canvas = $("canvas");
    let ctx = canvas[0].getContext('2d');
    let radians = (Math.PI / 180) * 180;
    let startTime = Date.now();
    let time = 2000;
    let clockwise = 1;
    let cp1x, cp1y, cp2x, cp2y;

    // 初始狀態(tài)
    // ctx.bezierCurveTo(90, 28, 92, 179, 200, 100);
    // 末尾狀態(tài)
    // ctx.bezierCurveTo(145, 100, 41, 100, 200, 100);

    requestAnimationFrame(function waveDraw() { 
        let t = Math.min(1.0, (Date.now() - startTime) / time);

        if(clockwise) {
            cp1x = 90 + (55 * t);
            cp1y = 28 + (72 * t);
            cp2x = 92 - (51 * t);
            cp2y = 179 - (79 * t);
        } else {
            cp1x = 145 - (55 * t);
            cp1y = 100 - (72 * t);
            cp2x = 41 + (51 * t);
            cp2y = 100 + (79 * t);
        }

        ctx.clearRect(0, 0, 200, 200); 
        ctx.beginPath();
        ctx.moveTo(0, 100);
        // 繪制三次貝塞爾曲線
        ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, 200, 100);
        // 繪制圓弧
        ctx.arc(100, 100, 100, 0, radians, 0);
        ctx.fillStyle = "rgba(154, 205, 50, .8)";
        ctx.fill();
        ctx.save(); 

        if( t == 1 ) {
            startTime = Date.now();
            clockwise = !clockwise;
        }

        requestAnimationFrame(waveDraw);
    });
})

主要是利用了動(dòng)態(tài)繪制 ctx.bezierCurveTo() 三次貝塞爾曲線實(shí)現(xiàn)波浪的運(yùn)動(dòng)效果,感興趣的可以自行研究。

CSS實(shí)現(xiàn)波浪效果

最開始不是說(shuō)css不能實(shí)現(xiàn)嗎?是,我們沒有辦法直接繪制出三次貝塞爾曲線,但是我們可以利用一些討巧的方法,模擬達(dá)到波浪運(yùn)動(dòng)時(shí)的效果,下面來(lái)看看這種方法。

原理

原理十分簡(jiǎn)單,我們都知道,一個(gè)正方形,給它添加 border-radius: 50%,將會(huì)得到一個(gè)圓形。

使用CSS怎么實(shí)現(xiàn)一個(gè)波浪效果

width: 240px;
height: 240px;
background: #f13f84;
border-radius: 50%;

好的,如果 border-radius 沒到 50%,但是接近 50% ,我們會(huì)得到一個(gè)這樣的圖形(注意邊角,整個(gè)圖形給人的感覺是有點(diǎn)圓,卻不是很圓。)

使用CSS怎么實(shí)現(xiàn)一個(gè)波浪效果

width: 240px;
height: 240px;
background: #f13f84;
border-radius: 40%;

好的,那整這么個(gè)圖形又有什么用?還能變出波浪來(lái)不成?

我們讓上面這個(gè)圖形滾動(dòng)起來(lái)(rotate) ,看看效果:

使用CSS怎么實(shí)現(xiàn)一個(gè)波浪效果

CSS實(shí)現(xiàn)波浪效果

@keyframes rotate{
    from{transform: rotate(0deg)}
    to{transform: rotate(359deg)}
}
.ripple{
    width: 240px;
    height: 240px;
    background: #f13f84;
    border-radius: 40%;
    animation: rotate 3s linear infinite;
}

上述內(nèi)容就是使用CSS怎么實(shí)現(xiàn)一個(gè)波浪效果,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁(yè)題目:使用CSS怎么實(shí)現(xiàn)一個(gè)波浪效果
瀏覽路徑:http://www.muchs.cn/article26/ihegcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、網(wǎng)站策劃、網(wǎng)站改版、網(wǎng)站營(yíng)銷、網(wǎng)頁(yè)設(shè)計(jì)公司、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)