写一个判断鼠标进入方向切换图片的效果

直接看代码:

<template>
    <div class="mainrouter centerWindi">
        <div ref="mouse" class="mouse" @mouseenter="handleMouse"></div>
    </div>
</template>
<script setup>
import { onMounted, ref } from 'vue';
import gsap from 'gsap'

const mouse = ref(null)
// 拿到鼠标的进入事件
// 监听鼠标进入事件
const handleMouse = (e) => {
    const theta = Math.atan2(mouse.value.offsetHeight, mouse.value.offsetWidth)
    const angle = Math.atan2(e.offsetX - mouse.value.offsetWidth / 2, mouse.value.offsetHeight / 2 - e.offsetY)
    const direction = ref('')
    console.log(theta, angle);
    if (angle < theta && angle > -theta) {
        direction.value = 'top';
        gsap.fromTo(mouse.value, {
            backgroundPositionY: `-${window.innerHeight}px`
        }, {
            backgroundPositionY: `0px`,
            duration: 0.5,
            ease: "none",//匀速
            // repeat:-1,//无限循环
            // yoyo: true,//往返运动
        })
    } else if (angle >= theta && angle < Math.PI - theta) {
        direction.value = "right";
        gsap.fromTo(mouse.value, {
            backgroundPositionX: `${window.innerHeight}px`
        }, {
            backgroundPositionX: `0px`,
            duration: 0.5,
            ease: "none",//匀速
            // repeat:-1,//无限循环
            // yoyo: true,//往返运动
        })
    } else if (angle >= Math.PI - theta || angle < -Math.PI + theta) {
        direction.value = "bottom";
        gsap.fromTo(mouse.value, {
            backgroundPositionY: `${window.innerHeight}px`
        }, {
            backgroundPositionY: `0px`,
            duration: 0.5,
            ease: "none",//匀速
            // repeat:-1,//无限循环
            // yoyo: true,//往返运动
        })
    } else {
        direction.value = "left";
        gsap.fromTo(mouse.value, {
            backgroundPositionX: `-${window.innerHeight}px`
        }, {
            backgroundPositionX: `0px`,
            duration: 0.5,
            ease: "none",//匀速
            // repeat:-1,//无限循环
            // yoyo: true,//往返运动
        })
    }

}



</script>

<style>
.mouse {
    border: 0.5px solid #000;
    background-color: red;
    width: 300px;
    height: 300px;
    background: url("./img/1.png") no-repeat;
    background-size: cover;
}
/* 路由 */
.mainrouter {
    background-color: white;
    width: 100%;
    height: 100%;
    border-radius: 4px;
    padding: 25px;
}
/* 居中 */
.centerWindi {
    @apply flex justify-center items-center;
}

</style>

 

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-18 03:26:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-18 03:26:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-18 03:26:02       87 阅读
  4. Python语言-面向对象

    2024-01-18 03:26:02       96 阅读

热门阅读

  1. redis内存淘汰机制

    2024-01-18 03:26:02       52 阅读
  2. LeetCode 36. 有效的数独

    2024-01-18 03:26:02       53 阅读
  3. pcie设备驱动无法工作排查

    2024-01-18 03:26:02       54 阅读
  4. 排序补充(C语言版)

    2024-01-18 03:26:02       38 阅读
  5. C++在结构(Struct)中使用队列(Queue)

    2024-01-18 03:26:02       55 阅读
  6. Composer安装

    2024-01-18 03:26:02       52 阅读
  7. 前端开发常用的地址

    2024-01-18 03:26:02       60 阅读
  8. 低端单片机彩色屏幕的内存占用疑惑

    2024-01-18 03:26:02       42 阅读
  9. 2024-01-16 创业日记-关于用户需求分析-思考

    2024-01-18 03:26:02       40 阅读