当前位置:网站首页>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();
}
})
}
边栏推荐
- Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
- Ansible
- IO stream file
- Thinkcmf6.0安装教程
- Live online system source code, using valueanimator to achieve view zoom in and out animation effect
- Ansible
- 2022-07-06: will the following go language codes be panic? A: Meeting; B: No. package main import “C“ func main() { var ch chan struct
- Cnopendata geographical distribution data of religious places in China
- LeetCode 90:子集 II
- Pytest + allure + Jenkins Environment - - achèvement du remplissage de la fosse
猜你喜欢
探索干货篇!Apifox 建设思路
Linux server development, MySQL transaction principle analysis
Li Kou interview question 04.01 Path between nodes
[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
Asemi rectifier bridge rs210 parameters, rs210 specifications, rs210 package
Qt学习26 布局管理综合实例
2022年茶艺师(中级)考试试题及模拟考试
Linux server development, redis source code storage principle and data model
SQL优化的魅力!从 30248s 到 0.001s
[experience sharing] how to expand the cloud service icon for Visio
随机推荐
[UVM foundation] what is transaction
Pytest + allure + Jenkins Environment - - achèvement du remplissage de la fosse
Linux server development, MySQL cache strategy
Use and analysis of dot function in numpy
Sign up now | oar hacker marathon phase III, waiting for your challenge
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
2022-07-06: will the following go language codes be panic? A: Meeting; B: No. package main import “C“ func main() { var ch chan struct
pytest+allure+jenkins环境--填坑完毕
buuctf misc USB
C语言航班订票系统
Ansible
Custom class loader loads network class
Common validation comments
[performance pressure test] how to do a good job of performance pressure test?
[VHDL parallel statement execution]
[guess-ctf2019] fake compressed packets
这5个摸鱼神器太火了!程序员:知道了快删!
Thinkcmf6.0 installation tutorial
【斯坦福计网CS144项目】Lab4: TCPConnection
[advanced digital IC Verification] command query method and common command interpretation of VCs tool