js轮播图

...由于我的图片画质太高,效果图传不上来, 大家自己试一试吧, 在同一个文件夹保存下面的代码和分别为1,2,3,4,5.jpg的五张图片
我在这里用的图片全部都是1920*1080的尺寸

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>轮播图</title>
    <style type="text/css">
        *{
            margin: 0px;
            padding: 0px;
            list-style: none;
        }
        #box{
            width: 1920px;
            height: 1080px;
            margin: 1px auto;
            position: relative;
            border: 1px soild block;
            overflow: hidden;
        }
        #box #list{
            position: absolute;
            width: 13440px;//为了让五张图片全部在一行表示出来
            height: 1080px;
            z-index: 1;
        }
        #box #list li{
            float: left;
            height: 1080px;
            width: 1920px;
        }
        #buttons {
            position: absolute;
            left: 910px;
            bottom: 50px;
            z-index: 2;//保证小圆点在图片上层
            height: 30px;
            width: 300px;
        }
        #buttons li {
            float: left;
            margin-right: 20px;
            width: 20px;
            height: 20px;
            border: 1px solid #fff;
            border-radius: 50%;
            background: #333;
            cursor: pointer;
            list-style: none;
        }
        #buttons .on {
            background: orangered;
        }
        .arrow {
            position: absolute;
            top: 520px;
            z-index: 2;
            display: none;
            width: 200px;
            height: 200px;
            font-size: 100px;
            font-weight: bold;
            line-height: 200px;//使方块中的">"水平垂直居中
            text-align: center;
            color: #fff;
            background-color: rgba(0, 0, 0, 0.3);
            cursor: pointer;
        }
        
        .arrow:hover {
            background-color: rgba(0, 0, 0, 0.7);
        }
        
        #box:hover .arrow {//鼠标移入图片时, 无
            display: block;
        }
        
        #prev {
            left: 20px;
        }
        
        #next {
            right: 20px;
        }
    </style>
</head>
<body>
    <div id="box">
        <ul id="list" style="left: -1920px">
这里又为什么要有7张图片呢, 主要是为了无缝的优化,无缝滚动。

当你从最后一张图切换回第一张图时,有很大空白,利用两张辅助图来填补这个空白。

这里补充下无缝滚动,直接看代码,复制最后一张图片放置第一张图片前,同时复制第一张图片放置最后一张图片的后面。并且,将第一张图片辅助图(实际上是实际显示的第5张图片隐藏起来,故设置style="left: -600px;")
            <li><a href="#"><img src="5.jpg"></a></li>
            <li><a href="#"><img src="1.jpg"></a></li>
            <li><a href="#"><img src="2.jpg"></a></li>
            <li><a href="#"><img src="3.jpg"></a></li>
            <li><a href="#"><img src="4.jpg"></a></li>
            <li><a href="#"><img src="5.jpg"></a></li>
            <li><a href="#"><img src="1.jpg"></a></li>
            
        </ul>
        <ul id="buttons">//小圆点
            <li index="1" class="on"></li>
            <li index="2"></li>
            <li index="3"></li>
            <li index="4"></li>
            <li index="5"></li>
        </ul>
        <a href="javascript:" id="prev" class="arrow">&lt;</a>
        <a href="javascript:" id="next" class="arrow">&gt;</a>
    </div>
    <script>
        window.onload = function() {
            var list = document.getElementById('list');
            var prev = document.getElementById('prev');
            var next = document.getElementById('next');
            var box = document.getElementById('box');

            function animate(offset) {
                //获取的是style.left,是相对左边获取距离,所以第一张图后style.left都为负值,
                //且style.left获取的是字符串,需要用parseInt()取整转化为数字。
                var newLeft = parseInt(list.style.left) + offset;
                if(newLeft < -11520){
                    newLeft = -3840;
                }//当到边界时, 返回图片的当前位置
                if(newLeft > 0){
                    newLeft = -7680;
                }
                list.style.left = newLeft + 'px';
            }

            // prev.onclick = function() {             
            //     animate(1920);
            // }
            // next.onclick = function() {  
            //     animate(-1920);
            // }
            
            var timer;
            function play() {//设置定时器
                timer = setInterval(function () {
                next.onclick()
                }, 1500)
            }
            play();


            function stop() {
                clearInterval(timer);
            }
            box.onmouseover = stop;
            box.onmouseout = play;


            var buttons = document.getElementById('buttons').getElementsByTagName('li');
            var index = 1;

            function buttonsShow() {
                for (var i = 0; i < buttons.length; i++) {
                    if (buttons[i].className == 'on') {
                        buttons[i].className = '';
                    }
                }//遍历找到有class为on的li, 并清除掉
                buttons[index-1].className = 'on';
            }

            prev.onclick = function() {
                index--;
                if (index < 1) {
                    index = 5;
                }
                buttonsShow();
                animate(1920);
            }
            next.onclick = function() {
                index++;
                if (index > 5) {
                    index = 1;
                }
                buttonsShow();
                animate(-1920);
            }

            for (var i = 0; i < buttons.length; i++) {//下面由于产生了一个典型的闭包, 因此要采用立即执行函数的方法
                (function(j){
                    buttons[j].onclick = function () {
                        console.log(j);

                        var clickIndex = parseInt(this.getAttribute('index'));
                        var offset = 1920 * (index - clickIndex);
                        animate(offset); //存放鼠标点击后的位置,用于小圆点的正常显示 
                        index = clickIndex;
                        buttonsShow();
                    }
                })(i)
            }


        }
    </script>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这个轮播图的JS动态代码: var currentNum = 0; var changeBgOfObj = doc...
    w蜗牛w阅读 571评论 2 0
  • 使用css渐变动画,完成图片的切换的效果。 效果图:
    Sing_Chan阅读 407评论 1 4
  • 那年长街春意正浓, 策马同游,烟雨如梦。 檐下躲雨,望进一双, 深邃眼瞳,宛如华山夹着细雪的微风。 雨丝微凉,风吹...
    薄荷喋喋阅读 274评论 0 0
  • 有的男人总是数落自己老婆爱花钱,但是你是否曾想过,从来没有哪个女人因为爱花钱而把家底败光,反倒很多省钱的女人把一家...
    茗艺堂阅读 504评论 0 0
  • 一品茶,四季花,水北江南是吾家。秋寂寥,胜春朝,青山踏遍,风景独好。少、少、少 贪嗔痴,爱别离,心羡黄莺恰恰啼。杯...
    棠君170809阅读 232评论 0 0