- Vue.js 2 Web Development Projects
- Guillaume Chau
- 258字
- 2021-07-02 22:34:37
The animated clouds
To add some life to the game world, we will create a few clouds that will slide in the sky. Their position and animation duration will be random and they will go from the left to the right of the window.
- In the world.js file, add the minimum and maximum durations for the cloud animation:
const cloudAnimationDurations = {
min: 10000, // 10 sec
max: 50000, // 50 sec
}
- Then, create the cloud component with an image and a type prop:
Vue.component('cloud', {
template: `<p class="cloud" :class="'cloud-' + type" >
<img :src="'svg/cloud' + type + '.svg'" />
</p>`,
props: ['type'],
})
There will be five different clouds, so the type prop will range from 1 to 5.
- We will need to change the z-index and transform CSS properties of the component with a reactive style data property:
data () {
return {
style: {
transform: 'none',
zIndex: 0,
},
}
},
- Apply these style properties with the v-bind directive:
<p class="cloud" :class="'cloud-' + type" :style="style">
- Let's create a method to set the position of the cloud component using the transform CSS property:
methods: {
setPosition (left, top) {
// Use transform for better performance
this.style.transform = `translate(${left}px, ${top}px)`
},
}
- We need to initialize the horizontal position of the cloud when the image is loaded, so that it's outside of the viewport. Create a new initPosition that uses the setPosition method:
methods: {
// ...
initPosition () {
// Element width
const width = this.$el.clientWidth
this.setPosition(-width, 0)
},
}
- Add an event listener on the image with the v-on directive shorthand that listens to the load event and calls the initPosition method:
<img :src="'svg/cloud' + type + '.svg'" @load="initPosition" />
推薦閱讀
- Mastering Concurrency Programming with Java 8
- Vue 3移動Web開發與性能調優實戰
- R語言數據分析從入門到精通
- Python數據可視化:基于Bokeh的可視化繪圖
- MATLAB應用與實驗教程
- JavaScript前端開發與實例教程(微課視頻版)
- FFmpeg入門詳解:音視頻原理及應用
- Windows Server 2016 Automation with PowerShell Cookbook(Second Edition)
- SSM開發實戰教程(Spring+Spring MVC+MyBatis)
- Java圖像處理:基于OpenCV與JVM
- Java程序設計教程
- INSTANT Premium Drupal Themes
- 高性能MVVM框架的設計與實現:San
- MonoTouch應用開發實踐指南:使用C#和.NET開發iOS應用
- Python量子計算實踐:基于Qiskit和IBM Quantum Experience平臺