当前位置:网站首页>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>
边栏推荐
- IC设计流程中需要使用到的文件
- Druid 数据库连接池 详解
- MRO工业品企业采购系统:如何精细化采购协同管理?想要升级的工业品企业必看!
- Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
- 反射及在运用过程中出现的IllegalAccessException异常
- Problems encountered in using RT thread component fish
- About image reading and processing, etc
- LeetCode-1279. Traffic light intersection
- 数学知识——高斯消元(初等行变换解方程组)代码实现
- [玩转Linux] [Docker] MySQL安装和配置
猜你喜欢

Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference

反射及在运用过程中出现的IllegalAccessException异常

JDBC details

Reflection and illegalaccessexception exception during application

CCNP Part 11 BGP (III) (essence)

Zero foundation entry polardb-x: build a highly available system and link the big data screen
深入分析,Android面试真题解析火爆全网
![Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting](/img/dd/c3f4a9c38b156e3a9b9adfd6253773.gif)
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting

系统性详解Redis操作Hash类型数据(带源码分析及测试结果)

JDBC详解
随机推荐
谷粒商城--分布式高级篇P129~P339(完结)
Vmware虚拟机无法打开内核设备“\\.\Global\vmx86“的解决方法
Using clip path to draw irregular graphics
思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理
Looting iii[post sequence traversal and backtracking + dynamic planning]
凤凰架构2——访问远程服务
Simple understanding of MySQL database
ModuleNotFoundError: No module named ‘PIL‘解决方法
Php+redis realizes the function of canceling orders over time
1805. 字符串中不同整数的数目
三面蚂蚁金服成功拿到offer,Android开发社招面试经验
ACTF 2022圆满落幕,0ops战队二连冠!!
GCC [7] - compilation checks the declaration of functions, and link checks the definition bugs of functions
保证接口数据安全的10种方案
潇洒郎: AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipe
Reflection and illegalaccessexception exception during application
Excel 中VBA脚本的简单应用
How to do smoke test
Leetcode topic [array] - 119 Yang Hui triangle II
In 50W, what have I done right?