当前位置:网站首页>学习探索-无缝轮播图
学习探索-无缝轮播图
2022-07-06 11:32:00 【miao_zz】
效果图
代码块
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
* {
padding: 0px;
margin: 0px;
box-sizing: border-box;
}
.carousel-container {
width: 400px;
position: relative;
margin: 40px auto;
overflow: hidden;
}
.carousel-list {
width: 100%;
height: 100%;
display: flex;
position: relative;
}
.carousel-items {
width: 100%;
height: 100%;
}
.carousel-items img {
width: 100%;
height: 100%;
display: block;
}
.carousel-arrow {
border-radius: 50%;
width: 40px;
height: 40px;
position: absolute;
top: 50%;
transform: translateY(-50%);
background-color: rgba(0, 0, 0, 0.3);
display: flex;
justify-content: center;
align-items: center;
color: #fff;
font-size: 12px;
cursor: pointer;
}
.carousel-arrow:hover {
font-size: 16px;
}
.carousel-arrow-left {
left: 50px;
}
.carousel-arrow-right {
right: 50px;
}
.indicator {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
}
.indicator span {
border: 1px solid #00a2ef;
width: 10px;
height: 10px;
border-radius: 50%;
display: inline-block;
cursor: pointer;
}
.indicator-current {
background-color: #00a2ef;
}
</style>
</head>
<body>
<div class="carousel-container">
<div class="carousel-list">
<div class="carousel-items">
<img src="img/8.jpg" />
</div>
<div class="carousel-items">
<img src="img/9.jpg" />
</div>
<div class="carousel-items">
<img src="img/10.jpg" />
</div>
<div class="carousel-items">
<img src="img/11.jpg" />
</div>
</div>
<div class="carousel-arrow carousel-arrow-left">≪</div>
<div class="carousel-arrow carousel-arrow-right">≫</div>
<div class="indicator">
<span class="indicator-current"></span>
<span></span>
<span></span>
<span></span>
</div>
</div>
</body>
<script type="text/javascript">
const doms = {
carouselBoxWidth: document.querySelector(".carousel-container").clientWidth, //获取轮播图可视区域的宽度
carouselList: document.querySelector(".carousel-list"),
arrowLeft: document.querySelector(".carousel-arrow-left"),
arrowRight: document.querySelector(".carousel-arrow-right"),
indicators: document.querySelectorAll(".indicator span"),
currentIndex: 0, //下标从0开始
}
init();
function init() {
//复制第一张图
const first = doms.carouselList.firstElementChild.cloneNode(true);
//复制最后一张图
const last = doms.carouselList.lastElementChild.cloneNode(true);
//将第一张图放到末尾
doms.carouselList.appendChild(first);
//将最后一张图放到第一张,
/**
* insertBefore() 方法可在已有的子节点前插入一个新的子节点。
* 语法:
* insertBefore(newnode,node);
* 参数:
* newnode: 要插入的新节点。
* node: 指定此节点前插入节点。
* **/
doms.carouselList.insertBefore(last, doms.carouselList.firstElementChild);
//设置包裹轮播内容块的宽度,
//document.querySelector(".carousel-list").childElementCount,要放在插入节点的后面,才能获取最新的节点个数
const childElementCount = document.querySelector(".carousel-list").childElementCount;
//给轮播图外框设置宽度,用于包裹所有轮播块
doms.carouselList.style.width = doms.carouselBoxWidth * childElementCount + 'px';
doms.carouselList.style.transform = `translateX(-${doms.carouselBoxWidth}px)`;
}
//轮播图渲染
function moveTo(index) {
doms.carouselList.style.transform = `translateX(-${(index+1)*doms.carouselBoxWidth}px)`;
doms.carouselList.style.transition = '0.5s';
//去掉指示器的选中效果
let active = document.querySelector(".indicator span.indicator-current");
active.classList.remove("indicator-current");
//添加选中的指示器
doms.indicators[index].classList.add("indicator-current");
doms.currentIndex = index;
}
//轮播图重置渲染效果
function resetMoveTo(index) {
doms.carouselList.style.transform = `translateX(-${(index+1)*doms.carouselBoxWidth}px)`;
doms.carouselList.style.transition = 'none';
//去掉指示器的选中效果
let active = document.querySelector(".indicator span.indicator-current");
active.classList.remove("indicator-current");
//添加选中的指示器
doms.indicators[index].classList.add("indicator-current");
doms.currentIndex = index;
}
//点击指示器
doms.indicators.forEach((item, index) => {
item.addEventListener("click", () => {
moveTo(index)
})
})
//点击向左箭头
doms.arrowLeft.addEventListener("click", () => {
const total = doms.indicators.length;
if (doms.currentIndex === 0) {
//无缝轮播
//resetMoveTo(total - 1);等效于下面
doms.carouselList.style.transform = `translateX(-${(total)*doms.carouselBoxWidth}px)`;
doms.carouselList.style.transition = 'none';
doms.carouselList.clientHeight; //强制渲染
moveTo(total - 1)
} else {
moveTo(doms.currentIndex - 1)
}
})
//点击向右箭头
doms.arrowRight.addEventListener("click", () => {
const total = doms.indicators.length;
if (doms.currentIndex === total - 1) {
//无缝轮播
//resetMoveTo(0);等效于下面
doms.carouselList.style.transform = `translateX(-${(0+1)*doms.carouselBoxWidth}px)`;
doms.carouselList.style.transition = 'none';
doms.carouselList.clientHeight; //强制渲染
moveTo(0)
} else {
moveTo(doms.currentIndex + 1)
}
})
</script>
</html>
边栏推荐
- 业务与应用同步发展:应用现代化的策略建议
- php+redis实现超时取消订单功能
- Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
- 快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
- How to type multiple spaces when editing CSDN articles
- [matlab] Simulink the input and output variables of the same module cannot have the same name
- R语言ggplot2可视化:使用ggpubr包的ggdotplot函数可视化点阵图(dot plot)、设置palette参数设置不同水平点阵图数据点和箱图的颜色
- 倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
- How word displays modification traces
- Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
猜你喜欢
Documents to be used in IC design process
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
[depth first search] Ji suanke: a joke of replacement
CCNP Part 11 BGP (III) (essence)
The second day of rhcsa study
黑马--Redis篇
助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!
多线程基础:线程基本概念与线程的创建
zabbix 代理服务器 与 zabbix-snmp 监控
Unlock 2 live broadcast themes in advance! Today, I will teach you how to complete software package integration Issues 29-30
随机推荐
ACTF 2022圆满落幕,0ops战队二连冠!!
驼峰式与下划线命名规则(Camel case With hungarian notation)
Reptiles have a good time. Are you full? These three bottom lines must not be touched!
About static type, dynamic type, ID, instancetype
Black Horse - - Redis Chapter
tensorflow和torch代码验证cuda是否安装成功
[depth first search] Ji suanke: Square
Modulenotfounderror: no module named 'PIL' solution
Don't miss this underestimated movie because of controversy!
GCC【7】- 编译检查的是函数的声明,链接检查的是函数的定义bug
AIRIOT物联网平台赋能集装箱行业构建【焊接工位信息监控系统】
R language uses DT function to generate t-distribution density function data and plot function to visualize t-distribution density function data
Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion
The nearest library of Qinglong panel
今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
[translation] a GPU approach to particle physics
R language ggplot2 visual time series histogram: visual time series histogram through two-color gradient color matching color theme
R语言使用order函数对dataframe数据进行排序、基于单个字段(变量)进行降序排序(DESCENDING)
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the grouped dot strip plot, and set the add parameter to add box plots for different levels of dot strip