当前位置:网站首页>详解轮播图二-通过left定位来轮播图片
详解轮播图二-通过left定位来轮播图片
2022-08-08 11:53:00 【勇敢*牛牛】
详解轮播图二



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
margin: 0;
padding: 0;
}
.carousel{
width: 100%;
height: 33.3vw;
position: relative;
left: 0;
top: 0;
font-size: 0;
min-width: 1000px;
overflow: hidden;
}
.carousel>.img-con{
width: 500%;
height: 100%;
position: absolute;
transition: all 0.5s;
left: 0;
}
.carousel .img-block{
width: 20%;
height: 100%;
position: relative;
float: left;
}
.carousel .img-block>img{
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
}
.carousel .img-block>div{
position: absolute;
left:10vw;
top: 2vw;
font-size: 20px;
color: white;
}
.carousel .img-block>div>span>i{
font-style:normal;
font-size: 28px;
}
.carousel .img-block>div>h1{
font-size: 20px;
}
.clear::after
{
content: "";
display: block;
visibility: hidden;
height: 0;
overflow: hidden;
clear: both;
}
ul{
list-style: none;
position: absolute;
bottom: 3vw;
left: 50%;
transform: translate(-50%,0);
}
ul li{
width: 1.6vw;
height: 1.6vw;
border:2px solid red;
margin-left: 1.3vw;
float: left;
border-radius: 1.6vw;
}
.left,.right{
position: absolute;
top: 50%;
transform: translate(0,-50%);
}
.left{
left: 3vw;
}
.right{
right: 3vw;
}
</style>
</head>
<body>
<div class="carousel">
<div class="img-con clear">
<div class="img-block" id="a">
<img src="./img/a.jpg">
<div>
<span><i>28</i>/Jul.2022</span>
<h1>与父母的47天自驾游|向疆而行2万里,我们依旧是过客</h1>
</div>
</div>
<div class="img-block" id="b">
<img src="./img/b.jpg">
<div>
<span><i>27</i>/Jul.2022</span>
<h1>自驾川西小环线,在千碉之国遇见梨花如雪的季节</h1>
</div>
</div>
<div class="img-block" id="c">
<img src="./img/c.jpg">
<div>
<span><i>26</i>/Jul.2022</span>
<h1>被误解的沙县,原来有这么多美食只有在当地才能吃到!</h1>
</div>
</div>
<div class="img-block" id="d">
<img src="./img/d.jpg">
<div>
<span><i>25</i>/Jul.2022</span>
<h1>周末出逃计划 | 打卡美丽中国</h1>
</div>
</div>
<div class="img-block" id="e">
<img src="./img/e.jpg">
<div>
<span><i>24</i>/Jul.2022</span>
<h1>寻迹山川湖海,邂逅云南的冬与夏</h1>
</div>
</div>
</div>
<ul class="clear">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<img src="./img/left.png" class="left">
<img src="./img/right.png" class="right">
</div>
<script>
var imgCon,left,right,dot;
var pos = 0;//定位图片的索引
var prev;
init();
function init(){
imgCon = document.querySelector('.img-con');
left = document.querySelector('.left');
right = document.querySelector('.right');
dot = document.querySelector('ul');
/* 给lu,left,right添加点击事件 */
dot.addEventListener('click',dotClickHandler);
left.addEventListener('click',bnClickHandler);
right.addEventListener('click',bnClickHandler);
}
/* ul的点击事件 */
function dotClickHandler(e){
if(e.target.nodeName !== "LI") return ;
/* 获取被点击的li位置 */
pos = Array.from(this.children).indexOf(e.target);
imgConMove()
changePrev()
}
/* 左右按钮的点击事件 */
function bnClickHandler(e){
/* 如果被点击左边执行 */
if(this === left){
/* 第一次点击之后pos==4 */
if(--pos<0) pos = 4
}else{
if(++pos>4) pos = 0
}
imgConMove();
changePrev()
}
/* 如果点击左边移动的是这个imgConleft的倍数 */
function imgConMove(){
/* imgCon.firstElementChild.offsetWidth 一个图片的宽度 */
imgCon.style.left = pos * -imgCon.firstElementChild.offsetWidth + 'px';
console.log(pos * -imgCon.firstElementChild.offsetWidth);
}
function changePrev(){
if(prev){
prev.style.backgroundColor = 'transparent'
}
prev = dot.children[pos];
prev.style.backgroundColor = 'red'
}
</script>
</body>
</html>
边栏推荐
猜你喜欢

移动适配vw/vh方法—vw/vh实例—模拟B站手机端首页—获取样式教程视频

探究!一个数据包在网络中的心路历程

Redis的那些事:一文入门Redis的基础操作

(kali - elevated privileges 】 【 4.2.4) social engineering toolkit: remote control trojans use, set up and use

分分钟快速定制您的专属个性化软件应用——BizTool自动化工具简介

论文阅读《Omnidirectional DSO: Direct Sparse Odometry with Fisheye Cameras》

office安装出现了“office对安装源的访问被拒绝30068-4(5)”错误

转转商品系统高并发实战(数据篇)

模式识别 学习笔记:第八章 特征提取

Mysql索引优化实战
随机推荐
neural network classification
上周热点回顾(8.1-8.7)
安科瑞预付费水电集团物业解决方案-Susie 周
软件测试之测试代表用户
一文搞懂│XSS攻击、SQL注入、CSRF攻击、DDOS攻击、DNS劫持
SQL 语言的详解 --- 最最基础的内容!!! 刚学完常复习
各位,我想知道,既然数据全部读取过来存放内存,我flink sql窗口关闭之后再次查询这个cdc映射
【访谈】Eotalk Vol.01:Eoapi,我们希望以开源的方式构建 API 生态系统
Supervisor 后台进程管理
案例分析 | 宜家以双钻设计模型探索线上零售新业务
Geoffrey Hinton:深度学习的下一个大事件
这个选项是不是当数据库主键或唯一键发生冲突时替换数据
宝塔实测-TinkPHP5.1框架小程序商城源码
如何在go重打印函数调用者信息Caller
模式识别 学习笔记:第六章 其他分类方法 (持续更新中。。。)
一文读懂配置管理(CM)
鲲鹏开发者创享日2022:鲲鹏全栈创新 与开发者共建数字湖南
安装MinGW-w64
MYSQL 的 MASTER到MASTER的主主循环同步
消防安全知识培训讲座