当前位置:网站首页>JS rotation chart
JS rotation chart
2022-07-01 04:21:00 【MyDreamingCode】
One 、 Carousel chart style + structure
1. structure ( Left and right arrows 、 Small circle 、 Rotation picture area )
<div class="banner">
<a href="javascript:;" class="arrow_pre arrow"></a>
<a href="javascript:;" class="arrow_next arrow"></a>
<ul class="lunbo">
<li>
<a href="#">
<img src="./uploads/banner1.jpg">
</a>
</li>
<li>
<a href="#">
<img src="./uploads/banner2.jpg">
</a>
</li>
<li>
<a href="#">
<img src="./uploads/banner3.jpg">
</a>
</li>
<li>
<a href="#">
<img src="./uploads/banner4.jpg">
</a>
</li>
</ul>
<ul class="dot"></ul>
</div>2. style
* {
padding: 0;
margin: 0;
}
ul {
list-style: none;
}
.banner {
position: relative;
width: 721px;
height: 455px;
margin: 50px auto;
overflow: hidden;
}
.banner img {
width: 100%;
height: 100%;
}
.arrow {
position: absolute;
top: 50%;
width: 20px;
height: 32px;
transform: translateY(-50%);
z-index: 1;
display: none;
}
.arrow_pre {
left: 0;
background: url('./img/arrow-prev.png') no-repeat;
}
.arrow_next {
right: 0;
background: url('./img/arrow-next.png') no-repeat;
}
.dot {
position: absolute;
left: 50%;
bottom: 10px;
height: 15px;
padding: 3px;
transform: translateX(-50%);
background-color: rgba(0, 0, 0, .3);
}
.dot li {
float: left;
width: 15px;
height: 15px;
margin: 0 5px;
border-radius: 50%;
background-color: #ccc;
cursor: pointer;
}
.dot li.current {
background-color: #fff;
}
.lunbo {
position: absolute;
top: 0;
left: 0;
width: 400%;
}
.lunbo li {
float: left;
}3. Dynamic generation of small circles
var length = document.querySelector('.lunbo').children.length;
var dot = document.querySelector('.dot');
for(var i = 0; i < length; i++) {
var li = document.createElement('li');
dot.appendChild(li);
li.addEventListener('click', function() {
for(var i = 0; i < dot.children.length; i++) {
dot.children[i].className = '';
}
this.className = 'current';
})
}
dot.children[0].className = 'current';Two 、 operation
1. When the mouse passes the carousel , Show left and right arrow buttons ; When leaving the carousel , Hide the left and right arrow buttons .
window.addEventListener('load', function() {
var banner = document.querySelector('.banner');
var arrow_pre = document.querySelector('.arrow_pre');
var arrow_next = document.querySelector('.arrow_next');
banner.addEventListener('mouseenter', function() {
arrow_pre.style.display = 'block';
arrow_next.style.display = 'block';
})
banner.addEventListener('mouseleave', function() {
arrow_pre.style.display = 'none';
arrow_next.style.display = 'none';
})
})2. introduce animate.js Slow motion animation encapsulation function
function animate(obj, destination, callback) {
clearInterval(obj.timer || null);
obj.timer = setInterval(function(){
var step = (destination - obj.offsetLeft) / 10;
step = step > 0 ? Math.ceil(step) : Math.floor(step);
if(obj.offsetLeft == destination) {
if(callback) {
callback();
}
clearInterval(obj.timer);
}
obj.style.left = obj.offsetLeft + step + 'px';
}, 10)
}3. ul The moving formula of the rotation area :dot Reference no. × Picture width
var lunbo = document.querySelector('.lunbo');
var length = document.querySelector('.lunbo').children.length;
var dot = document.querySelector('.dot');
for(var i = 0; i < length; i++) {
var li = document.createElement('li');
li.setAttribute('data-index',i);
dot.appendChild(li);
li.addEventListener('click', function() {
for(var i = 0; i < dot.children.length; i++) {
dot.children[i].className = '';
}
this.className = 'current';
animate(lunbo, -banner.offsetWidth * this.dataset.index);
})
}
dot.children[0].className = 'current';边栏推荐
- Libevent Library Learning
- Web server: how to choose a good web server these five aspects should be paid attention to
- TASK04|数理统计
- [untitled]
- JMeter learning notes 2 - brief introduction to graphical interface
- 2. Use of classlist (element class name)
- 【无标题】
- 跳槽一次涨8k,5年跳了3次...
- Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
- Unity之三维空间多点箭头导航
猜你喜欢

"Target detection" + "visual understanding" realizes the understanding of the input image

NFT: start NFT royalty journey with eip-2981
![[leetcode skimming] February summary (updating)](/img/62/0d0d9f11434e49d33754a2e4f2ea65.jpg)
[leetcode skimming] February summary (updating)

2022年上海市安全员C证考试题模拟考试题库及答案
【历史上的今天】6 月 30 日:冯·诺依曼发表第一份草案;九十年代末的半导体大战;CBS 收购 CNET
![[ta- frost wolf \u may- hundred people plan] 2.2 model and material space](/img/93/95ee3d4389ffd227559dc8b3207e1d.png)
[ta- frost wolf \u may- hundred people plan] 2.2 model and material space

MySQL advanced -- you will have a new understanding of MySQL

206.反转链表

Coinbase in a bear market: losses, layoffs, stock price plunges

HoloLens2开发环境搭建及部署app
随机推荐
网站服务器:好用的网站服务器怎么选这五方面要关注
283.移动零
CUDA development and debugging tool
206. reverse linked list
Ospfb notes - five messages [ultra detailed] [Hello message, DD message, LSR message, LSU message, lsack message]
使用WinMTR软件简单分析跟踪检测网络路由情况
OSPF notes [dr and bdr]
It's settled! 2022 JD cloud summit of JD global technology Explorer conference see you in Beijing on July 13
Why is Hong Kong server most suitable for overseas website construction
陈宇(Aqua)-安全-&gt;云安全-&gt;多云安全
[learn C and fly] S1E20: two dimensional array
浏览器顶部loading(来自知乎)
JMeter学习笔记2-图形界面简单介绍
Procurement intelligence is about to break out, and Alipay'3+2'system helps enterprises build core competitive advantages
[untitled]
CF1638E colorful operations
有效的 @SuppressWarnings 警告名称
HoloLens2开发环境搭建及部署app
Haskell lightweight threads overhead and use on multicores
NFT: utilisez EIP - 2981 pour commencer un voyage de redevances NFT