console
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>图标运动</title>
<style>
body {
margin: 0;
}
div {
display: flex;
justify-content: center;
}
a{
display: block;
margin: 0 20px;
text-align: center;
}
i {
position: relative;
top: 0px;
left: 0px;
width: 60px;
height: 40px;
margin-top: 20px;
display: block;
opacity:1;
}
i img{
position: absolute;
top: 20px;
height: 30px;
margin: 0 auto;
opacity: 1;
}
</style>
<script>
window.onload = function () {
let oMove = document.getElementById("move");
let aList=oMove.getElementsByTagName('a')
for(let i=0;i<aList.length;i++){
aList[i].timer=null
aList[i].onmouseover = function () {
let i0=aList[i].getElementsByTagName('i')[0]
startMove(i0,{'top':-30,'opacity':0},function(){
i0.style.top=30+'px'
startMove(i0,{'top':0,'opacity':100})
});
}
}
}
function getStyle(obj,attr){
return getComputedStyle(obj,false)[attr];
}
function startMove(obj,json,fn) {
clearInterval(obj.timer);
obj.timer = setInterval(function () {
let flag=true;
for(let attr in json){
let icur=0;
if(attr=='opacity'){
icur=Math.round(parseFloat(getStyle(obj,attr))*100)
}
else{
icur=parseInt(getStyle(obj,attr),10)
}
let speed = (json[attr]-icur)/10;
speed=speed>0?Math.ceil(speed):Math.floor(speed)
if(attr=='opacity'){
obj.style[attr] = (icur+speed)/100
}
else{
obj.style[attr] = icur + speed + 'px'
}
if(icur!=json[attr]){
flag=false
}
else{
flag;}
}
if(flag==true){
clearInterval(obj.timer);
if(fn){
fn()
}
}
}
, 30)
}
</script>
</head>
<body>
<div id="move">
<a href="#"><i><img src="https://via.placeholder.com/35x35" alt=""></i><p>铅笔</p></a>
<a href="#"><i><img src="https://via.placeholder.com/35x35" alt=""></i><p>铅笔</p></a>
<a href="#"><i><img src="https://via.placeholder.com/35x35" alt=""></i><p>铅笔</p></a>
<a href="#"><i><img src="https://via.placeholder.com/35x35" alt=""></i><p>铅笔</p></a>
<a href="#"><i><img src="https://via.placeholder.com/35x35" alt=""></i><p>铅笔</p></a>
</div>
</body>
</html>