在vue组件中如何写出弹射小球
1. 定义每个弹射的小球组件( ocicle )
2. 组件message自定义属性存放小球初始信息(可修改)
{top: "0px", //小球距离上方坐标 left: "0px", //小球距离左边坐标 speedX: 12, //小球每次水平移动距离 speedY: 6 //小球每次垂直移动距离 }
3. 思路
3.1 定时器设置小球每一帧移动
3.2 初始方向:isXtrue为true则小球为横坐标正方向;
isYtrue为true则小球为纵坐标正方向
3.3 每次移动之前获取小球当前坐标(oleft,otop),当前坐标加上移动距离为下一帧坐标
3.4 边界判断:横轴坐标范围超过最大值则加号变减号
4. vue知识点
4.1 父子组件传递信息使用props
4.2 模板编译之前获取el宽高
beforeMount: function (){ this.elWidth=this.$el.clientWidth; this.elHeight=this.$el.clientHeight; }
4.3 子组件获取el宽高 ( this.$root.elWidth,this.$root.elHeight )
4.4 模板编译完成后更新子组件信息
mounted: function (){ //根据父组件信息更新小球数据 this.addStyle.top=this.message.top; this.addStyle.left=this.message.left; this.speedX=this.message.speedX; this.speedY=this.message.speedY; //小球初始坐标 this.oleft=parseInt(this.addStyle.left); this.otop=parseInt(this.addStyle.top); this.move(); }
5. 代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> html, body{ padding: 0; margin: 0; width: 100%; height: 100%; } #app{ width: 800px; height: 500px; margin: 50px auto; outline: 1px solid #f69; position: relative; } </style> </head> <body> <p id="app"> <ocicle :message="message1"></ocicle> <ocicle :message="message2"></ocicle> <ocicle :message="message3"></ocicle> </p> <script src="https://unpkg.com/vue"></script> <script> var tem={ props: ["message"], template: '<p class="article" :style="addStyle"></p>', data: function (){ return { //初始化小球样式 addStyle: { width: "10px", height: "10px", backgroundColor: "#000", position: "absolute", marginTop: "-5px", marginLeft: "-5px", borderRadius: "50%", top: "0px", left: "0px"}, //横坐标方向的速度 speedX: 0, //纵坐标方向的速度 speedY: 0, //isX为真,则在横坐标方向为正 isX: true, //isY为真,则在纵坐标方向为正 isY: true, //小球当前坐标 oleft: 0, otop: 0 } }, mounted: function (){ //根据父组件信息更新小球数据 this.addStyle.top=this.message.top; this.addStyle.left=this.message.left; this.speedX=this.message.speedX; this.speedY=this.message.speedY; //小球初始坐标 this.oleft=parseInt(this.addStyle.left); this.otop=parseInt(this.addStyle.top); this.move(); }, methods: { move: function (){ var self=this; setInterval(function (){ //更新小球坐标 self.oleft=parseInt(self.addStyle.left); self.otop=parseInt(self.addStyle.top); self.isXtrue(); self.isYtrue(); }, 20); }, //判断横坐标 isXtrue: function (){ //true 横坐标正方向 //false 横坐标负方向 if(this.isX){ this.addStyle.left=this.oleft+this.speedX+"px"; //宽度超过最大边界 if(this.oleft>this.$root.elWidth-5){ this.addStyle.left=this.oleft-this.speedX+"px"; this.isX=false; } }else{ this.addStyle.left=this.oleft-this.speedX+"px"; //宽度超过最小边界 if(this.oleft<5){ this.addStyle.left=this.oleft+this.speedX+"px"; this.isX=true; } } }, // 判断纵坐标 isYtrue: function (){ //true 纵坐标正方向 //false 纵坐标负方向 if(this.isY){ this.addStyle.top=this.otop+this.speedY+"px"; //高度超过最大边界 if(this.otop>this.$root.elHeight-5){ this.addStyle.top=this.otop-this.speedY+"px"; this.isY=false; } }else{ this.addStyle.top=this.otop-this.speedY+"px"; //高度超过最小边界 if(this.otop<5){ this.addStyle.top=this.otop+this.speedY+"px"; this.isY=true; } } } } } var vm=new Vue({ el: "#app", data: { //获取el节点宽高 elWidth: 0, elHeight: 0, //设置小球初始信息 message1: { top: "0px", left: "600px", speedX: 12, speedY: 6 }, message2: { top: "0px", left: "300px", speedX: 8, speedY: 6 }, message3: { top: "300px", left: "0px", speedX: 13, speedY: 5 } }, //更新el节点宽高 beforeMount: function (){ this.elWidth=this.$el.clientWidth; this.elHeight=this.$el.clientHeight; }, components: { "ocicle": tem } }) </script> </body> </html>
以上是在vue组件中如何写出弹射小球 的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

可以通过以下步骤为 Vue 按钮添加函数:将 HTML 模板中的按钮绑定到一个方法。在 Vue 实例中定义该方法并编写函数逻辑。

在 Vue.js 中使用 Bootstrap 分为五个步骤:安装 Bootstrap。在 main.js 中导入 Bootstrap。直接在模板中使用 Bootstrap 组件。可选:自定义样式。可选:使用插件。

在 Vue.js 中引用 JS 文件的方法有三种:直接使用 <script> 标签指定路径;利用 mounted() 生命周期钩子动态导入;通过 Vuex 状态管理库进行导入。

Vue.js 中的 watch 选项允许开发者监听特定数据的变化。当数据发生变化时,watch 会触发一个回调函数,用于执行更新视图或其他任务。其配置选项包括 immediate,用于指定是否立即执行回调,以及 deep,用于指定是否递归监听对象或数组的更改。

Vue.js 返回上一页有四种方法:$router.go(-1)$router.back()使用 <router-link to="/"> 组件window.history.back(),方法选择取决于场景。

Vue 多页面开发是一种使用 Vue.js 框架构建应用程序的方法,其中应用程序被划分为独立的页面:代码维护性:将应用程序拆分为多个页面可以使代码更易于管理和维护。模块化:每个页面都可以作为独立的模块,便于重用和替换。路由简单:页面之间的导航可以通过简单的路由配置来管理。SEO 优化:每个页面都有自己的 URL,这有助于搜索引擎优化。

可以通过以下方法查询 Vue 版本:使用 Vue Devtools 在浏览器的控制台中查看“Vue”选项卡。使用 npm 运行“npm list -g vue”命令。在 package.json 文件的“dependencies”对象中查找 Vue 项。对于 Vue CLI 项目,运行“vue --version”命令。检查 HTML 文件中引用 Vue 文件的 <script> 标签中的版本信息。

Vue 中的函数截流是一种技术,用于限制函数在指定时间段内被调用的次数,防止性能问题。实现方法为:导入 lodash 库:import { debounce } from 'lodash';使用 debounce 函数创建截流函数:const debouncedFunction = debounce(() => { / 逻辑 / }, 500);调用截流函数,控制函数在 500 毫秒内最多被调用一次。
