首頁 web前端 js教程 在vue組件中如何寫出彈射小球

在vue組件中如何寫出彈射小球

Sep 06, 2017 am 11:22 AM
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: &#39;<p class="article" :style="addStyle"></p>&#39;,
            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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

vue中怎麼用bootstrap vue中怎麼用bootstrap Apr 07, 2025 pm 11:33 PM

在 Vue.js 中使用 Bootstrap 分為五個步驟:安裝 Bootstrap。在 main.js 中導入 Bootstrap。直接在模板中使用 Bootstrap 組件。可選:自定義樣式。可選:使用插件。

vue怎麼給按鈕添加函數 vue怎麼給按鈕添加函數 Apr 08, 2025 am 08:51 AM

可以通過以下步驟為 Vue 按鈕添加函數:將 HTML 模板中的按鈕綁定到一個方法。在 Vue 實例中定義該方法並編寫函數邏輯。

vue中的watch怎麼用 vue中的watch怎麼用 Apr 07, 2025 pm 11:36 PM

Vue.js 中的 watch 選項允許開發者監聽特定數據的變化。當數據發生變化時,watch 會觸發一個回調函數,用於執行更新視圖或其他任務。其配置選項包括 immediate,用於指定是否立即執行回調,以及 deep,用於指定是否遞歸監聽對像或數組的更改。

vue多頁面開發是啥意思 vue多頁面開發是啥意思 Apr 07, 2025 pm 11:57 PM

Vue 多頁面開發是一種使用 Vue.js 框架構建應用程序的方法,其中應用程序被劃分為獨立的頁面:代碼維護性:將應用程序拆分為多個頁面可以使代碼更易於管理和維護。模塊化:每個頁面都可以作為獨立的模塊,便於重用和替換。路由簡單:頁面之間的導航可以通過簡單的路由配置來管理。 SEO 優化:每個頁面都有自己的 URL,這有助於搜索引擎優化。

vue返回上一頁的方法 vue返回上一頁的方法 Apr 07, 2025 pm 11:30 PM

Vue.js 返回上一頁有四種方法:$router.go(-1)$router.back()使用 &lt;router-link to=&quot;/&quot;&gt; 組件window.history.back(),方法選擇取決於場景。

vue.js怎麼引用js文件 vue.js怎麼引用js文件 Apr 07, 2025 pm 11:27 PM

在 Vue.js 中引用 JS 文件的方法有三種:直接使用 &lt;script&gt; 標籤指定路徑;利用 mounted() 生命週期鉤子動態導入;通過 Vuex 狀態管理庫進行導入。

vue遍歷怎麼用 vue遍歷怎麼用 Apr 07, 2025 pm 11:48 PM

Vue.js 遍歷數組和對像有三種常見方法:v-for 指令用於遍歷每個元素並渲染模板;v-bind 指令可與 v-for 一起使用,為每個元素動態設置屬性值;.map 方法可將數組元素轉換為新數組。

vue的div怎麼跳轉 vue的div怎麼跳轉 Apr 08, 2025 am 09:18 AM

Vue 中 div 元素跳轉的方法有兩種:使用 Vue Router,添加 router-link 組件。添加 @click 事件監聽器,調用 this.$router.push() 方法跳轉。

See all articles