使用Bootstrap插件(Carousel)制作响应式轮播图

序言: 响应式轮播图制作后不管是大于768px的非移动端还是小于768px的移动端 都会加载所有资源,会导致页面的加载速度和解析效率降低 对移动端极其不利。所以我们需要手写js来动态创建

一、所要达到效果:

  1. w<768px-移动端:图片会随着屏幕的缩小自动适应–缩小
    做法:将img的宽度为100%,通过img标签来实现
  2. w>=768px:
    图片做为背景,当屏幕宽度变宽的时候,会显示更多的图片的两边区域
    做法:
    1.background-image添加图片
    2.添加background-position:center center
    3.background-size:cover

二、使用插件后的代码(将移动端和pc端放在一起)

  1. 每一个item就是一个轮播的图片内容
<!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">
    <title>Bootstrap 101 Template</title>
    <link href="./lib/bootstrap/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<style>
    .mobileImg{
        width: 100%;
        display: block;
    }
    .mobileImg > img{
        width: 100%;
        display: block;
    }
    .pcImg{
        width: 100%;
        height: 410px;
        display: block;
        /*设置背景图片了*/

        /*设置图片居中显示*/
        background-position: center center;
        /*设置背景图片的大小*/
        background-size: cover;
    }
</style>
<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>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
        <div class="item active">
            <!--移动端-->
            <a href="javascript:;" class="mobileImg hidden-sm hidden-md hidden-lg">
                <img src="./images/slide_01_640x340.jpg" alt="...">
            </a>
            <!--非移动端-->
            <a href="javascript:;" class="pcImg hidden-xs" style="background-image: url('./images/slide_01_2000x410.jpg');"></a>
        </div>
        <div class="item">
            <a href="javascript:;" class="mobileImg  hidden-sm hidden-md hidden-lg">
                <img src="./images/slide_02_640x340.jpg" alt="...">
            </a>
            <a href="javascript:;" class="pcImg  hidden-xs" style="background-image: url('./images/slide_02_2000x410.jpg');"></a>
        </div>
        <div class="item">
            <a href="javascript:;" class="mobileImg  hidden-sm hidden-md hidden-lg">
                <img src="./images/slide_03_640x340.jpg" alt="...">
            </a>
            <a href="javascript:;" class="pcImg  hidden-xs" style="background-image: url('./images/slide_03_2000x410.jpg');"></a>
        </div>
        <div class="item">
            <a href="javascript:;" class="mobileImg  hidden-sm hidden-md hidden-lg">
                <img src="./images/slide_04_640x340.jpg" alt="...">
            </a>
            <a href="javascript:;" class="pcImg  hidden-xs" style="background-image: url('./images/slide_04_2000x410.jpg');"></a>
        </div>
    </div>
    <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>
<script src="./lib/jquery/jquery.min.js"></script>
<script src="./lib/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>

三、出现问题

  1. 响应式轮播图制作后不管是大于768px的非移动端还是小于768px的移动端 都会加载所有资源,会导致页面的加载速度和解析效率降低 对移动端极其不利。
  2. 解决办法:使用js动态创建下面移动端和非移动端俩项:
       <div class="item active">
            <!--移动端-->
            <a href="javascript:;" class="mobileImg hidden-sm hidden-md hidden-lg">
                <img src="./images/slide_01_640x340.jpg" alt="...">
            </a>
            <!--非移动端-->
            <a href="javascript:;" class="pcImg hidden-xs" style="background-image: url('./images/slide_01_2000x410.jpg');"></a>
        </div>
  1. 创建后的结构 通过data来获取路径:
	<div class="carousel-inner" role="listbox">
        <div class="item active" data-large-image="./images/slide_01_2000x410.jpg" data-small-image="./images/slide_01_640x340.jpg">  
        </div>
 	</div>
  1. js代码(wjs-index.js)
    1.添加子元素
item.html($('<a href="javascript:;" class="pcImg"></a>').css("backgroundImage","url('"+imgSrc+"')"));
  1. 完整js代码
$(function(){
    /*获取当前所有item*/
    var items=$(".carousel-inner .item");
    /*监听屏幕的大小改变*/
    $(window).on("resize",function(){
        /*1.获取当前屏幕的宽度*/
        var width=$(window).width();
        /*2.判断屏幕的宽度*/
        if(width>=768){/*说明非移动端*/
            /*为每一个item添加子元素--遍历*/
            $(items).each(function(index,value){
                var item=$(this);
                /*当前自定义属性中 存储的图片路径*/
                var imgSrc=item.data("largeImage");
                console.log(imgSrc);
                /*添加非移动端的子元素*/
                item.html($('<a href="javascript:;" class="pcImg"></a>').css("backgroundImage","url('"+imgSrc+"')"));
            });
        }
        else{
            $(items).each(function(index,value){
                var item=$(this);
                var imgSrc=item.data("smallImage");
                item.html('<a href="javascript:;" class="mobileImg"><img src="'+imgSrc+'" alt="..."></a>');
            });
        }
    }).trigger("resize");

    /*添加移动端的滑动操作*/
    var startX,endX;
    var carousel_inner=$(".carousel-inner")[0];

    /*获取当前轮播图*/
    var carousel=$(".carousel");

    carousel_inner.addEventListener("touchstart",function(e){
        startX= e.targetTouches[0].clientX;
    });
    carousel_inner.addEventListener("touchend",function(e){
        endX= e.changedTouches[0].clientX;
        if(endX-startX > 0){
            /*上一张*/
            carousel.carousel('prev');
        }
        else if(endX-startX < 0){
            /*下一张*/
            carousel.carousel('next');
        }
    });
});

5.完整html代码

<!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">
    <title>Bootstrap 101 Template</title>
    <link href="./lib/bootstrap/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<style>
    .mobileImg{
        width: 100%;
        display: block;
    }
    .mobileImg > img{
        width: 100%;
        display: block;
    }
    .pcImg{
        width: 100%;
        height: 410px;
        display: block;
        /*设置背景图片了*/

        /*设置图片居中显示*/
        background-position: center center;
        /*设置背景图片的大小*/
        background-size: cover;
    }
</style>
<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>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
        <div class="item active" data-large-image="./images/slide_01_2000x410.jpg" data-small-image="./images/slide_01_640x340.jpg">  
        </div>
        <div class="item" data-large-image="./images/slide_02_2000x410.jpg" data-small-image="./images/slide_02_640x340.jpg">
 
        </div>
        <div class="item" data-large-image="./images/slide_03_2000x410.jpg" data-small-image="./images/slide_03_640x340.jpg">

        </div>
        <div class="item" data-large-image="./images/slide_04_2000x410.jpg" data-small-image="./images/slide_04_640x340.jpg">

        </div>
    </div>
    <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>
<script src="./lib/jquery/jquery.min.js"></script>
<script src="./lib/bootstrap/js/bootstrap.min.js"></script>
<script src="./js/wjs-index.js"></script>
</body>
</html>
上一篇:Bootstrap - Carousel


下一篇:Property ‘next‘ does not exist on type ‘Component<any, {}, any>‘问题的解决方法