当前位置:网站首页>Learning and Exploration - Seamless rotation map
Learning and Exploration - Seamless rotation map
2022-07-06 19:30:00 【miao_ zz】
Seamless carousel
design sketch
Code block
<!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, // Get the width of the visual area of the rotation chart
carouselList: document.querySelector(".carousel-list"),
arrowLeft: document.querySelector(".carousel-arrow-left"),
arrowRight: document.querySelector(".carousel-arrow-right"),
indicators: document.querySelectorAll(".indicator span"),
currentIndex: 0, // Subscript from 0 Start
}
init();
function init() {
// Copy the first picture
const first = doms.carouselList.firstElementChild.cloneNode(true);
// Copy the last picture
const last = doms.carouselList.lastElementChild.cloneNode(true);
// Put the first picture at the end
doms.carouselList.appendChild(first);
// Put the last picture on the first ,
/**
* insertBefore() Method inserts a new child node before an existing child node .
* grammar :
* insertBefore(newnode,node);
* Parameters :
* newnode: The new node to insert .
* node: Insert node before specifying this node .
* **/
doms.carouselList.insertBefore(last, doms.carouselList.firstElementChild);
// Set the width of the package carousel content block ,
//document.querySelector(".carousel-list").childElementCount, It should be placed behind the inserted node , To get the latest number of nodes
const childElementCount = document.querySelector(".carousel-list").childElementCount;
// Set the width for the outline of the carousel , It is used to wrap all the rotating blocks
doms.carouselList.style.width = doms.carouselBoxWidth * childElementCount + 'px';
doms.carouselList.style.transform = `translateX(-${doms.carouselBoxWidth}px)`;
}
// Carousel rendering
function moveTo(index) {
doms.carouselList.style.transform = `translateX(-${(index+1)*doms.carouselBoxWidth}px)`;
doms.carouselList.style.transition = '0.5s';
// Remove the selection effect of the indicator
let active = document.querySelector(".indicator span.indicator-current");
active.classList.remove("indicator-current");
// Add the selected indicator
doms.indicators[index].classList.add("indicator-current");
doms.currentIndex = index;
}
// The carousel image resets the rendering effect
function resetMoveTo(index) {
doms.carouselList.style.transform = `translateX(-${(index+1)*doms.carouselBoxWidth}px)`;
doms.carouselList.style.transition = 'none';
// Remove the selection effect of the indicator
let active = document.querySelector(".indicator span.indicator-current");
active.classList.remove("indicator-current");
// Add the selected indicator
doms.indicators[index].classList.add("indicator-current");
doms.currentIndex = index;
}
// Click the indicator
doms.indicators.forEach((item, index) => {
item.addEventListener("click", () => {
moveTo(index)
})
})
// Click the left arrow
doms.arrowLeft.addEventListener("click", () => {
const total = doms.indicators.length;
if (doms.currentIndex === 0) {
// Seamless rotation
//resetMoveTo(total - 1); Is equivalent to the following
doms.carouselList.style.transform = `translateX(-${(total)*doms.carouselBoxWidth}px)`;
doms.carouselList.style.transition = 'none';
doms.carouselList.clientHeight; // Force rendering
moveTo(total - 1)
} else {
moveTo(doms.currentIndex - 1)
}
})
// Click the right arrow
doms.arrowRight.addEventListener("click", () => {
const total = doms.indicators.length;
if (doms.currentIndex === total - 1) {
// Seamless rotation
//resetMoveTo(0); Is equivalent to the following
doms.carouselList.style.transform = `translateX(-${(0+1)*doms.carouselBoxWidth}px)`;
doms.carouselList.style.transition = 'none';
doms.carouselList.clientHeight; // Force rendering
moveTo(0)
} else {
moveTo(doms.currentIndex + 1)
}
})
</script>
</html>
边栏推荐
- Use of deg2rad and rad2deg functions in MATLAB
- The list of people who passed the fifth phase of personal ability certification assessment was published
- A method of removing text blur based on pixel repair
- 黑馬--Redis篇
- JDBC详解
- tensorflow和torch代码验证cuda是否安装成功
- 第五期个人能力认证考核通过名单公布
- Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
- Computer network: sorting out common network interview questions (I)
- 谷粒商城--分布式高级篇P129~P339(完结)
猜你喜欢
Leetcode 30. 串联所有单词的子串
中缀表达式转后缀表达式详细思路及代码实现
五金机电行业供应商智慧管理平台解决方案:优化供应链管理,带动企业业绩增长
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
安装Mysql报错:Could not create or access the registry key needed for the...
Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
[玩转Linux] [Docker] MySQL安装和配置
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
Looting iii[post sequence traversal and backtracking + dynamic planning]
随机推荐
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
1805. 字符串中不同整数的数目
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
short i =1; i=i+1与short i=1; i+=1的区别
LeetCode-1279. Traffic light intersection
凤凰架构2——访问远程服务
Vmware虚拟机无法打开内核设备“\\.\Global\vmx86“的解决方法
GCC [7] - compilation checks the declaration of functions, and link checks the definition bugs of functions
数学知识——高斯消元(初等行变换解方程组)代码实现
IC设计流程中需要使用到的文件
通俗的讲解,带你入门协程
spark基础-scala
Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
全套教学资料,阿里快手拼多多等7家大厂Android面试真题
关于图像的读取及处理等
Use of deg2rad and rad2deg functions in MATLAB
Modulenotfounderror: no module named 'PIL' solution
How can my Haskell program or library find its version number- How can my Haskell program or library find its version number?
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世