当前位置:网站首页>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>
边栏推荐
- 倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
- 关于图像的读取及处理等
- 零基础入门PolarDB-X:搭建高可用系统并联动数据大屏
- MATLAB中deg2rad和rad2deg函数的使用
- Interview assault 63: how to remove duplication in MySQL?
- 通俗的讲解,带你入门协程
- C language daily practice - day 22: Zero foundation learning dynamic planning
- USB host driver - UVC swap
- Unbalance balance (dynamic programming, DP)
- Simple understanding of MySQL database
猜你喜欢

Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!

Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars

Low CPU load and high loadavg processing method

Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry

The list of people who passed the fifth phase of personal ability certification assessment was published

今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
![Looting iii[post sequence traversal and backtracking + dynamic planning]](/img/9b/e9eeed138e46afdeed340bf2629ee1.png)
Looting iii[post sequence traversal and backtracking + dynamic planning]

数学知识——高斯消元(初等行变换解方程组)代码实现

LeetCode_双指针_中等_61. 旋转链表

10 schemes to ensure interface data security
随机推荐
测试用里hi
Tensorflow and torch code verify whether CUDA is successfully installed
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]
Take a look at how cabloyjs workflow engine implements activiti boundary events
Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars
Interview assault 63: how to remove duplication in MySQL?
USB host driver - UVC swap
[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
Using clip path to draw irregular graphics
Is not a drawable (color or path): the vector graph downloaded externally cannot be called when it is put into mipmap, and the calling error program crashes
系统性详解Redis操作Hash类型数据(带源码分析及测试结果)
Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference
MySQL information schema learning (I) -- general table
史上超级详细,想找工作的你还不看这份资料就晚了
zabbix 代理服务器 与 zabbix-snmp 监控
Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
Spark foundation -scala
Low CPU load and high loadavg processing method
ACTF 2022圆满落幕,0ops战队二连冠!!