官术网_书友最值得收藏!

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.

  1. 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
}
  1. 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.

  1. 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,
},
}
},
  1. Apply these style properties with the v-bind directive:
      <p class="cloud" :class="'cloud-' + type" :style="style">
  1. 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)`
},
}
  1. 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)
},
}
  1. 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" />
主站蜘蛛池模板: 碌曲县| 隆化县| 边坝县| 凉城县| 隆化县| 宾阳县| 盐城市| 手机| 华宁县| 许昌市| 财经| 临朐县| 阜新市| 平昌县| 托里县| 甘南县| 余干县| 澳门| 和政县| 巫山县| 巢湖市| 儋州市| 博爱县| 吐鲁番市| 信阳市| 固安县| 富源县| 平原县| 永胜县| 怀远县| 确山县| 灵川县| 台前县| 湖南省| 九寨沟县| 梁河县| 静宁县| 肥乡县| 滨海县| 浮山县| 西吉县|