继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

JS,jQuery手写图片轮播效果练习

resharpe
关注TA
已关注
手记 102
粉丝 7244
获赞 3476

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图片轮播</title>
    <style>
        body,div,span,img,a{margin:0;padding: 0;text-decoration: none;}
        #container{position: relative;width: 400px;height: 450px;overflow: hidden;margin: 20px auto;border:3px solid red;}
        #list{width: 2800px;height: 450px;position: absolute;z-index: 1;}
        #list img{width: 400px;height: 450px;float: left;}
        #buttons{position: absolute;float: left;width: 100px;height: 10px;bottom:50px;left:175px;z-index: 2;}
        #buttons span{float:left;height: 10px;width: 10px;margin-right: 5px;cursor: pointer;border:1px solid #000;border-radius: 50%;background: #ccc;z-index: 2;}
        #buttons .on{background: red;}
        .arrow{cursor: pointer; display: none;position:absolute;height: 30px;width: 30px;top:175px;font-size: 25px;line-height: 25px;text-align:center;color: #ccc;z-index: 2;background-color: RGBA(0,0,0,.3);}
        .arrow:hover{background-color: RGBA(0,0,0,.6);}
        #container:hover .arrow{display: block;}
        #prev{left: 25px;}
        #next{right: 25px;}
    </style>
    <script>
        window.onload=function () {//页面加载调用
            var container=document.getElementById('container');
            var list=document.getElementById('list');
            var buttons=document.getElementById('buttons').getElementsByTagName('span');
            var prev=document.getElementById('prev');
            var next=document.getElementById('next');
            var index=1;//用于索引当前按钮
            var len=5;//图片数量
            var animated = false;//判断切换是否进行
            var interval = 3000;//自动播放计时器3s
            var timer;//定时器
             function animate (offset) {//动画切换
                animated=true;//切换进行中
                var time=300;//位移总时间
                var inteval=10;//位移时间间隔
                var times=time/inteval;//位移次数
                var speed = offset/times;//位移速度
                var left= parseInt(list.style.left)+offset;//目标值

                var go=function () {
                    //这两种情况表示还在切换中
                    if ( (speed > 0 && parseInt(list.style.left) < left)  (speed < 0 && parseInt(list.style.left) > left)) {
                        list.style.left = parseInt(list.style.left) + speed + 'px';
                        setTimeout(go, inteval);//继续执行切换go()函数  
                    }
                    else {
                        list.style.left = left+ 'px';
                        if(left<-400*len){
                            list.style.left="-400px";
                        }
                        if(left>-400){
                            list.style.left=-400*len+"px";
                        }
                        animated = false;//切换完成 
                    }
                }
                go();
                }

           //用于为按钮添加样式
            function showButton() {     
               //先找出原来有.on类的按钮,并移除其类
                for (var i = 0; i < buttons.length ; i++) {  
                          if( buttons[i].className == 'on'){
                        buttons[i].className = '';     
                                   break;
                    }
                }        
                //为当前按钮添加类,索引下标从0开始,故需减1
                buttons[index - 1].className = 'on';
            }

//自动播放
            function play() {
                timer = setTimeout(function () {
                    next.onclick();
                    play();
                }, interval);
            }
            //清除定时器
            function stop() {
                clearTimeout(timer);
            }
//右点击
           next.onclick = function () {
                if (animated) {//如果切换还在进行,则直接结束,直到切换完成
                    return;
                }
                if(index==5){
                    index=1;
                    }
                    else{
                        index+=1;
                    }                   
                animate(-400);
                showButton();
            }
 //左点击          
            prev.onclick = function () {
                if (animated) {//如果切换还在进行,则直接结束,直到切换完成
                 return;
                 }
                if(index==1){
                    index=5;
                    }
                    else{
                        index-=1;
                    }

                animate(400);
                showButton(); 
                }

            //通过循环为按钮添加点击事件
            for (var i=0;i<buttons.length;i++) {
                buttons[i].onclick=function() {
                    if (animated) {//如果切换还在进行,则直接结束,直到切换完成
                     return;
                     }
                    //当继续点击按钮时不切换
                    if(this.className=='on'){
                        return;
                    }
                    //通过获取按钮的标签自定义属性index,得到索引值,再计算差值
                    var myIndex=parseInt(this.getAttribute('index'));
                    //真正的偏移量
                    var offset=-400*(myIndex-index);
                    animate(offset);
                    //将点击按钮的index属性置为当前
                    index=myIndex;
                    showButton();
                  }  
              } 
    //父容器的移入移出事件
            container.onmouseover = stop;
            container.onmouseout = play;
            play();//自动播放
    }

    </script>
</head>
<body>
    <div id="container">
        <div id='list' style='left: -400px'>
            <img src="img/5.jpg" alt="">
            <img src="img/1.jpg" alt="">
            <img src="img/2.jpg" alt="">
            <img src="img/3.jpg" alt="">
            <img src="img/4.jpg" alt="">
            <img src="img/5.jpg" alt="">
            <img src="img/1.jpg" alt="">
        </div>
        <div id='buttons'>
            <span index='1' class="on"></span>
            <span index='2'></span>
            <span index='3'></span>
            <span index='4'></span>
            <span index='5'></span>
        </div>
        <a href="javascript:;" id='prev' class="arrow"><</a>
        <a href="javascript:;" id='next' class="arrow">></a>        
    </div>
</body>
</html>

----------
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>query焦点轮播图</title>
    <style type="text/css">
        *{ margin: 0; padding: 0; text-decoration: none;}
        body { padding: 20px;}
        #container { width: 600px; height: 400px; border: 3px solid #333; overflow: hidden; position: relative;}
        #list { width: 4200px; height: 400px; position: absolute; z-index: 1;}
        #list img { float: left;}
        #buttons { position: absolute; height: 10px; width: 100px; z-index: 2; bottom: 20px; left: 250px;}
        #buttons span { cursor: pointer; float: left; border: 1px solid #fff; width: 10px; height: 10px; border-radius: 50%; background: #333; margin-right: 5px;}
        #buttons .on {  background: orangered;}
        .arrow { cursor: pointer; display: none; line-height: 39px; text-align: center; font-size: 36px; font-weight: bold; width: 40px; height: 40px;  position: absolute; z-index: 2; top: 180px; background-color: RGBA(0,0,0,.3); color: #fff;}
        .arrow:hover { background-color: RGBA(0,0,0,.7);}
        #container:hover .arrow { display: block;}
        #prev { left: 20px;}
        #next { right: 20px;}
    </style>
    <script type="text/javascript" src="js/jquery-3.0.0.min.js"></script>
    <script type="text/javascript">

        $(function () {//页面加载时调用
            var container = $('#container');
            var list = $('#list');
            var buttons = $('#buttons span');
            var prev = $('#prev');
            var next = $('#next');
            var index = 1;
            var len = 5;
            var interval = 3000;
            var timer;

            function animate (offset) {//动画效果
                var left = parseInt(list.css('left')) + offset;//目标值
                if (offset>0) {
                    offset = '+=' + offset;
                }
                else {
                    offset = '-=' + Math.abs(offset);
                }
                list.animate({'left': offset}, 300, function () {
                    if(left > -200){
                        list.css('left', -600 * len);
                    }
                    if(left < (-600 * len)) {
                        list.css('left', -600);
                    }
                });
            }

            function showButton() {
                buttons.eq(index-1).addClass('on').siblings().removeClass('on');
            }

            function play() {
                timer = setTimeout(function () {
                    next.trigger('click');
                    play();
                }, interval);
            }
            function stop() {
                clearTimeout(timer);
            }

            next.bind('click', function () {
                if (list.is(':animated')) {
                    return;
                }
                if (index == 5) {
                    index = 1;
                }
                else {
                    index += 1;
                }
                animate(-600);
                showButton();
            });

            prev.bind('click', function () {
                if (list.is(':animated')) {
                    return;
                }
                if (index == 1) {
                    index = 5;
                }
                else {
                    index -= 1;
                }
                animate(600);
                showButton();
            });

            buttons.each(function () {
                 $(this).bind('click', function () {
                     if (list.is(':animated')  $(this).attr('class')=='on') {
                         return;
                     }
                     var myIndex = parseInt($(this).attr('index'));
                     var offset = -600 * (myIndex - index);

                     animate(offset);
                     index = myIndex;
                     showButton();
                 })
            });

            container.hover(stop, play);

            play();

        });
    </script>
</head>
<body>

<div id="container">
    <div id="list" style="left: -600px;">
        <img src="img/5.jpg" alt="1"/>
        <img src="img/1.jpg" alt="1"/>
        <img src="img/2.jpg" alt="2"/>
        <img src="img/3.jpg" alt="3"/>
        <img src="img/4.jpg" alt="4"/>
        <img src="img/5.jpg" alt="5"/>
        <img src="img/1.jpg" alt="5"/>
    </div>
    <div id="buttons">
        <span index="1" class="on"></span>
        <span index="2"></span>
        <span index="3"></span>
        <span index="4"></span>
        <span index="5"></span>
    </div>
    <a href="javascript:;" id="prev" class="arrow">&lt;</a>
    <a href="javascript:;" id="next" class="arrow">&gt;</a>
</div>

</body>
</html>
打开App,阅读手记
24人推荐
发表评论
随时随地看视频慕课网APP

热门评论

能把源代码分享一下就好了。学习了好多,谢谢。

5张图片,加载7张图?

查看全部评论