前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >轮播(未完待续)

轮播(未完待续)

作者头像
天天_哥
发布2018-09-29 14:57:53
3230
发布2018-09-29 14:57:53
举报
文章被收录于专栏:天天天天

1.结构

eg:
 <body>
    <div class="container">
        <div id="list" style="left: -400px">
            ![](images/05.png)
            ![](images/01.png)
            ![](images/02.png)
            ![](images/03.png)
            ![](images/04.png)
            ![](images/05.png)
            ![](images/01.png)
        </div>
        <span id="prew"></span>
        <span id="next"></span>
        <ul id ="index"><li class="current"></li><li></li><li></li><li></li><li></li></ul>
    </div>
</body>

2.样式

<style>
        *{margin: 0;padding: 0}
        ul{
            list-style: none;
        }
        .container{
            width: 400px;
            height: 600px;
            border: 1px solid #ccc;
            position: relative;
            margin: 100px auto;
            overflow: hidden;
        }
        #list{
            width: 2800px;
            height: 600px;
            position: absolute;
        }
        #list img{
            float: left;
        }
        #index{
            position: absolute;
            text-align: center;
            bottom: 20px;
            left: 50%;
           transform: translateX(-50%);
        }
        #index li{
            display: inline-block;
            width: 12px;
            height: 12px;
            background: lightblue;
            border-radius: 50%;
            margin-right: 10px;
        }
        #index .current{
            background: hotpink;
        }
        .container>span{
            position: absolute;
            width: 40px;
            height: 70px;
            top: 50%;
            background: url("images/icon-slides.png") no-repeat 0 center;
        }
        .container #next{
            right: 0;
            background-position-x:-42px ;
        }
    </style>

3.行为

<script>
    var list = document.getElementById("list");
    var prev = document.getElementById("prew");
    var next = document.getElementById("next");
    var litages = document.getElementById("index").children;
    var Width = list.children[0].offsetWidth;
    var timer;
    var current_index = 0;
    var animated = false;
    var oldLeft = parseFloat(list.style.left);
    //点击运动封装
    function animate(offset){
        animated = true;
        var newLeft = parseFloat(list.style.left)+offset;
        //封装每一次运动的函数
        function gosteps(){
            var time_leght =300;//运动总时间
            var steps = 10;//运动的步数
            var everylength =offset/(300/10);//每一步走的长度;
            if((everylength<0 && parseFloat(list.style.left)>newLeft) || (everylength>0 && parseFloat(list.style.left)<newLeft)){
                list.style.left = parseFloat(list.style.left)+everylength+"px";
                setTimeout(gosteps,steps)
            }else{
                animated = false;
                list.style.left = newLeft+"px";
                if(newLeft<-2000){
                    list.style.left = -400+"px";
                }
                if(newLeft>-400){
                    list.style.left = -2000+"px";
                }
            }
        }
            gosteps();

    }
    //向前向后点击事件
    next.onclick = function(){
        current_index++;
        if(current_index>4){
            current_index = 0;
        }
        for(var i=0;i<litages.length;i++){
            litages[i].className="";
        }
        litages[current_index].className = "current";
        if(!animated){
            animate(-400);
        }
    };
    prev.onclick = function(){
        current_index--;
        if(current_index<0){
            current_index = 4;
        }
        for(var i=0;i<litages.length;i++){
            litages[i].className="";
        }
        litages[current_index].className = "current";
        if(!animated){
            animate(400);
        }
    };
    //    圆点点击事件
    for(var j=0;j<litages.length;j++){
        litages[j].index = j;
        litages[j].onclick = function(){
            current_index =this.index;
            for(var k=0;k<litages.length;k++){
                litages[k].className = "";
            }
            this.className= "current";
            if(!animated){
                list.style.left = -Width*(current_index+1)+"px";
            }
        }
    }
    //自动播放
    function play(){
        timer = setInterval(function(){
            next.onclick();
        },3000)
    }
    function stop(){
        clearInterval(timer);
    }
    list.onmouseover =stop;
    list.onmouseout = play;
    play();
</script>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.10.16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档