原生JS實現百葉窗特效

本文分享一個用原生JS實現的百葉窗特效,效果如下:

代碼實現如下,歡迎大傢復制粘貼。

<!DOCTYPE html>
<html>
 
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>原生JS實現百葉窗特效</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
 
        li {
            list-style: none;
        }
 
        a {
            text-decoration: none;
        }
 
        #box {
            width: 562px;
            padding: 10px 24px 20px;
            background: #EACFCF;
            overflow: hidden;
            margin: 20px auto 0;
        }
 
        .left {
            width: 266px;
            float: left;
        }
 
        .right {
            width: 266px;
            float: right;
        }
 
        #box h2 {
            height: 38px;
            border-bottom: 1px solid #D2A1A1;
            padding-left: 20px;
        }
 
        #box h2 strong {
            float: left;
            line-height: 38px;
            color: #885050;
            font-weight: normal;
        }
 
        #box h2 a {
            float: right;
            width: 52px;
            height: 14px;
            font-size: 12px;
            text-indent: 10px;
            color: #fff;
            line-height: 12px;
            font-weight: normal;
            margin-top: 10px;
        }
 
        #box li {
            height: 30px;
            position: relative;
            border-bottom: 1px dashed #D1A1A1;
        }
 
        #box li div,
        #box li p {
            height: 30px;
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
        }
 
        #box li p {
            background: #fff;
            opacity: 0;
            filter: alpha(opacity=0);
        }
 
        #box li div a,
        #box li div span {
            line-height: 30px;
            font-size: 12px;
            height: 30px;
        }
 
        #box li div a {
            float: left;
            padding-left: 20px;
            color: #7F5454;
            width: 172px;
            overflow: hidden;
        }
 
        #box li div span {
            padding-right: 10px;
            float: right;
            color: #CF9494;
        }
    </style>
    <script type="text/javascript">
        window.onload = function () {
            var oBox = document.getElementById("box");
            var aLi = document.getElementsByTagName('li');
            var i = 0;
            for (i = 0; i < aLi.length; i++) {
                var oP = aLi[i].getElementsByTagName('p')[0];
                oP.iAlpha = 0;
                oP.time = null;
                aLi[i].onmouseover = function () {
                    var oP = this.getElementsByTagName('p')[0];
                    oP.time ? clearInterval(oP.time) : "";
                    oP.style.opacity = 1;
                    oP.style.filter = "alpha(opacity=100)";
                    oP.iAlpha = 100;
                }
                aLi[i].onmouseout = function () {
                    starmove(this.getElementsByTagName('p')[0]);
                };
            }
        };
        function starmove(obj) {
            obj.time ? clearInterval(obj.time) : "";
            obj.time = setInterval(function () { domove(obj); }, 14);
        }
        function domove(obj) {
            var iSpeed = 5;
            if (obj.iAlpha <= iSpeed) {
                clearInterval(obj.time);
                obj.iAlpha = 0;
                obj.time = null;
            } else {
                obj.iAlpha -= iSpeed;
            }
            obj.style.opacity = obj.iAlpha / 100;
            obj.style.filter = "alpha(opacity=" + obj.iAlpha + ")";
        }
    </script>
</head>
 
<body>
    <div id="box">
        <div class="left">
            <h2>
                <strong>最新消息</strong>
                <a>more</a>
            </h2>
            <ul>
                <li>
                    <p></p>
                    <div>
                        <a>課程常見問題合集</a>
                        <span>20110329</span>
                    </div>
                </li>
                <li>
                    <p></p>
                    <div>
                        <a>JS課程結課標準</a>
                        <span>20110327</span>
                    </div>
                </li>
                <li>
                    <p></p>
                    <div>
                        <a>網頁特效大集合(每天更新)</a>
                        <span>20110322</span>
                    </div>
                </li>
                <li>
                    <p></p>
                    <div>
                        <a>上課地點</a>
                        <span>20110319</span>
                    </div>
                </li>
                <li>
                    <p></p>
                    <div>
                        <a>CSS課程最新開班時間</a>
                        <span>20110319</span>
                    </div>
                </li>
                <li>
                    <p></p>
                    <div>
                        <a>JavaScript課程最新開班時間</a>
                        <span>20110319</span>
                    </div>
                </li>
                <li>
                    <p></p>
                    <div>
                        <a>零基礎網頁制作課程</a>
                        <span>20110319</span>
                    </div>
                </li>
            </ul>
        </div>
        <div class="right">
            <h2>
                <strong>新鮮事</strong>
                <a>more</a>
            </h2>
            <ul>
                <li>
                    <p></p>
                    <div>
                        <a>JavaScript實現的3D球面標簽雲效果</a>
                        <span>20110329</span>
                    </div>
                </li>
                <li>
                    <p></p>
                    <div>
                        <a>周六JavaScript聚會活動現場記錄</a>
                        <span>20110327</span>
                    </div>
                </li>
                <li>
                    <p></p>
                    <div>
                        <a>阿裡前端面試題</a>
                        <span>20110319</span>
                    </div>
                </li>
                <li>
                    <p></p>
                    <div>
                        <a>淺析google地圖前端實現原理(一)</a>
                        <span>20110319</span>
                    </div>
                </li>
                <li>
                    <p></p>
                    <div>
                        <a>Windows計算器(簡易版)作業練習</a>
                        <span>20110319</span>
                    </div>
                </li>
                <li>
                    <p></p>
                    <div>
                        <a>手風琴緩動效果</a>
                        <span>20110319</span>
                    </div>
                </li>
                <li>
                    <p></p>
                    <div>
                        <a>JavaScript實現的3D球面標簽雲效果</a>
                        <span>20110319</span>
                    </div>
                </li>
            </ul>
        </div>
    </div>
</body>
 
</html>

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: