CSS01——随鼠标3D旋转

完整代码展示

<template>
    <div class="container">
        <div class="content">

            <el-image  class="img-view" :src="img" fit="cover" 
                :style="{width: `${width}px`, height: `${height}px`, '--xdeg': `${xdeg}deg`, '--ydeg': `${ydeg}deg`}"
                @mouseenter="onMouseenter" @mousemove="onMousemove" @mouseleave="onMouseleave"></el-image>

        </div>
    </div>
</template>

<script setup>
    import { ref, onMounted } from 'vue'
    
    const img = 'https://img2.baidu.com/it/u=1624963289,2527746346&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750'

    const width = ref(250)
    const height = ref(400)
    const xdeg = ref(0)
    const ydeg = ref(0)

    /**
     * 鼠标划入事件
     */
    const onMouseenter = (e) => {
        
    }

    /**
     * 鼠标划出事件
     */
    const onMouseleave = (e) => {
        ydeg.value = 0
        xdeg.value = 0
    }

    const xMax = [-10, 10]
    const yMax = [-10, 10]
    /**
     * 鼠标滑动事件
     */
    const onMousemove = (e) => {
        ydeg.value = -(e.offsetX * (xMax[1] - xMax[0]) / width.value + xMax[0])
        xdeg.value = e.offsetY * (yMax[1] - yMax[0]) / height.value + yMax[0]
    }

</script>

<style scoped>
.container{
    width: 100%;
    height: 100%;
    border-radius: 10px;
    background-color: #000000;
}
.content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.img-view {
    border-radius: 6px;
    box-shadow: 1px 1px 7px 5px #FFFFFF44;
    cursor: pointer;
    transition: all .2s;
    transform: perspective(500px) rotateX(var(--xdeg, 0deg)) rotateY(var(--ydeg, 0deg));
}

.img-view:hover {
    transition: none;
}
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值