序言: 响应式轮播图制作后不管是大于768px的非移动端还是小于768px的移动端 都会加载所有资源,会导致页面的加载速度和解析效率降低 对移动端极其不利。所以我们需要手写js来动态创建
一、所要达到效果:
- w<768px-移动端:图片会随着屏幕的缩小自动适应–缩小
做法:将img的宽度为100%,通过img标签来实现 - w>=768px:
图片做为背景,当屏幕宽度变宽的时候,会显示更多的图片的两边区域
做法:
1.background-image添加图片
2.添加background-position:center center
3.background-size:cover
<!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>
三、出现问题
响应式轮播图制作后不管是大于768px的非移动端还是小于768px的移动端 都会加载所有资源,会导致页面的加载速度和解析效率降低 对移动端极其不利。
- 解决办法:使用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>
- 创建后的结构 通过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>
item.html($('<a href="javascript:;" class="pcImg"></a>').css("backgroundImage","url('"+imgSrc+"')"));
- 完整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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。