美文网首页
js轮播图

js轮播图

作者: 刘翾 | 来源:发表于2017-11-08 20:44 被阅读42次

...由于我的图片画质太高,效果图传不上来, 大家自己试一试吧, 在同一个文件夹保存下面的代码和分别为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>

相关文章

  • 三种样式的轮播图

    一、100%比例轮播图 HTML代码 CSS样式 js代码 二、手动箭头轮播图 三、简易轮播图

  • 第五周学习内容

    焦点图轮播特效之原理、焦点图轮播样式之布局、焦点图轮播之箭头切换、焦点图轮播之无限滚动。 js简介、用法、输出。

  • 轮播图

    轮播图01 html css js

  • 项目-轮播图

    整个轮播图分为三部分:轮播指标、轮播项目及轮播导航。用boostrap实现轮播图要比用js、jQuery方便的多,...

  • 使用vue-awesome-swiper方法

    在main.js中引入轮播图插件 在基础组件中建立轮播图组件:

  • 原生JS轮播图

    :轮播图 1:页面 2:CSS 3:Js

  • JavaScript | 365笔记第87天 | 原生JS做轮播

    用原生JS做一个轮播图

  • 2021-11-25

    js 一页显示多张图无缝轮播

  • 原生js制作轮播图

    原生js 制作的轮播图 今天学习了一个原生js轮播图的方法。效果图如下 通过点击上下页和中间的点进行翻页,通过改变...

  • 常用插件

    js 插件 1,myFocus 焦点图插件===专注焦点图的js 库(轮播图)轻量级 http://demo.jb...

网友评论

      本文标题:js轮播图

      本文链接:https://www.haomeiwen.com/subject/smtumxtx.html