开发者学堂课程【移动 Web 前端开发:轮播图-非移动端 】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/594/detail/8457
轮播图-非移动端
目录
一、如何创建非移动端的轮播图
一、如何创建非移动端的轮播图
<
style>
body{
/*在页面当中的任何地方都可以*/
font-size:100px;
.
pc_imgeBox{
display:black;
width:100%;
height:450px;
b
ackground-image:url(“../images/slide_01_2000x410.jpg”);
b
ackground-repeat:no-repeat;
b
ackground-size:cover;
b
ackground-position:center;
.
carousel-inner .item:nth-child(2){
background-image:url(“../images/slide_02_2000x410.jpg”)
.
carousel-inner .item:nth-child(3){
background-image:url(“../images/slide_03_2000x410.jpg”)
.
carousel-inner .item:nth-child(4){
background-image:url(“../images/slide_04_2000x410.jpg”)
}
<
/style>
<
div class=”carousel-inner”>
<div class=”item active”>
<a class=”pc_imageBox” href=”#”>
<
/a>
<
/div>
<!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>
使用 js 动态创建下面移动端和非移动端
通过 data 来获取路径:
添加子元素:
item.html($('').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($('').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');
}
});
});