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

直接看代码:

复制代码
<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>
相关推荐
m0_738120723 小时前
CTFshow系列——命令执行web53-56
前端·安全·web安全·网络安全·ctfshow
Liu.7745 小时前
uniappx鸿蒙适配
前端
山有木兮木有枝_6 小时前
从代码到创作:探索AI图片生成的神奇世界
前端·coze
ZXT6 小时前
js基础重点复习
javascript
言兴6 小时前
秋招面试---性能优化(良子大胃袋)
前端·javascript·面试
WebInfra7 小时前
Rspack 1.5 发布:十大新特性速览
前端·javascript·github
雾恋8 小时前
我用 trae 写了一个菜谱小程序(灶搭子)
前端·javascript·uni-app
烛阴8 小时前
TypeScript 中的 `&` 运算符:从入门、踩坑到最佳实践
前端·javascript·typescript
Java 码农9 小时前
nodejs koa留言板案例开发
前端·javascript·npm·node.js
ZhuAiQuan10 小时前
[electron]开发环境驱动识别失败
前端·javascript·electron