原生js实现的鼠标滑过图标效果,实现起来比较简单,重点就是计算放大缩小的比例,看效果
最终效果
业务逻辑分析
js
window.onload = function () {
let aInput = document.getElementsByTagName('input')
let oDiv = document.getElementById('div1')
let aImg = oDiv.getElementsByTagName('img')
document.onmousemove = function (e) {
let ev = e || window.event
for (let index = 0; index < aImg.length; index++) {
const element = aImg[index]
let x = element.offsetLeft + element.offsetWidth / 2
let y =
element.offsetTop + element.offsetHeight / 2 + oDiv.offsetTop
let a = ev.clientX - x
let b = ev.clientY - y
// debugger
let c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2))
let scale = c / 300
scale = 1 - scale <= 0.5 ? 0.5 : 1 - scale
aInput[index].value = 1 - scale
element.style.width = scale * 128 + 'px'
}
}
}
html
<div id="div1">
<img src="./images/1.png" alt="" srcset="" />
<img src="./images/2.png" alt="" srcset="" />
<img src="./images/3.png" alt="" srcset="" />
<img src="./images/4.png" alt="" srcset="" />
<img src="./images/5.png" alt="" srcset="" />
</div>
css
#div1 {
width: 100%;
position: absolute;
bottom: 0;
text-align: center;
}
#div1 {
}
img {
width: 64px;
}
```