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

直接看代码:

复制代码
<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>
相关推荐
lar_slw5 小时前
k8s部署前端项目
前端·容器·kubernetes
这里不能睡觉6 小时前
js 实现 Blob、File、ArrayBuffer、base64、URL 之间互转
javascript
拉拉肥_King6 小时前
Ant Design Table 横向滚动条神秘消失?我是如何一步步找到真凶的
前端·javascript
GreenTea6 小时前
DeepSeek-V4 技术报告深度分析:基础研究创新全景
前端·人工智能·后端
河阿里6 小时前
HTML5标准完全教学手册
前端·html·html5
吴声子夜歌7 小时前
Vue3——新语法
前端·javascript·vue.js
jiayong237 小时前
第 36 课:任务详情抽屉快捷改状态
开发语言·前端·javascript·vue.js·学习
FFF_634560237 小时前
通用 vue 页面 js 下载任何文件的方法
开发语言·前端·javascript
光影少年7 小时前
中级前端需要会的东西都有那些?
前端·学习·前端框架
琢磨先生TT7 小时前
为什么很多后台系统功能不少,看起来却还是很廉价?
前端·vue.js·设计