当前位置:网站首页>Two methods of rotation chart and automatic rotation
Two methods of rotation chart and automatic rotation
2022-07-19 05:12:00 【Create splendid -- hand in hand with you】
There are four methods in the rotation chart , In this issue, I will first share two , The remaining two methods will be added in the next period
Common part :
* {
padding: 0px;
margin: 0px;
}
.banner {
width: 600px;
margin: auto;
border: 10px solid green;
height: 350px;
position: relative;
/* overflow: hidden; */
}
.imgList img {
width: 600px;
height: 350px;
}
.imgList {
position: absolute;
}
.imgList li {
float: left;
margin-right: 20px;
list-style: none;
}
.circle {
position: absolute;
bottom: 15px;
left: 50%;
transform: translateX(-50%);
}
.circle a {
width: 15px;
height: 15px;
background: green;
display: block;
border-radius: 50%;
opacity: .8;
float: left;
margin-right: 10px;
}
.circle a.hover {
background: black;
opacity: .7;
}
.hover{
background-color:red;
}HTML The content part :
<div class="banner">
<ul class="imgList">
<li><img src="banner/1.png" /></li>
<li><img src="banner/2.jpg" /></li>
<li><img src="banner/3.jpg" /></li>
<li><img src="banner/4.jpg" /></li>
</ul>
<div class="circle">
</div>
</div>The first way js Content :
var imgList = document.querySelector('.imgList');
var banner = document.querySelector('.banner');
var circle = document.querySelector('.circle');
// Get the length of the carousel picture The width of each picture is 620px And assign the overall width value to ul;
window.οnlοad=function(){
imgList.style.width = imgList.children.length * 620 + "px";
// adopt for loop , according to ul The length of the sub tag , Create the corresponding a Number of labels ;
for (var i = 0; i < imgList.children.length; i++) {
var aNub = document.createElement('a');
// Created above a After label , We will build a Label placement circle In turn
circle.appendChild(aNub);
aNub.setAttribute('index', i);
}
circle.addEventListener('click',function(e){
var thisindex =e.target.getAttribute('index');
imgList.style.left=-thisindex*620+"px";
console.log(e.target.nodeName);
if(e.target.nodeName!='A'){
// return false namely Exit operation
return false;
}
// thisIndex = e.target.getAttribute('index');
// Call the function of changing the style of small dots
// circlechange();
})
}
The second way is js Content :
var imgList = document.querySelector('.imgList');
var banner = document.querySelector('.banner');
var circle = document.querySelector('.circle');
var circleA = circle.children;
var thisIndex = 0;
// Get the length of the carousel picture The width of each picture is 620px And assign the overall width value to ul;
window.οnlοad=function(){
imgList.style.width = imgList.children.length * 620 + "px";
// adopt for loop , according to ul The length of the sub tag , Create the corresponding a Number of labels ;
for (var i = 0; i < imgList.children.length; i++) {
var aNub = document.createElement('a');
// Created above a After label , We will build a Label placement circle In turn
circle.appendChild(aNub);
aNub.setAttribute('index', i);
let thisIndex=aNub.getAttribute('index');
aNub.οnclick=function(){
imgList.style.left=-thisIndex*620+"px";
}
// In order to create and write later , So let's create another one index attribute
// console.log(index);
}
Automatic rotation part :
css, And HTML The content is synchronized with the previous ;
js Call content :
function animation(obj, target, callback) {
clearInterval(obj.timer);// eliminate obj Of timer Method , Prevent animation from overlapping
obj.timer = setInterval(function () { // Add timer Method , Reduce memory consumption obj Global object , The method added to it will not be destroyed as the callback function ends
var step = (target - obj.offsetLeft) / 10;// One action is done in ten times , So there will be a process Slow down
//step There will be problems when it is not an integer , Take the value upward when moving forward , Value down when retreating , Move closer to the higher absolute value
step = step > 0 ? Math.ceil(step) : Math.floor(step);
if (obj.offsetLeft == target) {// Forward and backward share judgment sentences ==
clearInterval(obj.timer);
/* if (callback) {// Judge whether there are callback function parameters passed
callback();// call
} */
// Logic and computation
callback && callback();
}
else {// Not writing will affect if Judge
// The step size is a fixed value : uniform ;
// obj.style.left = obj.offsetLeft + 1 + 'px';
// Slow motion animation , Algorithm : step =( The target - The status value )/ 10( It can be selected randomly )
obj.style.left = obj.offsetLeft + step + 'px';
}
}, 15)
}js Use the content section :
var imgList = document.querySelector('.imgList');
var circle = document.querySelector('.circle');
var circleA = circle.children;
var thisIndex = 0;
var banner = document.querySelector('.banner');
// Get viewport
var banWidth = banner.offsetWidth;
window.onload = function() {
imgList.style.width = imgList.children.length * 620 + 'px';
for (var i = 0; i < imgList.children.length; i++) {
var aNode = document.createElement('a');
circle.appendChild(aNode);
aNode.setAttribute('index', i);
}
var num = 0;
circle.addEventListener('click', function(e) {
if (e.target.nodeName != 'A') {
return false;
}
thisIndex = e.target.getAttribute('index');
imgList.style.left = -thisIndex * 620 + 'px';
circlechange();
})
// Timer part of automatic rotation ;
setInterval(function(e) {
if (num == imgList.children.length - 1) {
// Restore initial
imgList.style.left = 0 + 'px';
num = 0;
}
num++;
// Add animation effect
animation(imgList, -num * banWidth);
},2000)
function circlechange() {
// Clear style first Add a style
for (var i = 0; i < circleA.length; i++) {
circleA[i].className = '';
}
// default setting 0 Position style
circleA[thisIndex].className = 'hover';
}
}边栏推荐
猜你喜欢

User management - restrictions

Teddy Cup title a full version optimization update (4/23)

mysql数据库实验实训5,数据查询yggl数据库查询(详细)

哨兵二号轨道数据下载

IText modify PDF Text
![Database training 7 [index and creation of data integrity constraints]](/img/7d/2855d945c0d7ffb970634451b600a1.png)
Database training 7 [index and creation of data integrity constraints]

SQL statement learning

Fanoutexchange switch is simple to use

使用Echars实现水滴状、环形图、分割图、堆叠、组织架构图、地图轮廓等图表

实习项目1-个性化主页配置
随机推荐
ArcGIS Pro发布服务
实习项目2-主页配置-我的数据模块
实习项目3-更改所有者
PyGame installation -requirement already satisfied
Class object automatic injection attribute operation tool
读论文《SNUNet-CD: A Densely Connected Siamese Network for Change Detection of VHR Images》
Word2Vec原理及应用与文章相似度(推荐系统方法)
Feature extraction of machine learning (digitization and discretization of category features and digitization of text features)
【C语言—零基础第七课】顺序结构与选择结构
H5如何获取内网IP和公网IP
(精讲)Es6 剩余参数,ES6内置对象,模板字符串内容(详例宝典)及灵活运用项目的实战案例
Uniapp uses uview to realize folding panel
mysql数据库实验实训5,数据查询yggl数据库查询(详细)
User login - and create SMS verification code
Getting started with harmonios
Harmonyos fourth training notes
cookie是否有效时间限定?如何设置cookie?手把手教你设置
学习C语言第三天
租用服务器,以及部署在pycharm专业版上的pytorch环境训练yolov5模型教程服务器环境安装库文件:
Simply and quickly establish a pytorch environment yolov5 target detection model to run (super simple)