当前位置:网站首页>Roulette chart 2 - writing of roulette chart code
Roulette chart 2 - writing of roulette chart code
2022-07-07 07:59:00 【Code machine - Ying】
<div class="banner"> </div>
<script src="./02move.js"></script>
<script>
let oBanner = document.querySelector('.banner');
const arr = [
{ id: 1, width: 500, height: 333, url: '1.jpg' },
{ id: 2, width: 500, height: 333, url: '2.jpg' },
{ id: 3, width: 500, height: 333, url: '3.jpg' },
{ id: 4, width: 500, height: 333, url: '4.jpg' },
{ id: 5, width: 500, height: 333, url: '5.jpg' }
];
let index = 1;
let oUl;
let oOl;
let oDiv;
let oUllis;
let oOllis;
let liWidth;
let time;
let res = true;
// Call the auto generate page function
setPage();
autoLoop();
mouseMove();
setClick();
hidden();
// Dynamically generate pages
function setPage() {
oUl = document.createElement('ul');
oOl = document.createElement('ol');
oDiv = document.createElement('div');
let oUlLiStr = '';
let oOlLiStr = '';
arr.forEach(function (item, key) {
oUlLiStr += `<li><img src="./images/${item.url}" alt=""></li>`;
oOlLiStr += key === 0 ? `<li class="active" num=${key} name="olli"></li>` : `<li num=${key} name='olli'></li>`
})
oUl.innerHTML = oUlLiStr;
oOl.innerHTML = oOlLiStr;
oDiv.innerHTML = `<a href="Javascript:;" name="left"><</a><a href="Javascript:;" name="right">></a>`;
oBanner.appendChild(oUl);
oBanner.appendChild(oOl);
oBanner.appendChild(oDiv);
oUllis = oUl.querySelectorAll('li');
oOllis = oOl.querySelectorAll('li');
liWidth = oUllis[0].offsetWidth;
const oFirstClone = oUllis[0].cloneNode(true);
const oLastClone = oUllis[arr.length - 1].cloneNode(true);
oUl.appendChild(oFirstClone);
oUl.insertBefore(oLastClone, oUllis[0]);
oUl.style.width = (arr.length + 2) * liWidth + 'px';
oUl.style.left = -liWidth + 'px'
}
// Auto carousel function
function autoLoop() {
time = setInterval(function () {
// Prevent clicking too fast
if (res) {
res = false;
}
else {
return;
}
index++;
setFocusStyle();
move(oUl, { left: -liWidth * index }, loopEnd)
}, 3000)
}
// At the end of the rotation function , Called function
function loopEnd() {
if (index === arr.length + 2 - 1) {
index = 1;
}else if(index===0){
index=arr.length;
}
oUl.style.left = -liWidth * index + 'px';
res = true;
}
// Change of focus style
function setFocusStyle() {
oOllis.forEach(function (item, key) {
item.classList.remove('active');
})
if (index === arr.length + 2 - 1) {
oOllis[0].classList.add('active');
}
else if (index === 0) {
oOllis[arr.length - 1].classList.add('active');
}
else {
oOllis[index - 1].classList.add('active')
}
}
// Mouse in and out
function mouseMove() {
oBanner.addEventListener('mouseenter', function () {
clearInterval(time);
});
oBanner.addEventListener('mouseleave', function () {
autoLoop();
})
}
// Add click event
function setClick() {
oBanner.addEventListener("click", function (e) {
if (e.target.getAttribute('name') === 'left') {
if (res) {
res = false;
} else {
return;
}
index--;
setFocusStyle();
move(oUl, { left: -index * liWidth }, loopEnd);
}
else if (e.target.getAttribute('name') === 'right') {
if (res) {
res = false;
} else {
return;
}
index++;
setFocusStyle();
move(oUl, { left: -index * liWidth }, loopEnd);
}
else if (e.target.getAttribute('name') === 'olli') {
if (res) {
res = false;
} else {
return;
}
index = Number(e.target.getAttribute('num'))+1;
setFocusStyle();
move(oUl, { left: -index * liWidth }, loopEnd);
}
})
}
//
function hidden(){
// to documnent Add browser display status monitoring
document.addEventListener('visibilitychange',function(){
// If the status displayed by the browser is hidden
if(document.visibilityState==='hidden'){
// Prove that the current browser hide is minimized
// Clear timer
clearInterval(time);
}
// If the browser displays a status that describes visible
else if(document.visibilityState==='visible'){
// Prove that the current browser displays
// Call the auto rotation function again
autoLoop();
}
})
}
边栏推荐
- 自定义类加载器加载网络Class
- Use and analysis of dot function in numpy
- Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)
- Linux server development, MySQL process control statement
- 2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
- [SUCTF 2019]Game
- Gslx680 touch screen driver source code analysis (gslx680. C)
- C语言通信行程卡后台系统
- Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
- 【webrtc】m98 screen和window采集
猜你喜欢
Ansible
@component(““)
Technology cloud report: from robot to Cobot, human-computer integration is creating an era
自定义类加载器加载网络Class
Asemi rectifier bridge rs210 parameters, rs210 specifications, rs210 package
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
Leetcode 90: subset II
[SUCTF 2019]Game
即刻报名|飞桨黑客马拉松第三期等你挑战
2022 recurrent training question bank and answers of refrigeration and air conditioning equipment operation
随机推荐
2022 tea master (intermediate) examination questions and mock examination
Ansible
[Stanford Jiwang cs144 project] lab4: tcpconnection
paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
C language queue
Li Kou interview question 04.01 Path between nodes
[UTCTF2020]file header
Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
Cnopendata geographical distribution data of religious places in China
Chip information website Yite Chuangxin
LeetCode 40:组合总和 II
C语言二叉树与建堆
Value sequence (subsequence contribution problem)
[P2P] local packet capturing
Pytorch parameter initialization
Visualization Document Feb 12 16:42
Linux server development, MySQL index principle and optimization
【斯坦福计网CS144项目】Lab4: TCPConnection
buuctf misc USB