原生js實現網易輪播圖

    前端基本功-網頁特效5

一、實現效果圖
這裡寫圖片描述
二、分析佈局
主盒子裡分上下兩個小盒子(1和2)。
包含圖片的盒子占兩張圖片的寬(3),處於上盒子中,當前圖片在上盒子(1)中,其它圖片在盒子(3)的右側等待播放。
下邊的盒子(2)包括瞭六個小方塊及定位在主盒子上的兩個箭頭。
三、html部分

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>wangyi</title>
    <link type="text/css" rel="stylesheet" href="css/guide.css"/>
    <script type="text/javascript" src="../animate.js"></script>
    <script type="text/javascript" src="js/main.js"></script>
</head>
<body>
    <p class="w-slider">
        <p class="slider">
            <p class="slider-main" id="slider_main_block">
                <p class="slider-main-img"><a href="#"><img src="images/1.jpg"/></a></p>
                <p class="slider-main-img"><a href="#"><img src="images/2.jpg"/></a></p>
                <p class="slider-main-img"><a href="#"><img src="images/3.jpg"/></a></p>
                <p class="slider-main-img"><a href="#"><img src="images/4.jpg"/></a></p>
                <p class="slider-main-img"><a href="#"><img src="images/5.jpg"/></a></p>
                <p class="slider-main-img"><a href="#"><img src="images/6.jpg"/></a></p>
 
            </p>
        </p>
        <p class="slider-ctrl" id="slider_ctrl">
            <span class="slider-ctrl-pre"></span>
            <span class="slider-ctrl-next"></span>
        </p>
    </p>
</body>
</html>

四、CSS部分

* {
    margin: 0;
    padding: 0;
}
.w-slider {
    width:310px;
    height:265px;
    margin:100px auto;
    overflow: hidden;
    position: relative;
}
.slider {
    width:310px;
    height:220px;

}
.slider-main {
    width:620px;
    height:220px;
}
.slider-main-img{
    width:310px;
    height:220px;
    position:absolute;
    top:0;
    left:0;
}
.slider-main-img img{
    vertical-align: top;
}
.slider-ctrl {
    width:310px;
    height:45px;
    text-align:center;
}
.slider-ctrl span {
    cursor: pointer;
}
.slider-ctrl-con {
    display:inline-block;
    width:24px;
    height:20px;
    margin-right:8px;
    text-indent: -20em;
    overflow:hidden;
    background:url(../images/icon.png)-24px -782px no-repeat;
}
.slider-ctrl .current {
    background:url(../images/icon.png)-24px -762px no-repeat;
}
.slider-ctrl-pre,
.slider-ctrl-next {
    width:27px;
    height:38px;
    position:absolute;
    top:50%;
    margin-top:-36px;
    opacity:0.7;
    filter:alpha(opacity=50);
}
.slider-ctrl-pre {
    background:url(../images/icon.png)6px 1px no-repeat;
    left:0;
}
.slider-ctrl-next {
    background:url(../images/icon.png)-9px -44px no-repeat;
    right:0;
}

五、JS部

/**
 * Created by 15623544233 on 2016/10/17.
 */
window.onload=function () {
    //imgs的DOM對象
    var slider_main_block =$("slider_main_block");
    var sliderImgs =slider_main_block.children;
    //最外層的slider大盒子
    var sider_box =slider_main_block.parentNode.parentNode;
    //slider-ctrl的DOM對象
    var slider_ctrl = $("slider_ctrl");
    var sliderCtrls =slider_ctrl.children;
    //動態添加Ctrl
    for(var i=0;icurrentId){
                    //像點擊右側按鈕一樣播放
                    animate(sliderImgs[currentId],{left:-imgW});
                    sliderImgs[that].style.left=imgW+"px";

                }else if(that5,第0張回到框中
        ++currentId>sliderImgs.length-1?currentId = 0:currentId;
        //上一張圖片左移後,下一張圖片快速到右邊
        sliderImgs[currentId].style.left=imgW+"px";
        //下張右側圖片左移
        animate(sliderImgs[currentId],{left:0});
    }
    sider_box.onmouseover =function () {
        clearInterval(timer);
    }
    sider_box.onmouseout=function () {
        timer =setInterval(autoPlay,2000);
    }

};
function $(id) {
    return document.getElementById(id);
}
 /*當前樣式屬性值的獲取 */
function curStyle(obj,attr){
    if(obj.currentStyle){
//IE瀏覽器  
    return obj.currentStyle[attr];
    }else{
  //標準瀏覽器    
  return window.getComputedStyle(obj,null)[attr];
    }
}
 /*封裝的運動框架*/
/*obj ----做動畫的DOM對象 ;json----變化的部分是json數據 */                      
function animate(obj,json) {
//調用時先清定時器            
    clearInterval(obj.timer);
//定時器為obj的內部定時器,不用每次調用都創建一個新的定時器               
    obj.timer = setInterval(function () {
//遍歷json數據,每次遍歷的標志位為flag              
        var flag=true;
//遍歷json數據,eg:{left:20,top:40,opacity:50,z-index:3}                
        for(var key in json){
            //取得盒子運動當前的位置
            var current= 0;
            if(key=="opacity"){
                //Ie6,7,8沒有設置透明度,默認為undefined
                current =Math.round(parseInt(curStyle(obj,key)*100))||1;
                //console.log(current);
            }else{
                current= parseInt(curStyle(obj,key));
            }

//運動步長:(目標位置-當前位置)/10
            var step = (json[key]-current)/10;
            step = step>0?Math.ceil(step):Math.floor(step);
//各屬性值的漸變動畫         
             if(key=="opacity"){
                if("opacity" in obj.style){
                    obj.style.opacity = (current+step)/100;
                }else{
                    //兼容ie6,7,8
                    obj.style.filter ="alpha(opacity ="+(current+step)*10+")";
                }
            }else if(key=="zIndex"){
                obj.style[key] =json[key];
            }else
            {
                obj.style[key] = current+step+"px";
            }

//遍歷每個屬性時都判斷標志位
            if(current!=json[key]){
                flag =false;
            }
        }遍歷結束後,標志位都為true,判斷起所有動畫執行完畢,清除定時器
        if(flag){
            clearInterval(obj.timer);
        }
    },20)

}

發佈留言