Wie fade-in Bildern, wenn geladen mit Vue

Habe ich diese Komponente, die fades in ein Bild, sobald es geladen wird an den client. Ich würde denken, es ist eine Vue-wie Weg, dieses Problem zu lösen, wie mit Vue events, konnten ihn aber nicht finden. Was ist die Vue Möglichkeit zu erkennen, wenn ein Bild geladen ist?

https://codepen.io/kslstn/pen/ooaPGW

JS:

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">&nbsp;
      </transition>
   </div>
  `
})

new Vue({
  el: '#wrapper'
});

CSS:

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

HTML:

<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>

  • @load="onLoaded"? (oder v-on:load="onLoaded") jsfiddle.net/vcL986Lx
  • Irgendwie funktioniert nicht im template: codepen.io/kslstn/pen/dZgqNW ich kann nicht finden die Dokumentation auf v-on:laden, vielleicht ist es aber genannt, wenn das Vue-app beginnt zu laden? Damals war die Vorlage wäre nicht fertig Rendern noch.
InformationsquelleAutor kslstn | 2017-11-28
Schreibe einen Kommentar