js缓动效果
作者:互联网
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
ul {
width: 1200px;
text-align: center;
display: inline-flex;
margin-top: 200px;
}
li {
list-style: none;
width: 40px;
height: 60px;
background: skyblue;
color: #fff;
float: left;
display: flex;
justify-content: center;
align-items: center;
transition: transform .2s;
transition: width .6s;
}
li.active {
background: red;
width: 100px;
}
</style>
</head>
<body>
<script>
eventUntil = {
addEvent: function (element, type, handler) {
if (element.addEventListener) {
//事件类型、需要执行的函数、是否捕捉
element.addEventListener(type, handler, false);
} else if (element.attachEvent) {
element.attachEvent('on' + type, function () {
handler.call(element);
});
} else {
element['on' + type] = handler;
}
}
};
class MusiceJuip {
juip_el() {
function tiao() {
return new Promise((resolve, reject) => {
let add_el = {
parent_el: document.querySelector('body'),
ul_el: document.createElement('ul'),
num: 10,
juip_num: -10,
init_num: 0,
juipTime : 700,
creat_el: function () {
const _THIS_ = this;
_THIS_.parent_el.append(_THIS_.ul_el);
for (let i = 1; i <= _THIS_.num; i++) {
let li_el = document.createElement('li');
li_el.innerText = i;
_THIS_.ul_el.append(li_el);
}
// 循环跳动
let ul_childnodes = document.querySelector('ul').getElementsByTagName('li');
for (let i = 0; i < ul_childnodes.length; i++) {
eventUntil.addEvent(ul_childnodes[i], 'click', clickJuip);
function clickJuip() {
for (let i = 0; i < ul_childnodes.length; i++) {
ul_childnodes[i].style.transform = `translate(${_THIS_.init_num},${_THIS_.init_num}px)`;
ul_childnodes[i].className = "";
}
this.style.transform = `translate(${_THIS_.init_num},${_THIS_.juip_num}px)`;
this.className = "active";
}
}
let index = -1;
function time_juip() {
index++;
index == 10 ? index = -1 : (
new function () {
for (let i = 0; i < ul_childnodes.length; i++) {
ul_childnodes[i].style.transform = `translate(${_THIS_.init_num},${_THIS_.init_num}px)`;
ul_childnodes[i].className = "";
}
switch (index) {
case index:
ul_childnodes[index].style.transform = `translate(${_THIS_.init_num},${_THIS_.juip_num * (index + 2)}px)`;
ul_childnodes[index].className = "active";
break;
}
}
)
}
setInterval(time_juip, this.juipTime);
}
}
add_el.creat_el();
document.querySelector('ul') ? resolve() : reject();
})
}
async function Jus() {
await tiao().then(() => {
console.log('程序运行成功');
}).catch(() => {
console.log('程序运行失败!');
})
}
Jus();
}
}
const JUIP = new MusiceJuip();
JUIP.juip_el();
</script>
</body>
</html>
标签:function,el,效果,handler,element,width,缓动,js,type 来源: https://blog.csdn.net/qq_38161537/article/details/94990662