このコンポーネントは、クライアントにロードされた画像をフェードインして作成しました。私はVueイベントを使用するのと同じように、これを解決するVueのような方法があると思うが、見つけられない。イメージが読み込まれたときに検出するVueの方法は何ですか?Vueを使用して画像をフェードインする方法
https://codepen.io/kslstn/pen/ooaPGW
Vue.component('imageThatFadesInOnLoad',{
data: function(){
return {
src: 'http://via.placeholder.com/350x150',
loaded: false,
}
},
mounted: function() {
var image = new Image()
var that = this
this.loaded = image.addEventListener('load', function(){that.onLoaded()}) // This is the key part: it is basically vanilla JS
image.src = this.src
},
methods:{
onLoaded(){
this.loaded = true
}
},
template: `
<div class="wrapper">
<transition name="fade">
<img class="icon" v-bind:src="src" v-if="loaded">
</transition>
</div>
`
})
new Vue({
el: '#wrapper'
});
.wrapper{
width: 350px;
height: 150px;
background: slategrey;
}
.fade-enter-active {
transition: opacity 3s ease-in-out;
}
.fade-enter-to{
opacity: 1;
}
.fade-enter{
opacity: 0;
}
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<div id="wrapper">
<image-that-fades-in-on-load></image-that-fades-in-on-load>
</div>
'@負荷=' "onLoaded"? (または 'v-on:load =" onLoaded ")https://jsfiddle.net/vcL986Lx/ – yuriy636
それはどういうわけかテンプレートでは動かない:https://codepen.io/kslstn/pen/dZgqNW v-onのドキュメントを見つけることができません:load、それはおそらく、Vueアプリケーションの読み込みが開始されたときに呼び出されますか?その時点でテンプレートはまだレンダリングされませんでした。 – kslstn