vue 实现一个有趣的围绕圆弧动画

25 篇文章 0 订阅

前几天在好朋友楼上小黑的介绍下,看到了某个平台的官网,里面有一个人物介绍的模块,里面的动画感觉不错,于是就自己动手写了一个。

1.0 原官网示例

在这里插入图片描述
当然这里去掉了具体信息,原网站是里面圆圈中是人物的头像,旁边是介绍信息,每个人物就沿着圆弧移动到指定位置

2.0 我们实现的结果

在这里插入图片描述
当点击中间开始时,几个小球一次转动到固定角度

3.0 简单分析下

要让小圆在圆弧上动,我们只需要知道圆心在圆弧上的坐标(x,y)就行了
在这里插入图片描述
所以当我们知道外圆的半径,小圆的半径,以及角度再利用三角函数就可以计算出 小圆在圆弧上定位的位置 top right

4.0 代码实现

这里是将 移动的小圆封装成一个组件 moveRound,只需要将
大圆半径小圆半径转动的角度,剩下的就可以按自己需要添加

<template>
  <div>
      <h3>围绕圆弧移动动画</h3>
      <div class="arc_bo" >
         <move-round :minR="25" :bigR="150" :angle="30" ref="mRound1" >	     </move-round>
          <span class="start" @click="toMove" >开始</span>
      </div>
  </div>
</template>

<script>
    import moveRound from './components/moveRound.vue'
    export default {
        name: 'arcMoveAni',
        components: {
            moveRound
        },
        methods: {
            toMove() {
                this.$refs.mRound1.toMove()
            }
        }
    }
</script>

<style scoped>
    .arc_bo{
        margin: 0 auto;
        width: 6rem;
        height: 6rem;
        border-radius: 50%;
        border: 1px solid #ccc;
        position: relative;
        margin-top: 2rem;
    }
    .start{
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        color: #f40;
        width: 1rem;
        height: 1rem;
        border-radius: 50%;
        border: 1px solid #ccc;
        text-align: center;
        line-height: 1rem;
    }
</style>

moveRound组件,
角度的动态改变计算 top,right的值,其中 raf,cafrequestAnimationFrame的兼容处理,不明白的可以看往期文章或者百度。

<template>
  <div>
      <div class="round" :style="setPosition" ></div>
  </div>
</template>

<script>
    let timer = 0
    import { raf,caf } from '../../utils/raf'
    export default {
        name: 'moveRound',
        props: {
            angle: { // 需要转动的角度
                type: Number,
                default: 0
            },
            bigR: { // 外层大圆半径
                type: Number,
                default: 0
            },
            minR: { //移动小圆半径
                type: Number,
                default: 0
            },
            backgroundColor: {
                type: String,
                default: '#7fffd4'
            }
        },
        data() {
            return {
                top: '',
                right: '',
                setAngle: 0
            }
        },
        mounted() {

        },
        computed: {
            /**
             * sin 对应 y 的值,转换为定位中距离顶部top等于 圆的半径 - y - 小圆半径(让圆心在圆弧上)
             * cos 对应 x 的值,转换为定位中距离右边right等于 圆的半径 - x - 小圆半径
             * **/ 
            setPosition( { top, right ,setAngle, bigR, minR, backgroundColor} ) {
                let size = minR*2 + 'px'

                let x = bigR * ( 1 - Math.cos(setAngle * Math.PI/180)) - minR
                let y = bigR * ( 1 - Math.sin(setAngle * Math.PI/180))  - minR

                right = x + 'px'
                top = y + 'px'
                return {
                    top,
                    right,
                    width: size,
                    height: size,
                    backgroundColor
                }
            }
        },
        methods: {
            toMove() {
            	// 调整 累加值,改变速度
                this.setAngle += 1
                timer = raf(this.toMove)
                // 结束动画
                if(this.setAngle > this.angle) {
                    caf(timer)
                    // 也可以根据需要重复执行
                    // this.setAngle = 0
                }
            }
        }
    }
</script>

<style>
 .round{
    position: absolute;
    will-change: top,right;
    border-radius: 50%;
 }

</style>

5.0 总结

主要的点就是根据角度计算位置,只要思路正确,应该可以少走弯路。

6.0 补充下 requestAnimationFrame 兼容处理


const vendors = ['webkit', 'moz']
let r = window.requestAnimationFrame
let c = window.cancelAnimationFrame
for (let x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
	// 统一前缀
	r = window[vendors[x] + 'RequestAnimationFrame']
	c =
		window[vendors[x] + 'CancelAnimationFrame'] ||
		window[vendors[x] + 'CancelRequestAnimationFrame']
}

export const raf = (function() {
	if (r) {
		return r
	} else {
		let lastTime = 0
		return function(callback, element) {
			let currTime = new Date().getTime()
			let timeToCall = Math.max(0, 16.7 - (currTime - lastTime))
			let id = window.setTimeout(function() {
				callback(currTime + timeToCall)
			}, timeToCall)
			lastTime = currTime + timeToCall
			return id
		}
	}
})()

export const caf = (function() {
	if (c) {
		return c
	} else {
		return function(id) {
			clearTimeout(id)
		}
	}
})()

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 Vue 水波纹循环动画实现: 1. 首先,我们需要在 Vue 的 `template` 中添加一个 `div` 元素,作为容器来显示水波纹动画: ```html <template> <div class="ripple-container"> <div class="ripple"></div> </div> </template> ``` 2. 然后,在 `style` 标签中添加样式,使容器和水波纹的位置、大小、颜色等属性符合我们的需求: ```css <style> .ripple-container { position: relative; width: 200px; height: 200px; border-radius: 50%; overflow: hidden; background-color: #ccc; } .ripple { position: absolute; width: 0; height: 0; margin: auto; left: 0; right: 0; top: 0; bottom: 0; background-color: rgba(255, 255, 255, 0.7); border-radius: 50%; opacity: 0.7; animation: ripple 2s linear infinite; } @keyframes ripple { from { width: 0; height: 0; opacity: 0.7; } to { width: 400px; height: 400px; opacity: 0; } } </style> ``` 3. 最后,在 Vue 的 `script` 中添加逻辑代码,使水波纹动画循环播放: ```js <script> export default { name: 'RippleAnimation', created() { setInterval(() => { const ripple = document.querySelector('.ripple'); ripple.classList.remove('ripple'); void ripple.offsetWidth; ripple.classList.add('ripple'); }, 2000); }, }; </script> ``` 以上就是一个简单的 Vue 水波纹循环动画实现。需要注意的是,为了实现动画循环播放,我们在 `created()` 生命周期中使用 `setInterval()` 定时器来不断地重复添加、移除类名,以触发动画效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值