LoginSignup
1
1

More than 5 years have passed since last update.

vue.jsでlazyload

Last updated at Posted at 2019-02-10

メモ

カスタムディレクティブでlazyloadの処理を定義する
以下を参考に
https://jp.vuejs.org/v2/cookbook/creating-custom-scroll-directives.html

import Vue from 'vue'

Vue.directive('lazyload', {
  inserted: function (el, binding) {
    let f = function (evt) {
      if (window.scrollY + window.innerHeight > el.offsetTop) {
        el.setAttribute('src', binding.value)
        window.removeEventListener('scroll', f)
      }
    }
    window.addEventListener('scroll', f)
    f()
  }
})

imgタグに設定する

<template>
  <div>
    <template v-for="src in imageList">
      <img src="~/assets/images/loading.gif"
        v-lazyload="src"
      >
    </template>
  </div>
</template>
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1