DIV CSS 佈局教程網

用 JavaScript 做飄雪動畫:動態建 DOM 與定時下落實作
編輯:關於JavaScript     

原理:

一、用 JS 動態建立 DIV,透過指定不同的 class 套用不同背景圖,做出多種雪花樣貌。

二、用 JS 取得這些 DIV 並持續改寫它的 top 屬性值;一旦下落高度超過視窗高度,就把該元素移除。

三、還有優化空間,先分享出來給大家參考。

HTML

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>雪花飛舞</title>
<link rel="stylesheet" href="css/index.css">
<script src="js/move.js"></script>
</head>
<body>
<div class="snow_parent" id="js_sonw">
</div>
</body>
</html>

CSS

四種雪花各自用 background-position 從同一張雪堆圖上裁出不同造型,再用 transform scale 縮小。

 *{ margin:0; padding:0; list-style: none; border: none; }
body{ width: 100%; height:600px; background:#000; }
.snow_parent{ position: relative; width: 100%; height:100%; overflow: hidden; margin: 0 auto; }
.snow_parent div.parent{
    background-image: url(../img/snow.png);
    float: left;
    -webkit-transform: scale(.1);
    -moz-transform: scale(.1);
    -o-transform: scale(.1);
    -ms-transform: scale(.1);
    transform: scale(.1);
    position: absolute;
}
.snow_one{ width: 180px; height: 180px; background-position:0 0; background-repeat: no-repeat; left:-70px; top: -95px; }
.snow_two{ width: 140px; height: 140px; background-position:-220px -18px; left:-30px; top: -75px; }
.snow_three{ width:150px; height: 150px; background-position:-400px -15px; left:-20px; top: -80px; }
.snow_four{ width: 160px; height: 160px; background-position:-10px -206px; }
.snow_four{ left:-10px; top: -85px; }

JS

 /* creatBy jiucheng 2016-4-24 */
window.onload = function(){
    init();
}

// 建立 DIV
function creatDiv(){
    // 建立 DIV 並掛到父元素上
    var snowDiv = document.createElement("div");
    document.getElementById("js_sonw").appendChild(snowDiv);

    // 讓 class 隨機,呈現不同雪花
    var whatName = ["snow_one parent","snow_two parent","snow_three parent","snow_four parent"];
    var index = Math.floor(Math.random() * whatName.length);
    snowDiv.className = whatName[index];

    // 隨機水平位置
    var whatLeft = getLeft() + 'px';
    snowDiv.style.left = whatLeft;
    return snowDiv;
}

// 取得隨機 left 值
function getLeft(){
    var eleParent = document.getElementById("js_sonw");
    var style = window.getComputedStyle(eleParent);
    // CSS 中的 left 是負數,這裡要補回來
    var maxWidth = parseInt(style.width) + 70;
    var randomLeft = Math.floor(Math.random() * maxWidth);
    return randomLeft;
}

// 向下移動
function moveDown(){
    var moveElem = creatDiv();
    var eleStyle = window.getComputedStyle(moveElem);
    var eleTop = parseInt(eleStyle.top);

    var t = setInterval(function(){
        eleTop++;
        moveElem.style.top = eleTop + "px";
        // 落到視窗底部後停止計時器並移除元素
        if(eleTop >= window.innerHeight){
            clearInterval(t);
            document.getElementById("js_sonw").removeChild(moveElem);
        }
    }, 10); // 每10毫秒下落1px
}

function init(){
    // 動態設定 body 高度
    document.body.style.height = window.innerHeight + "px";
    // 每100毫秒產生一片雪花
    var t = setInterval(function(){
        moveDown();
    }, 100);
}

以上就是這次 JavaScript 飄雪效果的分享,希望能給大家一個參考。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved