当前位置: 首页 > news >正文

vue实现小球掉落

首先,将小球儿动画代码封装成组件,创建个文件,例如qiu.js

let createBall = (left, top,box) => {
    // 点击事件 const {clientX,clienty} = ev  createBall(clientX,clienty)
    const ball = document.createElement('div');
    ball.style.position = 'absolute';
    ball.style.left = left - 2+ 'px';
    ball.style.top = top - 5 + 'px';
    ball.style.width = '10px';
    ball.style.height = '10px';
    ball.style.borderRadius = '50%';
    ball.style.backgroundColor = 'red';
    ball.style.transition = 'left .6s linear, top .6s cubic-bezier(0.5,-0.5,1,1)';
    document.body.appendChild(ball);
    // 使用 requestAnimationFrame 替代 setTimeout
    requestAnimationFrame(() => {
        ball.style.left = box.getBoundingClientRect().left + box.offsetWidth / 2 + 'px';
        ball.style.top = box.getBoundingClientRect().top + 'px';
    });
    ball.ontransitionend = function () {
        ball.remove();
    };
}
 
//传入两个参数分别为:当前点击的元素、和小球飞入到盒子的元素
const Qiu = (e, Box) => {
    const { clientX, clientY } = e;
    createBall(clientX, clientY, Box)
}
 
export default Qiu;  //导出
接下来就是引入js文件,在使用的vue文件中使用

元素部分

<template>
      <div className='xq-all'>
        <!-- //触发小球儿掉落按钮 -->
      <button className='xq-btn' @click="ev=>onchufa(ev)">点击触发</button> 
      <div className='xq-bottom'>
        <!-- //小球掉落的位置 -->
        <p ref="refs">小球儿</p> 
      </div>
    </div>
</template>

逻辑部分

<script setup>
import {ref} from 'vue'
import Qiu from './qiu.js'  //引入组件
const refs=ref()
    const onchufa=(ev)=>{
        // const { clientX, clientY } = ev;
        Qiu(ev,refs.value) //在此时用组件中的方法
    }

</script>

样式部分

<style lang="less" scoped>
.xq-all{
    .xq-btn{
        float: right;
        margin: 50px 0;
    }
    .xq-bottom{
        height: 45px;
        line-height: 45px;
        width: 100vw;
        // background-color: bisque;
        position: fixed;
        bottom: 200px;
        left: 200px;
        p{
            height: 40px;
            width: 40px;
            background-color: aquamarine;
            margin-left: 30px;
            font-size: 12px;
            position: fixed;
        }
    }
}
</style>

效果展示:

*************

相关文章:

  • 【Git】查看凭据管理器的账号信息,并删除账号,解决首次认证登录失败后无法重新登录的问题
  • 网络传输(TCP)
  • 微信小程序:flex常用布局
  • Windows pip install -r requirement.txt 太慢
  • 【grpc】利用protobuf实现java或kotlin调用python脚本,含实现过程和全部代码
  • MongoDB索引详解
  • 【C】void指针(通用指针)
  • 蚁群算法解决旅行商问题的完整Python实现
  • 谷歌浏览器安装不在默认安装位置Selenium无法打开解决方法
  • Machine Interrupt Registers
  • 响应式编程Reactor API大全(中)
  • Nacos:发现微服务的未来
  • mongo统计数据库和集合大小
  • google drive api
  • 半Happy的一天
  • Android AAudio
  • 计算机网络-2021期末考试解析
  • Python 基础【八】--数据类型-字典【2024.1.11】
  • 【leetcode】力扣热门之回文链表【简单难度】
  • 【Spring】ApplicationRunner
  • 马上评|重病老人取款身亡,如何避免类似悲剧?
  • 上海老字号卖黄金,与动漫IP联名两周销售额近亿元
  • 中方是否计划解除或调整稀土出口管制?外交部回应
  • “多规合一”改革7年成效如何?自然资源部总规划师亮成绩单
  • 一船明月过沧州:为何这座城敢称“文武双全”?
  • 李强:把做强国内大循环作为推动经济行稳致远的战略之举