1. 程式人生 > >全屏banner及全屏輪播

全屏banner及全屏輪播

鼠標移出 banner blog pan ont float true nts data-

一、全屏banner

1、設置網頁圖片全屏banner

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .pic {
            width: 100%;
            height: 600px;
            background: url("...") no-repeat center;
        }
    </style>
</head>
<body>
    <div class="banner">
        <div class="pic"></div>
    </div>
</body>
</html>

  二、全屏輪播

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>全屏輪播模板</title>
    <style>
        html,body,head,span,button,div,title {
            margin: 0;
            padding: 0;
        }
        .cont {
            position: relative;
            height: 600px;
        }
        .list{
            position: relative;
        }
        .list div {
            width: 100%;
            height: 600px;
            position: absolute;
            opacity: 0;
            transition: 1s;  /*設置切換時間*/
        }
        .pic1 {
            background: url(img/lbt01.jpg) no-repeat center red;
        }
        .pic2 {
            background: url(img/lbt02.jpg) no-repeat center blue;
        }
        .pic3 {
            background: url(img/lbt03.jpg) no-repeat center yellow;
        }
        .pic4 {
            background: url(img/lbt04.jpg) no-repeat center green;
        }
        .pic5 {
            background: url(img/lbt05.jpg) no-repeat center pink;
        }
        .btns {
            position: absolute;
            z-index: 6;
            left: 50%;
            bottom: 80px;
            margin-left: -170px;
        }
        .btns span { 
            float: left;
            width: 60px;
            height: 5px;
            margin-right: 10px;
            background-color: rgba(255,255,255,0.3);
            border-radius: 5px;
        } 
        .buttons {
            width: 1180px;
            height: 600px;
            margin: 0 auto;
            position: relative;
        }
        .btn {  /*左右切換按鈕樣式*/
            position: absolute;
            top: 50%;
            margin-top: -26px;
            width: 32px;
            height: 52px;
            font-size: 30px;
            border-radius: 5px;
            z-index: 6;
            opacity: 0.5;
        }
        .btn-prev {
            left: 0;
        }
        .btn-next {
            right: 0;
        }
        .bgc {
            background-color: white !important;
        }
    </style>
</head>
<body>
    <div class="cont">
        <div class="list">
            <div class="pic1" data-index=0 style="opacity: 1; z-index: 2;"></div>
            <div class="pic2" data-index=1></div>
            <div class="pic3" data-index=2></div>
            <div class="pic4" data-index=3></div>
            <div class="pic5" data-index=4></div>
        </div>
        <div class="btns">
            <span class="bgc"></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
        </div>
        <div class="buttons">
            <button class="btn btn-prev"> < </button>
            <button class="btn btn-next"> > </button>
        </div>
    </div>

    <script>
        var btn = document.getElementsByClassName("btn");
        var imgs = document.querySelectorAll(".list div");
        var btns = document.querySelectorAll(".btns span");
        var cont = document.getElementsByClassName("cont")[0];
        var indexes, timer = null;
        for(let i=0; i<btns.length; i++){
            btns[i].onmouseover = function(){  // 給所有下面的白色線條添加一個鼠標經過事件
                animate(i);
            }
        }

        btn[0].onclick = function(){  // 上一張按鈕
            indexes = currentPage();
            if(indexes == 0) indexes=5;
                indexes--;
            animate(indexes);
        }

        btn[1].onclick = next;

        function next(){  // 下一張按鈕
            indexes = currentPage();
            if(indexes == 4) indexes = -1;
                indexes++;
           animate(indexes);
        }

        function animate(i){  // 動畫函數
            for(let j = 0; j<imgs.length; j++){
                imgs[j].style.opacity = 0;
                imgs[j].style.zIndex = 1;
                btns[j].classList.remove("bgc");
            }
            imgs[i].style.opacity = 1;
            imgs[i].style.zIndex = 2;
            btns[i].classList.add("bgc");
        }

        function currentPage() { // 返回當前頁面
            for(var i = 0; i<imgs.length; i++){
                 if(imgs[i].style.zIndex == 2){
                    return imgs[i].dataset.index;
                 }
            }
        }

        timer = setInterval(next,3000); // 設置自動播放
        cont.onmousemove = function(){  // 鼠標移入時停止自動播放
            clearInterval(timer);
        }
        cont.onmouseout = function(){   // 鼠標移出時又開始播放
            timer = setInterval(next,3000);
        }
    </script>
</body>
</html>

  

全屏banner及全屏輪播