cardMixins.js 4.1 KB

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