前端之各种情况下实现的轮播图

1.样式一

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        *{margin: 0;padding: 0;}
        ul,ol{ list-style: none;}
        .wrapper{
            width: 580px;
            height: 240px;
            margin: 100px auto;
            /*overflow: hidden;*/
            position: relative;
        }
        .wrapper ul{
            width: 100%;
            height: 240px;
            overflow: hidden;

        }
      
        ol{
            position: absolute;
            right: 0;
            bottom: 10px;
            width: 190px;
        }
        ol li{
            float: left;
            width: 20px;
            height: 20px;
            margin: 0 5px;
            text-align: center;
            border-radius: 50%;
            cursor: pointer;
            background-color: #abc;
        }
        ol li.current{
            background-color: pink;
        }
    </style>
    <script type="text/javascript" src="./jquery-3.3.1.js"></script>
    <script type="text/javascript">
        $(function () {
            // 根据ol下li的索引号,匹配ul下相对应li的索引号
            $(".wrapper ol li").mouseenter(function () {
                $(this).addClass("current").siblings('li').removeClass("current");
                console.log($(this).index());
                $(".wrapper ul li").eq($(this).index()).stop().fadeIn("fast").siblings('li').stop().fadeOut();
            });
        });
    </script>
</head>
<body>
    <div class="wrapper">
        <ul>
            <li><img src="./1.jpg" alt=""/></li>
            <li><img src="./2.jpg" alt=""/></li>
            <li><img src="./3.jpg" alt=""/></li>
            <li><img src="./4.jpg" alt=""/></li>
            <li><img src="./5.jpg" alt=""/></li>

            
        </ul>
        <ol>
            <li class="current">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ol>
    </div>
</body>
</html>

2.无缝轮播

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        *{
            padding: 0;
            margin: 0;
        }
        ul{
            list-style: none;
        }
        .slider-list{

            width: 580px;
            overflow: hidden;
            margin: 100px auto;
            position: relative;
        }
        .slider-list .slider-wrapper{
            height: 470px;
        }
        .slider-wrapper ul{
            height: 100%;

            position: relative;

        }
        .slider-wrapper ul li{
            float: left;
            width: 590px;
            height: 470px;

        }
        .slider-wrapper ul li a{
            display: block;
            width: 100%;
            height: 100%;
        }
         .focus-img{
            width: 590px;
            height: 470px;
        }
        button{
            position: absolute;
            width: 24px;
            height: 40px;
            top: 50%;
            line-height: 40px;
            text-align: center;
            background-color: rgba(0,0,0,.2);
            color: white;
            font-size: 30px;
            border: 0;
            outline: none;
            cursor: pointer;
            z-index: 99;
        }
        button.next{
            right: 0;
        }
        button.prev{
            left: 0;
        }
        .slider-index{
            position: absolute;
            bottom: 10px;
            left:250px;
            z-index: 2;

        }
        .slider-index span{
            display: inline-block;
            width: 10px;
            height: 10px;
            border: 2px solid red;
            border-radius: 50%;
        }
        .slider-index span.active{
            background-color: orange;
        }

    </style>
</head>
<body>
    <div class="slider-list">
        <div class="slider-wrapper">
            <ul>
                
            </ul>
        </div>
        <button class="next">></button>
        <button class="prev"><</button>
        <div class="slider-index">
            <span class="active"></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            
        </div>
    </div>
    <script type="text/javascript" src="jquery-3.3.1.js"></script>
    <script type="text/javascript">
        
        $(function(){
            // 1.获取本地图片数据 590*470
            var imgArr = ['./5.jpg','./1.jpg','./2.jpg','./3.jpg','./4.jpg','./5.jpg','./1.jpg'];

            // 获取图片的宽度
            var imgWidth = 590;
            var len = $('span').length;

            // 2.遍历数据 将图片添加到ul中
            for(let i = 0;i < imgArr.length;i++){
               
                $(`<li>
                    <a href="javascript:;">
                        <img src=${imgArr[i]} title=${i}>
                    </a>
                </li>`).appendTo('.slider-wrapper ul').addClass('slider-item');    

            }
            

            // 设置图片的类名
            // $('img').addClass('focus-img');
            // // 设置父盒子的总宽度
            $('.slider-wrapper').width(imgArr.length*imgWidth);
            $('.slider-wrapper ul').width(imgArr.length*imgWidth);


            // // 初始化 
            // // 默认显示第一张图片
            init();
            function init(){
                 $("ul").css("left",-imgWidth);
            }

            
            // // 下一张
            $('button.next').click(function(event) {
                
                next();
            });

            // // 控制图片显示第几张
            var count  = 1;
            function next(){
                console.log(count)

                if (count ==len+1) {
                    // alert(3);
                    count  = 2;
                    $("ul").css("left",-imgWidth);
                }else{
                    // alert(5);
                    count++;
                }
                $('.slider-wrapper ul').stop().animate({left:-count*imgWidth},200);

                
                // 控制轮播图索引改变颜色
                if (count>len) {
                    $("span").eq(0).addClass("active").siblings("span").removeClass("active");
                }else{

                 $("span").eq(count-1).addClass("active").siblings("span").removeClass("active");
                }
                
            }

            // 给小圆圈添加点击事件

            $('span').click(function(){
                  //自己的样式
                 $(this).addClass("active").siblings("span").removeClass("active");
                  count = $(this).index()+1;
                $("ul").animate({"left":-count*imgWidth},200);
            })

            // setInterval(next,2000);


            

        })
    </script>
</body>
</html>

3.基于bootstrapt下的轮播图

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 必须定义一个盒子 -->
    <div class="container">
        <!-- 设置栅格 -->
        <div class="col-lg-12">
            
            <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
              <!-- Indicators -->
              <ol class="carousel-indicators">
                <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
                <li data-target="#carousel-example-generic" data-slide-to="1"></li>
                <li data-target="#carousel-example-generic" data-slide-to="2"></li>
                <li data-target="#carousel-example-generic" data-slide-to="3"></li>
                <li data-target="#carousel-example-generic" data-slide-to="4"></li>
              </ol>

              <!-- Wrapper for slides -->
              <div class="carousel-inner" role="listbox">
                <div class="item active">
                  <img src="./1.jpg" alt="...">
                  <div class="carousel-caption">
                  </div>
                </div>
                <div class="item">
                  <img src="./2.jpg" alt="...">
                  <div class="carousel-caption">
                  </div>
                </div>
                <div class="item">
                  <img src="./3.jpg" alt="...">
                  <div class="carousel-caption">
                  </div>
                </div>
                <div class="item">
                  <img src="./4.jpg" alt="...">
                  <div class="carousel-caption">
                  </div>
                </div>
                <div class="item">
                  <img src="./5.jpg" alt="...">
                  <div class="carousel-caption">
                  </div>
                </div>
              </div>

              <!-- Controls -->
              <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
                <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                <span class="sr-only">Previous</span>
              </a>
              <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
                <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                <span class="sr-only">Next</span>
              </a>
            </div>


                </div>
            </div>



        </div>
    </div>
  </head>
  <body>
    
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  </body>
</html>

4.基于swiper下的轮播图

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="swiper-4.3.2/dist/css/swiper.min.css">
    <style type="text/css">

    img {
        width: 600px;
        height: 300px;
    }  
    </style>
</head>
<body>
    <div class="swiper-container">
        <div class="swiper-wrapper">
            <div class="swiper-slide"><img src="./1.jpg" alt="..."></div>
            <div class="swiper-slide"><img src="./2.jpg" alt="..."></div>
            <div class="swiper-slide"><img src="./3.jpg" alt="..."></div>
            <div class="swiper-slide"><img src="./4.jpg" alt="..."></div>
            <div class="swiper-slide"><img src="./5.jpg" alt="..."></div>
        </div>
        <!-- 如果需要分页器 -->
        <div class="swiper-pagination"></div>
        
        <!-- 如果需要导航按钮 -->
        <div class="swiper-button-prev"></div>
        <div class="swiper-button-next"></div>
        
        <!-- 如果需要滚动条 -->
        <div class="swiper-scrollbar"></div>
    </div>    


    
    <script src="swiper-4.3.2/dist/js/swiper.min.js"></script>
    <script language="javascript"> 
    var mySwiper = new Swiper('.swiper-container',{
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
      on: {
        slideChange: function(){
          if(this.isBeginning){
            this.navigation.$prevEl.css('display','none');
          }else{
            this.navigation.$prevEl.css('display','block');  
          }
        },
      },
    })
    </script>

</body>
</html>

 

posted @ 2018-06-09 22:05  -Learning-  阅读(584)  评论(0编辑  收藏  举报