自適應(yīng)淡入淡出輪播

前端入坑紀(jì) 19

最近這段時間一直在做公司自己的項目,忙的不可開交。而且手頭上那次給同學(xué)定制的網(wǎng)站也被要求 “回爐再造” ,整個人都要不好不好了~

人生的處境可真是夠激蕩起伏的啊?。。?/p>

那么,老規(guī)矩,先上截圖

效果圖

OK,first things first!項目鏈接

HTML 結(jié)構(gòu)
<div class="swipWrp">
        <a id="arwL" href="javascript:;" class="arwL"></a>
        <div id="swiper">
            ![壁紙](http://upload-images.jianshu.io/upload_images/4732938-a08f44fb5106cf00.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            ![壁紙](http://upload-images.jianshu.io/upload_images/4732938-c9d2e9e5d834ac50.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            ![壁紙](http://upload-images.jianshu.io/upload_images/4732938-032e0972240840bc.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            ![壁紙](http://upload-images.jianshu.io/upload_images/4732938-ac690325c464815b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            ![壁紙](http://upload-images.jianshu.io/upload_images/4732938-aa78d263a7c0bbe9.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
        </div>
        <a id="arwR" href="javascript:;" class="arwR"></a>
    </div>

輪播里的標(biāo)示圖片位置的圓點是用JS動態(tài)生成的,數(shù)量等于圖片的數(shù)量

CSS 結(jié)構(gòu)
        body {
            font-family: "Microsoft YaHei", Verdana, Geneva, Tahoma, sans-serif;
        }
        
        html,
        body,
        ul,
        li {
            margin: 0;
            padding: 0
        }
        
        .clearfix::after {
            content: "";
            display: table;
            clear: both
        }
        
         ::-webkit-scrollbar {
            display: none
        }
        
        ul,
        li {
            list-style: none
        }
        
        .swipWrp {
            position: relative;
            width: 100%;
            overflow: hidden;
        }
        
        #swiper {
            position: relative;
            padding: 28% 50%;
            overflow: hidden;
        }
        
        #swiper img {
            display: none;
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
            width: 100%;
            transition: all 300ms linear 60ms;
        }
        
        .arwL,
        .arwR {
            background-color: rgba(255, 255, 255, .5);
            border-radius: 50%;
            display: block;
            position: absolute;
            padding: 3%;
            background-size: 50% auto;
            background-position: center center;
            background-repeat: no-repeat;
            top: 50%;
            transform: translateY(-50%);
            z-index: 9
        }
        
        .arwL {
            left: 0;
            background-image: url("http://sandbox.runjs.cn/uploads/rs/113/ajzbmzhg/arrowL.png")
        }
        
        .arwR {
            right: 0;
            background-image: url("http://sandbox.runjs.cn/uploads/rs/113/ajzbmzhg/arrowR.png")
        }
        
        #tags {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            text-align: center;
        }
        
        #tags a {
            display: inline-block;
            width: 16px;
            height: 16px;
            margin: 3px 6px;
            border-radius: 8px;
            background-color: rgba(0, 0, 0, .5)
        }
        
        #tags a.on {
            background-color: rgba(255, 255, 255, .5)
        }

這里輪播自適應(yīng)的樣式,和以前發(fā)的輪播是差不多的原理,就不贅述了。

JS 結(jié)構(gòu)
        var swiper = document.getElementById("swiper");
        var imgs = swiper.getElementsByTagName("img");
        var arwL = document.getElementById("arwL");
        var arwR = document.getElementById("arwR");
        var indx = 0;
        var timers = null;
        var nodeFgmt = document.createDocumentFragment();

// 這里是動態(tài)添加位置原點的js代碼
        newNode = document.createElement("div");
        newNode.id = "tags";

        for (var i = 0; i < imgs.length; i++) {
            var ond = document.createElement("a");
            ond.href = "javascript:;";
            ond.setAttribute("data-indx", i);
            nodeFgmt.appendChild(ond);
        }
        newNode.appendChild(nodeFgmt);

        swiper.appendChild(newNode);
        var tagsA = document.getElementById("tags").getElementsByTagName("a");
// 鼠標(biāo)滑過原點時顯示對應(yīng)的輪播圖片
        for (var s = 0; s < tagsA.length; s++) {
            tagsA[s].onmouseover = function() {
                offEft();
                hideAll();
                var indxA = this.getAttribute("data-indx");
                changeEffect(indxA);
                this.className = "on"

            }
            tagsA[s].onmouseout = function() {
                onEft();
            }
        }
// 這里是初始化輪播的3條代碼
        tagsA[0].className = "on";
        imgs[0].style.display = "block";
        imgs[0].style.opacity = "1";

// 隱藏所有的輪播圖
        function hideAll() {
            for (var i = 0; i < imgs.length; i++) {
                imgs[i].style.display = "none";
                imgs[i].style.opacity = "0";
                tagsA[i].className = ""
            }
        }

// 每隔3.6秒切換輪播圖
        function scrollIntvel() {
            timers = setInterval(function() {
                hideAll();
                if (indx < imgs.length - 1) {
                    indx++;
                } else {
                    indx = 0;
                }
                changeEffect(indx);
            }, 3600);
        }


// 切輪播圖的函數(shù),傳入對應(yīng)的輪播圖序號
        function changeEffect(indx) {
            imgs[indx].style.display = "block";
          //   console.log("imgs的indx ", indx)
            setTimeout(function() {
                imgs[indx].style.opacity = "1";
                tagsA[indx].className = "on";
             //    console.log("times的indx ", indx)
            }, 30);
        }

// 鼠標(biāo)滑過時的切換動作函數(shù)
        function onEft() {
            scrollIntvel()
          //   console.log("scrolling now  ")
        }

// 鼠標(biāo)滑出時的切換動作函數(shù)
        function offEft() {
            clearInterval(timers);
        //     console.log("stopping now  ")
        }


// 啟動輪播效果
        scrollIntvel()

// 鼠標(biāo)滑入滑出的函數(shù)調(diào)用
        arwL.onmouseover = offEft;
        arwR.onmouseover = offEft;
        arwL.onmouseout = onEft;
        arwR.onmouseout = onEft;


// 左箭頭點擊效果
        arwL.onclick = function() {
            hideAll();
            if (indx > 0) {
                indx--;
            } else {
                indx = imgs.length - 1;
            }
            changeEffect(indx);
        }
// 右箭頭點擊效果
        arwR.onclick = function() {
            hideAll();
            if (indx < imgs.length - 1) {
                indx++;
            } else {
                indx = 0;
            }
            changeEffect(indx);
        }

這會的JS有點多,本來想寫的簡單點。可是碼著碼著就變成現(xiàn)在這個樣子了。原理和先前文章的也是差不多,有興趣的小伙伴可以好好研究一番。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前端入坑紀(jì) 28 確切的說,這個算是偽更新了。只是改了幾行代碼,原諒這拙劣的套路。算是為了讓更多人看到,其實很多j...
    kerush閱讀 399評論 0 0
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,057評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,346評論 4 61
  • [不可否認(rèn),安靜下來讀書的藺少閣主很養(yǎng)眼。 但好似和蘇兄斗嘴和飛流玩鬧的時候更養(yǎng)眼。(劃掉)] 文/覃浠 他的出場...
    覃浠閱讀 1,718評論 4 18
  • 文|佳鵬 01 身邊的行人已經(jīng)湊夠了一波,一位“勇者”邁出了堅定的步伐,行人如開閘的洪水般涌向了馬路對面,而我卻像...
    職場飛叨閱讀 378評論 0 1

友情鏈接更多精彩內(nèi)容