面試題(12)之手撕輪播圖

一、移動動畫效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #demo {
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute; /*必定要記得加定位*/
        }
    </style>
</head>
<body>
<button id="btn">奔跑吧盒子</button>
<div id="demo"></div>
<script>
    var timer = null;
    var btn = document.getElementById("btn");
    var demo = document.getElementById("demo");
    // 點擊按鈕 讓盒子跑
    btn.onclick = function () {
        clearInterval(timer);//防止重複設定定時器
        timer = setInterval(function () {
            var target = 400; // 目標值
            var leader = demo.offsetLeft; // 獲取當前位置
            var step = 10;
            if (leader < target) {
                leader = leader + step;
                demo.style.left = leader + "px";
            } else {
                clearInterval(timer);
            }
        }, 15);
    };
</script>
</body>
</html>

二、動畫封裝

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #demo {
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute; /*必定要記得加定位*/
        }
    </style>
</head>
<body>
<button id="btn1">奔跑到200吧盒子</button>
<button id="btn2">奔跑到400吧盒子</button>
<div id="demo"></div>
<script>
    var timer = null;
    var btn1 = document.getElementById("btn1");
    var btn2 = document.getElementById("btn2");
    var demo = document.getElementById("demo");
    //點擊按鈕 讓盒子跑
    btn1.onclick = function () {
        animate(demo, 200);
    };
    btn2.onclick = function () {
        animate(demo, 400);
    };

    //需求 可以讓任意對象移動到指定位置
    function animate(obj, target) {
        clearInterval(obj.timer);
        obj.timer = setInterval(function () {
            var leader = obj.offsetLeft;
            var step = 10;
            if (leader < target) {
                leader = leader + step;
                obj.style.left = leader + "px";
            } else {
                clearInterval(obj.timer);
            }
        }, 15);
    }
</script>
</body>
</html>

三、動畫改進

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #demo {
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute; /*必定要記得加定位*/
        }
    </style>
</head>
<body>
<button id="btn1">奔跑到200吧盒子</button>
<button id="btn2">奔跑到400吧盒子</button>
<div id="demo"></div>
<script>
    var timer = null;
    var btn1 = document.getElementById("btn1");
    var btn2 = document.getElementById("btn2");
    var demo = document.getElementById("demo");
    //點擊按鈕 讓盒子跑
    btn1.onclick = function () {
        animate(demo, 200);
    };
    btn2.onclick = function () {
        animate(demo, 400);
    };

    //需求 可以讓任意對象移動到指定位置
    function animate(obj, target) {
        clearInterval(obj.timer);
        obj.timer = setInterval(function () {
            var leader = obj.offsetLeft;
            var step = 10;
            step = leader < target ? step : -step; // step有了正負
            if (Math.abs(leader - target) >= Math.abs(step)) {
                leader = leader + step;
                obj.style.left = leader + "px";
            } else {
                obj.style.left = target + "px"; // 手動放到終點
                clearInterval(obj.timer);
            }
        }, 15);
    }
</script>
</body>
</html>

四、簡單輪播圖

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0
        }

        ul {
            list-style: none
        }

        img {
            vertical-align: top
        }

        .box {
            width: 490px;
            height: 170px;
            margin: 100px auto;
            padding: 5px;
            border: 1px solid #ccc;
            overflow: hidden;
        }

        .inner {
            width: 490px;
            height: 170px;
            background-color: pink;
            /*overflow: hidden;*/
            position: relative;
        }

        .inner ul {
            width: 1000%;
            position: absolute;
            top: 0;
            left: 0;
        }

        .inner li {
            float: left;
        }

        .square {
            position: absolute;
            right: 10px;
            bottom: 10px;
        }

        .square span {
            display: inline-block;
            width: 16px;
            height: 16px;
            background-color: #fff;
            text-align: center;
            line-height: 16px;
            cursor: pointer;
        }

        .square span.current {
            background-color: orangered;
            color: #fff;
        }

    </style>
</head>
<body>
<div class="box" id="box">
    <div class="inner">
        <ul>
            <li><a href="#"><img src="images/01.jpg" alt=""/></a></li>
            <li><a href="#"><img src="images/02.jpg" alt=""/></a></li>
            <li><a href="#"><img src="images/03.jpg" alt=""/></a></li>
            <li><a href="#"><img src="images/04.jpg" alt=""/></a></li>
            <li><a href="#"><img src="images/05.jpg" alt=""/></a></li>
        </ul>
        <div class="square">
            <span class="current">1</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
        </div>
    </div>
</div>
<script>
    //需求:鼠標通過按鈕 按鈕排他 還要把ul以動畫的方式移動到指定位置
    //1.鼠標通過按鈕 按鈕排他
    var box = document.getElementById("box");
    var inner = box.children[0];
    var ul = inner.children[0];
    var squareList = inner.children[1];
    var squares = squareList.children;//全部按鈕
    var imgWidth = inner.offsetWidth;//圖片寬度
    //alert(imgWidth);
    //給每個按鈕註冊鼠標通過事件
    for (var i = 0; i < squares.length; i++) {
        squares[i].index = i;//把索引保存在自定義屬性中
        squares[i].onmouseover = function () {
            for (var j = 0; j < squares.length; j++) {
                squares[j].className = "";
            }
            //留下我本身
            this.className = "current";
            //目標 和 當前按鈕索引有關 和 圖片寬度有關 並且是負數
            var target = -this.index * imgWidth;
            animate(ul, target);

        };
    }

    function animate(obj, target) {
        clearInterval(obj.timer);
        obj.timer = setInterval(function () {
            var leader = obj.offsetLeft;
            var step = 30;
            step = leader < target ? step : -step;//step有了正負
            if (Math.abs(leader - target) >= Math.abs(step)) {
                leader = leader + step;
                obj.style.left = leader + "px";
            } else {
                obj.style.left = target + "px";//手動放到終點
                clearInterval(obj.timer);
            }
        }, 15);
    }
</script>
</body>
</html>

五、輪播圖左右焦點圖

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        body, ul, ol, li, img {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        #box {
            width: 490px;
            height: 270px;
            padding: 5px;
            position: relative;
            border: 1px solid #ccc;
            margin: 100px auto 0;
        }

        .ad {
            width: 490px;
            height: 270px;
            /*overflow: hidden;*/
            position: relative;
        }

        #box img {
            width: 490px;
        }

        .ad ol {
            position: absolute;
            right: 10px;
            bottom: 10px;
        }

        .ad ol li {
            width: 20px;
            height: 20px;
            line-height: 20px;
            border: 1px solid #ccc;
            text-align: center;
            background: #fff;
            float: left;
            margin-right: 10px;
            cursor: pointer;
            _display: inline;
        }

        .ad ol li.current {
            background: yellow;
        }

        .ad ul li {
            float: left;
        }

        .ad ul {
            position: absolute;
            top: 0;
            width: 2940px;
        }

        .ad ul li.current {
            display: block;
        }

        #arr {
            display: none;
        }

        #arr span {
            width: 40px;
            height: 40px;
            position: absolute;
            left: 5px;
            top: 50%;
            margin-top: -20px;
            background: #000;
            cursor: pointer;
            line-height: 40px;
            text-align: center;
            font-weight: bold;
            font-family: '黑體';
            font-size: 30px;
            color: #fff;
            opacity: 0.3;
            border: 1px solid #fff;
        }

        #arr #right {
            right: 5px;
            left: auto;
        }
    </style>
</head>
<body>
<div id="box" class="all">
    <div class="ad">
        <ul id="imgList">
            <li><img src="images/1.jpg"/></li>
            <li><img src="images/2.jpg"/></li>
            <li><img src="images/3.jpg"/></li>
            <li><img src="images/4.jpg"/></li>
            <li><img src="images/5.jpg"/></li>
        </ul>
    </div>
    <div id="arr"><span id="left">&lt;</span><span id="right">&gt;</span></div>
</div>
<script>
    //需求 點擊左右箭頭 以動畫效果移動ul到指定位置
    //找人
    var box = document.getElementById("box");
    var ad = box.children[0];
    var ul = document.getElementById("imgList");
    var lis = ul.children;
    var arr = document.getElementById("arr");
    var left = document.getElementById("left");
    var right = document.getElementById("right");
    var imgWidth = ad.offsetWidth;//圖片寬度
    //alert(imgWidth);
    //1.鼠標通過盒子 顯示arr 鼠標離開後隱藏
    box.onmouseover = function () {
        arr.style.display = "block";
    };
    box.onmouseout = function () {
        arr.style.display = "none";
    };
    //2.點擊右箭頭 以動畫的形式 把ul移動到指定的位置
    var pic = 0;//記錄當前顯示的圖片的索引
    right.onclick = function () {
        //先判斷 而後再去執行 移動ul的代碼
        if (pic === lis.length - 1) {//若是是最後一個圖片的索引 就不能再執行了
            return;
        }
        pic++;//計算出接下來要顯示的圖片的索引
        //目標 和pic有關 和 圖片寬度有關 並且是負數
        var target = -pic * imgWidth;
        animate(ul, target);
    };
    left.onclick = function () {
        //先判斷 而後再去執行 移動ul的代碼
        if (pic === 0) {//若是是第一個圖片的索引 就不能再執行了
            return;
        }
        pic--;//計算出接下來要顯示的圖片的索引
        //目標 和pic有關 和 圖片寬度有關 並且是負數
        var target = -pic * imgWidth;
        animate(ul, target);
    };

    function animate(obj, target) {
        clearInterval(obj.timer);
        obj.timer = setInterval(function () {
            var leader = obj.offsetLeft;
            var step = 30;
            step = leader < target ? step : -step;//step有了正負
            if (Math.abs(leader - target) >= Math.abs(step)) {
                leader = leader + step;
                obj.style.left = leader + "px";
            } else {
                obj.style.left = target + "px";//手動放到終點
                clearInterval(obj.timer);
            }
        }, 15);
    }
</script>
</body>
</html>

六、完整輪播圖

CSShtml

        * {
            padding: 0;
            margin: 0;
            list-style: none;
            border: 0;
        }

        .all {
            width: 500px;
            height: 200px;
            padding: 7px;
            border: 1px solid #ccc;
            margin: 100px auto;
            position: relative;
        }

        .screen {
            width: 500px;
            height: 200px;
            overflow: hidden;
            position: relative;
        }

        .screen li {
            width: 500px;
            height: 200px;
            overflow: hidden;
            float: left;
        }

        .screen ul {
            position: absolute;
            left: 0;
            top: 0px;
            width: 3000px;
        }

        .all ol {
            position: absolute;
            right: 10px;
            bottom: 10px;
            line-height: 20px;
            text-align: center;
        }

        .all ol li {
            float: left;
            width: 20px;
            height: 20px;
            background: #fff;
            border: 1px solid #ccc;
            margin-left: 10px;
            cursor: pointer;
        }

        .all ol li.current {
            background: yellow;
        }

        #arr {
            display: none;
        }

        #arr span {
            width: 40px;
            height: 40px;
            position: absolute;
            left: 5px;
            top: 50%;
            margin-top: -20px;
            background: #000;
            cursor: pointer;
            line-height: 40px;
            text-align: center;
            font-weight: bold;
            font-family: '黑體';
            font-size: 30px;
            color: #fff;
            opacity: 0.3;
            border: 1px solid #fff;
        }

        #arr #right {
            right: 5px;
            left: auto;
        }

HTML:app

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>

</head>
<body>
<div class="all" id='box'>
    <div class="screen">
        <ul>
            <li><img src="images/1.jpg" width="500" height="200"/></li>
            <li><img src="images/2.jpg" width="500" height="200"/></li>
            <li><img src="images/3.jpg" width="500" height="200"/></li>
            <li><img src="images/4.jpg" width="500" height="200"/></li>
            <li><img src="images/5.jpg" width="500" height="200"/></li>
        </ul>
        <ol></ol>
    </div>
    <div id="arr"><span id="left">&lt;</span><span id="right">&gt;</span></div>
</div>
<script>
    var timer = null;
    //需求 實現一個 完整的輪播圖
    //找人
    var box = document.getElementById("box");
    var screen = box.children[0];
    var ul = screen.children[0];
    var ol = screen.children[1];
    var ulLis = ul.children;//全部的廣告
    var imgWidth = screen.offsetWidth;
    //alert(imgWidth);
    //箭頭
    var arr = document.getElementById("arr");
    var left = document.getElementById("left");
    var right = document.getElementById("right");
    //1.動態生成結構
    //1.1根據圖片數量動態生成按鈕
    for (var i = 0; i < ulLis.length; i++) {
        var li = document.createElement("li");
        li.innerHTML = i + 1;
        ol.appendChild(li);
    }
    var olLis = ol.children;//全部的按鈕
    olLis[0].className = "current";
    //1.2克隆第一張廣告 放到最後
    var firstImg = ulLis[0].cloneNode(true);
    ul.appendChild(firstImg);
    //2.鼠標通過按鈕
    //鼠標通過按鈕 按鈕排他 以動畫的形式把ul移動到指定的位置
    for (var j = 0; j < olLis.length; j++) {
        olLis[j].index = j;
        olLis[j].onmouseover = function () {
            //排他
            //幹掉全部人
            for (var k = 0; k < olLis.length; k++) {
                olLis[k].className = "";
            }
            //留下我本身
            this.className = "current";
            //以動畫的形式把ul移動到指定的位置
            //目標 和 按鈕索引有關 和 圖片寬度有關 並且是負數
            var target = -this.index * imgWidth;
            animate(ul, target);
            //還要把幾個索引統一
            pic = this.index;
            square = this.index;
        };
    }
    //3.鼠標點擊箭頭
    //3.1鼠標通過盒子 顯示箭頭 鼠標離開盒子 隱藏箭頭
    box.onmouseover = function () {
        arr.style.display = "block";//顯示箭頭
        clearInterval(timer);//中止自動播放
    };
    box.onmouseout = function () {
        arr.style.display = "none";//隱藏箭頭
        timer = setInterval(right.onclick, 1000);//重新自動播放
    };
    //3.2點擊右箭頭 以動畫的形式把ul移動到指定的位置
    var pic = 0;//記錄當前正在顯示的圖片的索引
    var square = 0;//記錄當前正在亮起的按鈕的索引
    right.onclick = function () {
        //先判斷 若是是最後一個圖片 先讓ul瞬間跳會開始位置 而後索引也要歸零
        if (pic === ulLis.length - 1) {
            ul.style.left = 0 + "px";
            pic = 0;//索引也要歸零
        }
        pic++;//計算出將要顯示的圖片的索引
        //目標 和pic有關 和 圖片寬度有關 並且是負數
        var target = -pic * imgWidth;
        animate(ul, target);
        //按鈕也要跟着走
        if (square < olLis.length - 1) {
            square++;
        } else {
            square = 0;
        }
        //幹掉全部人
        for (var i = 0; i < olLis.length; i++) {
            olLis[i].className = "";
        }
        //留下對應的
        olLis[square].className = "current";
    };
    left.onclick = function () {
        //先判斷 若是是第一個圖片 先讓ul瞬間跳到最後的位置 而後索引也要到最後
        if (pic === 0) {
            ul.style.left = -(ulLis.length - 1) * imgWidth + "px";
            pic = ulLis.length - 1;//索引也要歸零
        }
        pic--;//計算出將要顯示的圖片的索引
        //目標 和pic有關 和 圖片寬度有關 並且是負數
        var target = -pic * imgWidth;
        animate(ul, target);
        //按鈕也要跟着走
        if (square > 0) {
            square--;
        } else {
            square = olLis.length - 1;
        }
        //幹掉全部人
        for (var i = 0; i < olLis.length; i++) {
            olLis[i].className = "";
        }
        //留下對應的
        olLis[square].className = "current";
    };

    timer = setInterval(right.onclick, 1000);//自動播放

    function animate(obj, target) {
        clearInterval(obj.timer);
        obj.timer = setInterval(function () {
            var leader = obj.offsetLeft;
            var step = 30;
            step = leader < target ? step : -step;//step有了正負
            if (Math.abs(leader - target) >= Math.abs(step)) {
                leader = leader + step;
                obj.style.left = leader + "px";
            } else {
                obj.style.left = target + "px";//手動放到終點
                clearInterval(obj.timer);
            }
        }, 15);
    }
</script>
</body>
</html>
相關文章
相關標籤/搜索