达拉~ 2022-12-05 15:55 采纳率: 64.7%
浏览 20
已结题

onmouseover频繁触发问题

看到阿里云官网的东晓是使用雪碧图做的

img


于是想着也写一个,把图片复制来后开始写,思路就是利用onmouseover和out事件,然后使用setInterval累计他的backgroundPositionY属性,


```html
<!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>Document</title>
</head>
<style>
    .box {
        width: 60px;
        height: 60px;
        overflow: hidden;
        background-image: url('./deo.png');
        background-repeat: no-repeat;
        background-size: 100%;
        /* background-position-y: 0px; */
    }
</style>

<body>
    <div class="box"></div>
    <div style="background-color: red;">
        11
    </div>
</body>
<script>
    let box = document.querySelector('.box')
    // 防抖
    const debounce = (fn, time) => {
        let timeout = null
        return function () {
            clearTimeout(timeout)
            timeout = setTimeout(() => {
                fn.apply(this, arguments)
            }, time);
        }
    }
    let y = 60
    // let num = 1
    box.addEventListener('mouseover', () => {
        let num = 1
        let a = setInterval(() => {
            if (num === 20) {
                clearInterval(a);
            } else {
                num++
            }
            box.style.backgroundPositionY = -y * num + 'px';
        }, 40)
    })
    box.addEventListener('mouseout', () => {
        let num = 1
        let a = setInterval(() => {
            num++
            box.style.backgroundPositionY = -1200 + (y * num) + 'px';
            if (num == 20) {
                clearInterval(a);
            }
            console.log(num)
        }, 40)
    })
</script>

</html>

```
代码这里,但是现在有个问题,就是mouseover事件如果频繁触发的话效果会出问题,不知道有没有什么好的方法,防抖也没什么用

  • 写回答

2条回答 默认 最新

  • 全栈小5 全栈领域优质创作者 2022-12-05 16:06
    关注

    1、mouseover改为mouseenter试下
    1)mouseover和mouseout
    2)mouseenter和mouseleave

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月13日
  • 创建了问题 12月5日

悬赏问题

  • ¥15 提问一个关于vscode相关的环境配置问题,就是输入中文但是显示不出来,代码在idea可以显示中文,但在vscode不行,不知道怎么配置环境
  • ¥15 netcore使用PuppeteerSharp截图
  • ¥20 这张图页脚具体代码该怎么写?
  • ¥20 WPF MVVM模式 handycontrol 框架, hc:SearchBar 控件 Text="{Binding NavMenusKeyWords}" 绑定取不到值
  • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂
  • ¥15 数字信号处理考试111
  • ¥15 allegro17.2生成bom表是空白的
  • ¥15 请问一下怎么打通CAN通讯
  • ¥20 如何在 rocky9.4 部署 CDH6.3.2?
  • ¥35 navicat将excel中的数据导入mysql出错