原生js+css實現重力模擬彈跳系統的登錄頁面


   今天小穎把之前保存的js特效視頻看了一遍,跟着視頻敲了敲嘻嘻,用原生js實現一個炫酷的登錄頁面。怎么個炫酷法呢,看看下面的圖片大家就知道啦。

效果圖:

不過在看代碼之前呢,大家先和小穎看看css中的opacity、transition、box-shadow這三個屬性。

1.opacity

作用:設置一個元素的透明度

定義和用法

opacity 屬性設置元素的不透明級別。

默認值: 1
繼承性: no
版本: CSS3
JavaScript 語法: object.style.opacity=0.5

語法

opacity: value|inherit;
描述 測試
value 規定不透明度。從 0.0 (完全透明)到 1.0(完全不透明)。 測試
inherit 應該從父元素繼承 opacity 屬性的值。  

親自試一試 - 實例

2.transition

作用:將元素從一種樣式逐漸改變為另一種的效果。

定義和用法

transition 屬性是一個簡寫屬性,用於設置四個過渡屬性:

  • transition-property
  • transition-duration
  • transition-timing-function
  • transition-delay

注釋:請始終設置 transition-duration 屬性,否則時長為 0,就不會產生過渡效果。

默認值: all 0 ease 0
繼承性: no
版本: CSS3
JavaScript 語法: object.style.transition="width 2s"

語法

transition: property duration timing-function delay;
描述
transition-property 規定設置過渡效果的 CSS 屬性的名稱。
transition-duration 規定完成過渡效果需要多少秒或毫秒。
transition-timing-function 規定速度效果的速度曲線。
transition-delay 定義過渡效果何時開始。

親自試一試 - 實例

3.box-shadow

作用:給元素添加陰影效果。

定義和用法

box-shadow 屬性向框添加一個或多個陰影。

提示:請使用 border-image-* 屬性來構造漂亮的可伸縮按鈕!

默認值: none
繼承性: no
版本: CSS3
JavaScript 語法: object.style.boxShadow="10px 10px 5px #888888"

語法

box-shadow: h-shadow v-shadow blur spread color inset;

注釋:box-shadow 向框添加一個或多個陰影。該屬性是由逗號分隔的陰影列表,每個陰影由 2-4 個長度值、可選的顏色值以及可選的 inset 關鍵詞來規定。省略長度的值是 0。

描述 測試
h-shadow 必需。水平陰影的位置。允許負值。 測試
v-shadow 必需。垂直陰影的位置。允許負值。 測試
blur 可選。模糊距離。 測試
spread 可選。陰影的尺寸。 測試
color 可選。陰影的顏色。請參閱 CSS 顏色值。 測試
inset 可選。將外部陰影 (outset) 改為內部陰影。 測試

親自試一試 - 實例

怎么實現的呢,哈哈哈,代碼看這里:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>夢幻登錄</title>
    <style type="text/css">
    * {
        margin: 0;
        padding: 0;
        list-style: none;
    }

    body {
        overflow: hidden;
    }

    #bg_wrap {
        width: 100%;
        height: 100%;
        position: absolute;
        left: 0;
        top: 0;
        overflow: hidden;
    }

    #bg_wrap div {
        width: 100%;
        height: 100%;
        position: absolute;
        left: 0;
        top: 0;
        opacity: 0;
        /* 設置透明度 */
        transition: opacity 3s;
    }
    /* nth-of-type(1) *篩選選擇器選擇第一個*/

    #bg_wrap div:nth-of-type(1) {
        opacity: 1;
    }

    #Login {
        width: 272px;
        height: 300px;
        margin: 200px auto;
    }

    #Login .move {
        position: absolute;
        top: -100px;
        z-index: 999;
    }

    #Login h3 {
        width: 270px;
        font-size: 30px;
        font-weight: 700;
        color: #fff;
        font-family: '微軟雅黑';
        text-align: center;
        margin-bottom: 30px;
        cursor: move;
        /* top: 100px; */
    }
    /*  #username {
        top: 170px;
    }

    #password {
        top: 225px;
    } */

    #Login input.text {
        width: 270px;
        height: 42px;
        color: #fff;
        background: rgba(45, 45, 45, 0.15);
        border-radius: 6px;
        border: 1px solid rgba(255, 255, 255, 0.15);
        box-shadow: 0 2px 3px 0 rgba(0, 0, 0, 1.0) inset;
        text-indent: 10px;
    }

    #Login input.btn {
        /* top: 280px; */
        background: #ef4300;
        width: 272px;
        height: 44px;
        border-radius: 6px;
        color: #fff;
        box-shadow: 0 15px 30px 0 rgba(255, 255, 255, 0.25) inset, 0 2px 7px 0 rgba(0, 0, 0, 0.2);
        /* -webkit-box-shadow: 0 15px 30px 0 rgba(255, 255, 255, 0.25) inset, 0 2px 7px 0 rgba(0, 0, 0, 0.2);
        -moz-box-shadow: 0 15px 30px 0 rgba(255, 255, 255, 0.25) inset, 0 2px 7px 0 rgba(0, 0, 0, 0.2); */
        border: 0;
        text-align: center;
    }
    /* #Login input.focus {
        outline: none;
        box-shadow: 0 2px 3px 0 rgba(0, 0, 0, 0.2) inset;
    } */

    input::-webkit-input-placeholder {
        color: #fff;
    }
    </style>
</head>

<body>
    <div id="bg_wrap">
        <div><img src="images/1.jpg" width="100%" height="100%"></div>
        <div><img src="images/2.jpg" width="100%" height="100%"></div>
        <div><img src="images/3.jpg" width="100%" height="100%"></div>
    </div>
    <div id="Login">
        <h3 id="title" class="move">User Login</h3>
        <form action="#" method="post" target="_blank">
            <input type="text" placeholder="UserName" name="username" id="username" class="text move">
            <input type="password" placeholder="PassWord" name="password" id="password" class="text move">
            <input type="submit" value="Sign in" class="btn move" id="submit">
        </form>
    </div>
    <script type="text/javascript">
    /*背景漸變*/
    /*function(){}   匿名函數
      ()()           IIFE匿名函數立刻執行,函數自執行體*/
    (function() {
        var timer = null; //聲明定時器
        var oImg = document.querySelectorAll('#bg_wrap div') //h5最新元素獲取寫法獲取到的是一組元素
        //querySelector獲取單個元素的   兼容ie8
        var len = oImg.length; //3
        var index = 0;
        timer = setInterval(function() {
            oImg[index].style.opacity = 0;
            index++;
            // if(index>=3){
            // 	index=0;
            // }
            index %= len; //index=index%len求模取余 0%3=0; 1%3=1; 2%3=2; 3%3=0;
            oImg[index].style.opacity = 1;
        }, 2000);
    })();
    // 重力模擬彈跳系統
    (function() {
        /*
        改變定位元素的top值
        達到指定位置之后進行彈跳一次
        多個元素一次運動
        動畫序列*/
        var oMove = document.querySelectorAll('.move');
        var oLen = oMove.length;
        var timer = null;
        var timeout = null;
        var speed = 3; //移動距離
        move(oLen - 1);

        function move(index) {
            if (index < 0) {
                clearInterval(timer); //清除循環定時器
                clearTimeout(timeout); //清除延時定時器
                return; //終止函數
            }
            var endTop = 150 + (index * 60); //根據下標計算endTop值
            timer = setInterval(function() {
                speed += 3;
                var T = oMove[index].offsetTop + speed; //設置每一次的top值
                if (T > endTop) {
                    T = endTop;
                    speed *= -1 //取反,讓移動距離變為負數
                    speed *= 0.4;
                    //慢慢停下來
                }
                oMove[index].style.top = T + 'px';
            }, 20);
            timeout = setTimeout(function() {
                clearInterval(timer);
                index--;
                console.log(9);
                move(index);
                console.log(index);
            }, 900) //過900毫秒之后再執行方法里的代碼
        }
    })()
    </script>
</body>

</html>

 git地址


免責聲明!

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



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