- Vue.js 2 Web Development Projects
- Guillaume Chau
- 305字
- 2021-07-02 22:34:37
The animation
Now, let's move on to the animation itself. Like we did for the castle banners, we will use the TWEEN.js library:
- First, create a new startAnimation method that calculates a random animation duration and accepts a delay parameter:
methods: {
// ...
startAnimation (delay = 0) {
const vm = this
// Element width
const width = this.$el.clientWidth
// Random animation duration
const { min, max } = cloudAnimationDurations
const animationDuration = Math.random() * (max - min) + min
// Bing faster clouds forward
this.style.zIndex = Math.round(max - animationDuration)
// Animation will be there
},
}
The faster a cloud is, the lower its animation duration will be. Faster clouds will be displayed before slower clouds, thanks to the z-index CSS property.
- Inside the startAnimation method, calculate a random vertical position for the cloud and then create a Tween object. It will animate the horizontal position with a delay and set the position of the cloud each time it updates. When it completes, we will start another animation with a random delay:
// Random position
const top = Math.random() * (window.innerHeight * 0.3)
new TWEEN.Tween({ value: -width })
.to({ value: window.innerWidth }, animationDuration)
.delay(delay)
.onUpdate(function () {
vm.setPosition(this.value, top)
})
.onComplete(() => {
// With a random delay
this.startAnimation(Math.random() * 10000)
})
.start()
- In the mounted hook of the component, call the startAnimation method to begin the initial animation (with a random delay):
mounted () {
// We start the animation with a negative delay
// So it begins midway
this.startAnimation(-Math.random() *
cloudAnimationDurations.min)
},
Our cloud component is ready.
- Add some clouds to the main template in the world element:
<p class="clouds">
<cloud v-for="index in 10" :type="(index - 1) % 5 + 1" />
</p>
Be careful to pass a value to the type prop ranging from 1 to 5. Here, we use the % operator to return the pision remainder for 5.
Here is what it should look like:

推薦閱讀
- 高手是如何做產(chǎn)品設(shè)計(jì)的(全2冊(cè))
- GeoServer Cookbook
- 認(rèn)識(shí)編程:以Python語(yǔ)言講透編程的本質(zhì)
- Web開(kāi)發(fā)的貴族:ASP.NET 3.5+SQL Server 2008
- Mastering openFrameworks:Creative Coding Demystified
- Python深度學(xué)習(xí)原理、算法與案例
- C++寶典
- Service Mesh實(shí)戰(zhàn):基于Linkerd和Kubernetes的微服務(wù)實(shí)踐
- Learning Modular Java Programming
- Django 3.0入門與實(shí)踐
- JSP程序設(shè)計(jì)實(shí)例教程(第2版)
- Getting Started with Polymer
- 并行編程方法與優(yōu)化實(shí)踐
- Mastering Bootstrap 4
- SQL Server 2008實(shí)用教程(第3版)