
<template>
<div @mousemove="onHandleMousemove"
@mouseup="onHandleMouseend"
@mouseleave="onHandleMouseend"
class="block" ></div>
</template>
<script setup>
const onHandleMousemove = (e) => {
const {offsetX, offsetY, target} = e;
const {clientWidth, clientHeight} = target;
// 计算中点坐标
const cx = clientWidth / 2, cy = clientHeight / 2;
// 距离中心点的偏移
const offsetcy = offsetY - cy;
const offsetcx = offsetX - cx;
// 计算各轴的旋转度,按百分比
let retatex = offsetcx/cx * 15;
let retatey = -offsetcy/cy * 12;
target.style.transform = `perspective(1000px) rotateX(${retatey}deg) rotateY(${retatex}deg) scale3d(1.05, 1.05, 1.05)`;
}
const onHandleMouseend = (e) => {
const {offsetX, offsetY, target} = e;
target.style.transform = `perspective(1000px) rotateX(0deg) rotateY(0deg) scale3d(1, 1, 1)`;
}
</script>
<style scoped>
.block {
height: 160px;
width: 80%;
margin: 50px auto;
background: #004b43;
perspective: 10000px;
position: relative;
transition: all 0.1s linear;
}
.block::after {
content: "";
height: 6px;
width: 6px;
background: #fd00b8;
border-radius: 3px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>