原生JS實現圖片跑馬燈特效

今天給大傢分享一個用原生JS實現的圖片跑馬燈特效,效果如下:

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

<!DOCTYPE html>
<html>
 
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>原生JS實現圖片跑馬燈特效</title>
 
    <style type="text/css">
        * {
            padding: 0;
            margin: 0;
        }
 
        li {
            list-style: none;
        }
 
        img {
            border: none;
        }
 
        body {
            background: #eee;
        }
 
        .slide-module {
            width: 120px;
            height: 400px;
            margin: 0 auto;
            background: #fff;
            border: 1px solid #ccc;
            position: relative;
            top: 50px;
        }
 
        .slide-module .up {
            width: 27px;
            height: 27px;
            /* 向上的箭頭 */
            background: url(images/0.gif) no-repeat;
            position: absolute;
            top: 4px;
            left: 50%;
            margin-left: -16px;
            cursor: pointer;
            filter: alpha(opacity=60);
            opacity: 0.6;
        }
 
        .slide-module .down {
            width: 27px;
            height: 27px;
            /* 向下的箭頭 */
            background: url(images/5.gif) no-repeat;
            position: absolute;
            bottom: 4px;
            left: 50%;
            margin-left: -16px;
            cursor: pointer;
            filter: alpha(opacity=60);
            opacity: 0.6;
        }
 
        .slide-module .wrap {
            width: 120px;
            height: 330px;
            position: absolute;
            top: 35px;
            left: 0;
            overflow: hidden;
        }
 
        .slide-module ul {
            width: 120px;
            position: absolute;
            top: 0;
            left: 0;
        }
 
        .slide-module li {
            width: 120px;
            height: 110px;
            float: left;
        }
 
        .slide-module a {
            display: block;
            width: 100px;
            height: 100px;
            border: 1px solid red;
            margin: 0 auto;
            position: relative;
            top: 4px;
        }
 
        .slide-module a:hover {
            border: 1px solid #333;
        }
 
        .slide-module .active {
            border: 10px solid #000;
        }
    </style>
    <script type="text/javascript">
 
        window.onload = function () {
            miaovSlide('miaovSlide');
        };
 
        function miaovSlide(o) {
            //獲取操作對象容器
            var obj = document.getElementById(o);
            if (!obj) return;
            //獲取對象下面所有的div
            var aDiv = obj.getElementsByTagName('div');
            //獲取向上箭頭
            var oUp = getClass('up');
            //獲取向下箭頭
            var oDown = getClass('down');
            //獲取圖片容器
            var oWrap = getClass('wrap');
            //獲取圖片列表
            var oUl = oWrap.getElementsByTagName('ul')[0];
            //獲取圖片列表項
            var oLi = oUl.getElementsByTagName('li');
 
            var oTime = null;
            var iMs = 30;
            var i = 0;
            var iNum = 5;
            var toggle = -1;
 
            oUl.innerHTML += oUl.innerHTML;
 
            // 點擊向上時,向上走
            oUp.onclick = function () {
 
                toggle = -1;
 
                autoPlay(toggle);
            };
 
            // 點擊向下時,向走走
            oDown.onclick = function () {
                toggle = 1;
                autoPlay(toggle);
            };
 
            // 向上與向下箭頭鼠標移入時,修改其透明度為1
            oUp.onmouseover = oDown.onmouseover = function () {
                this.style.filter = 'alpha(opacity:100)';
                this.style.opacity = 1;
            };
 
            // 向上與向下箭頭鼠標移入時,修改其透明度為0.6
            oUp.onmouseout = oDown.onmouseout = function () {
                this.style.filter = 'alpha(opacity:60)';
                this.style.opacity = 0.6;
            };
 
            // 圖片運動方法,toggle代表向下或是向上的值
            function autoPlay(toggle) {
                // 清除原有定時器
                if (oTime) {
                    clearInterval(oTime);
                }
                // 重新開啟定時器
                oTime = setInterval(function () {
                    // 第次增量
                    iNum += 2 * toggle;
                    // UL向下走,當top值大於0時
                    if (iNum > 0) {
                        // 設定top值為負的UL高度的一半(向上拉)
                        iNum = -oLi.length * oLi[0].offsetHeight / 2;
                    }
                    // UL向上走,當top值的絕對值大於UL自身寬度的一半時
                    if (Math.abs(iNum) > oLi.length * oLi[0].offsetHeight / 2) {
                        // 設定top的值為0(向下拉)
                        iNum = 0;
                    }
                    // 賦值給top值
                    oUl.style.top = iNum + 'px';
 
                }, iMs);
            };
 
            autoPlay(toggle);
 
            // 獲取擁有當前樣式的元素
            function getClass(sName) {
                for (i = 0; i < aDiv.length; i++) {
                    if (aDiv[i].className == sName) {
                        return aDiv[i];
                    }
                }
            }
        }
 
    </script>
</head>
 
<body>
 
    <div class="slide-module" id="miaovSlide">
        <!-- 向上箭頭 -->
        <div class="up"></div>
        <div class="wrap">
            <ul>
                <li>
                    <a>
                        <img src="images/1.jpg" />
                    </a>
                </li>
                <li>
                    <a>
                        <img src="images/2.jpg" />
                    </a>
                </li>
                <li>
                    <a>
                        <img src="images/3.jpg" />
                    </a>
                </li>
                <li>
                    <a>
                        <img src="images/4.jpg" />
                    </a>
                </li>
            </ul>
        </div>
        <!-- 向下箭頭 -->
        <div class="down"></div>
    </div>
 
</body>
 
</html>

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

推薦閱讀: