Vue 旋转动画效果

前言

    这里记录一个旋转动画,在鼠标经过的时候停止,鼠标离开继续旋转。

    实现思路: 利用@keyframes关键字定义一个旋转动画

效果演示

在这里插入图片描述

具体代码实现

<template>
    <div class="container">
        <div class="box"  @mouseenter=" stopRotate = true " @mouseleave=" stopRotate = false " >
            
            <!-- 旋转图片-->
            <img alt="Vue logo" class="rotate-icon"  :class="{'paused': stopRotate === true}" src="./assets/logo.svg" />
        </div>
    </div>
</template>

<script>
export default {
    name: 'App',
    data(){
        return{
            stopRotate:false //控制是否旋转
        }
    }
}
</script>

<style lang="scss" scoped>
.container{
    width:100vw;
    height:100vh;
    position:relative;
    background-color:#000000;

    .box{
        width: 48px;
        height: 48px;
        background: #ffffff;
        border-radius: 50%;
        position:absolute;
        left:50%;
        top:30%;
        transform:translateX(-50%);
        display: flex;
        justify-content:center;
        align-items: center;

        &:hover{
            cursor: pointer;
        }

        .rotate-icon{
            width: 24px;
            height: 24px;
            //旋转动画
            animation: rotate 3s linear infinite;
        }

        @keyframes rotate {  
            from {  
                transform: rotate(0deg);  
            }  
            to {  
                transform: rotate(360deg);  
            }  
        }
        
        // 控制动画暂停
        .paused {  
            animation-play-state: paused;  
        } 

    }
}
</style>

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 中实现 3D 旋转动画效果,可以使用 CSS3 的 `transform` 属性,结合 Vue 的过渡动画实现。 首先,需要在组件的样式中添加 `transform-style: preserve-3d;` 属性,以开启 3D 空间的支持。然后,通过对 `transform` 属性的设置,实现元素的旋转、平移等变换。 下面是一个简单的示例代码: ```html <template> <div class="cube-container"> <div class="cube" :style="{ transform: `rotateX(${rotateX}deg) rotateY(${rotateY}deg)` }" > <div class="face front">Front</div> <div class="face back">Back</div> <div class="face left">Left</div> <div class="face right">Right</div> <div class="face top">Top</div> <div class="face bottom">Bottom</div> </div> </div> </template> <script> export default { data() { return { rotateX: 0, rotateY: 0, }; }, methods: { handleMousemove(e) { const centerX = this.$el.offsetWidth / 2; const centerY = this.$el.offsetHeight / 2; const moveX = (e.clientX - centerX) / centerX; const moveY = (centerY - e.clientY) / centerY; this.rotateX = moveY * 30; this.rotateY = moveX * 30; }, }, }; </script> <style scoped> .cube-container { perspective: 1000px; } .cube { width: 200px; height: 200px; position: relative; transform-style: preserve-3d; transition: transform 0.5s ease; } .cube .face { position: absolute; width: 200px; height: 200px; padding: 20px; background-color: rgba(255, 255, 255, 0.8); line-height: 160px; text-align: center; font-size: 40px; font-weight: bold; box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.2); } .cube .front { transform: translateZ(100px); } .cube .back { transform: translateZ(-100px) rotateY(180deg); } .cube .left { transform: translateX(-100px) rotateY(-90deg); } .cube .right { transform: translateX(100px) rotateY(90deg); } .cube .top { transform: translateY(-100px) rotateX(90deg); } .cube .bottom { transform: translateY(100px) rotateX(-90deg); } </style> ``` 在上面的代码中,我们创建了一个 3D 立方体,并通过 `rotateX` 和 `rotateY` 控制立方体的旋转角度。在 `handleMousemove` 方法中,我们监听鼠标移动事件,并计算出鼠标的相对位置,用来调整旋转角度。 需要注意的是,我们给立方体容器设置了 `transition` 属性,以实现平滑的过渡效果。同时,我们也可以在过渡过程中,添加其他的 CSS 属性变化,以实现更加绚丽的效果。 这只是一个简单的示例,你可以根据自己的需求,调整代码实现更加复杂的 3D 动画效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值