cardMixins.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // https://forum.vuejs.org/t/how-to-use-helper-functions-for-imported-modules-in-vuejs-vue-template/6266/5
  2. export default {
  3. directives: {
  4. lazy: {
  5. bind (figure, binding) {
  6. // console.log('directive lazy bind', figure, binding)
  7. // show only the first image
  8. if (binding.value === 0) {
  9. const img = figure.querySelector('img:not(.blank)')
  10. figure.classList.add('loading')
  11. img.addEventListener('load', function (e) {
  12. // console.log('img loaded', e)
  13. figure.classList.remove('loading')
  14. figure.classList.add('loaded')
  15. })
  16. img.addEventListener('error', function (e) {
  17. console.error('img ERROR', e)
  18. e.target.classList.remove('loading')
  19. e.target.classList.add('error')
  20. })
  21. img.setAttribute('src', img.getAttribute('data-src'))
  22. // img.removeAttribute('data-src')
  23. // img.classList.remove('lazy')
  24. }
  25. }
  26. },
  27. switcher: {
  28. inserted (el, binding) {
  29. // switch images on mousemove
  30. el.addEventListener('mousemove', function (event) {
  31. const figs = this.querySelectorAll('figure.loaded')
  32. // console.log('mousemove', figs.length, this, event)
  33. // let len = figs.length
  34. // let w = this.clientWidth;
  35. // let g = w / len;
  36. // let delta = Math.floor(event.layerX / g)
  37. // console.log(event.offsetX, this.clientWidth, figs.length)
  38. // console.log(event.offsetX / (this.clientWidth / figs.length))
  39. let delta = Math.floor(event.offsetX / (this.clientWidth / figs.length))
  40. delta = delta < 0 ? 0 : delta >= figs.length ? figs.length - 1 : delta
  41. // console.log('delta', delta)
  42. figs.forEach((fig, index) => {
  43. // console.log(index)
  44. if (index === delta) {
  45. // fig.style.display = "block"
  46. fig.classList.remove('hide')
  47. fig.classList.add('show')
  48. } else {
  49. // fig.style.display = "none"
  50. fig.classList.remove('show')
  51. fig.classList.add('hide')
  52. }
  53. })
  54. })
  55. }
  56. }
  57. },
  58. mounted () {
  59. // lazy load images on mouseover
  60. // console.log('card mounted', this.$options.name)
  61. // if (this.$options.name ==! 'ModalCard') {
  62. this.activateLazyLoad()
  63. // }
  64. },
  65. updated () {
  66. // lazy load images on mouseover
  67. // console.log('card updated', this.$options.name)
  68. // if (this.$options.name ==! 'ModalCard') {
  69. this.activateLazyLoad()
  70. // }
  71. },
  72. methods: {
  73. // used by ModalCard
  74. activateLazyLoad () {
  75. // console.log('card activateLazyLoad', this.$options.name)
  76. this.$el.addEventListener('mouseover', function (event) {
  77. const figures = this.querySelectorAll('.images figure.lazy:not(.loaded):not(.loading)')
  78. console.log('mouseover', this, figures)
  79. figures.forEach((figure, index) => {
  80. const img = figure.querySelector('img:not(.blank)')
  81. // console.log('forEach img',img)
  82. img.classList.add('loading')
  83. img.addEventListener('load', function (e) {
  84. // console.log('img loaded', e)
  85. figure.classList.remove('loading')
  86. figure.classList.add('loaded')
  87. })
  88. img.addEventListener('error', function (e) {
  89. console.error('img ERROR', figure, e)
  90. })
  91. const src = img.getAttribute('data-src')
  92. // for debug purpose
  93. console.log(figures.length, index)
  94. // let src = img.getAttribute('data-src')
  95. // if (index > 0 && index < figures.length - 1) {
  96. // src = img.getAttribute('data-src').replace('?', '/bad?')
  97. // }
  98. img.setAttribute('src', src)
  99. // img.removeAttribute('data-src')
  100. // img.classList.remove('lazy')
  101. })
  102. }, { once: true })
  103. }
  104. }
  105. }