cardMixins.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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', this, event, figs.length)
  33. // let len = figs.length
  34. // let w = this.clientWidth;
  35. // let g = w / len;
  36. // let delta = Math.floor(event.layerX / g)
  37. let delta = Math.floor(event.layerX / (this.clientWidth / figs.length))
  38. delta = delta < 0 ? 0 : delta >= figs.length ? figs.length - 1 : delta
  39. console.log('delta', delta)
  40. figs.forEach((fig, index) => {
  41. console.log(index)
  42. if (index === delta) {
  43. // fig.style.display = "block"
  44. fig.classList.remove('hide')
  45. fig.classList.add('show')
  46. } else {
  47. // fig.style.display = "none"
  48. fig.classList.remove('show')
  49. fig.classList.add('hide')
  50. }
  51. })
  52. })
  53. }
  54. }
  55. },
  56. mounted () {
  57. // lazy load images on mouseover
  58. console.log('card mounted', this.$options.name)
  59. // if (this.$options.name ==! 'ModalCard') {
  60. this.activateLazyLoad()
  61. // }
  62. },
  63. updated () {
  64. // lazy load images on mouseover
  65. console.log('card updated', this.$options.name)
  66. // if (this.$options.name ==! 'ModalCard') {
  67. this.activateLazyLoad()
  68. // }
  69. },
  70. methods: {
  71. // used by ModalCard
  72. activateLazyLoad () {
  73. console.log('card activateLazyLoad', this.$options.name)
  74. this.$el.addEventListener('mouseover', function (event) {
  75. const figures = this.querySelectorAll('.images figure.lazy:not(.loaded):not(.loading)')
  76. console.log('mouseover', this, figures)
  77. figures.forEach((figure, index) => {
  78. const img = figure.querySelector('img:not(.blank)')
  79. // console.log('forEach img',img)
  80. img.classList.add('loading')
  81. img.addEventListener('load', function (e) {
  82. // console.log('img loaded', e)
  83. figure.classList.remove('loading')
  84. figure.classList.add('loaded')
  85. })
  86. img.addEventListener('error', function (e) {
  87. console.error('img ERROR', figure, e)
  88. })
  89. const src = img.getAttribute('data-src')
  90. // for debug purpose
  91. console.log(figures.length, index)
  92. // let src = img.getAttribute('data-src')
  93. // if (index > 0 && index < figures.length - 1) {
  94. // src = img.getAttribute('data-src').replace('?', '/bad?')
  95. // }
  96. img.setAttribute('src', src)
  97. // img.removeAttribute('data-src')
  98. // img.classList.remove('lazy')
  99. })
  100. }, { once: true })
  101. }
  102. }
  103. }