Card.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <div class="card">
  3. <header>
  4. <h1>{{ item.title }}</h1>
  5. <h4>{{ item.description }}</h4>
  6. </header>
  7. <section class="images" v-switcher>
  8. <figure
  9. v-for="(img, index) in item.images"
  10. :key="img.url"
  11. >
  12. <img
  13. class="lazy"
  14. v-lazy="index"
  15. :data-src="img.url"
  16. :title="img.title"
  17. />
  18. <img class="blank" :src="blanksrc">
  19. </figure>
  20. </section>
  21. </div>
  22. </template>
  23. <script>
  24. export default {
  25. name: "Card",
  26. props: ['item'],
  27. data() {
  28. return {
  29. blanksrc:`${drupalSettings.path.themePath}/assets/img/blank.gif`
  30. }
  31. },
  32. directives: {
  33. lazy: {
  34. bind(img,binding){
  35. // console.log('lazy bind', img, binding);
  36. if(binding.value === 0){
  37. img.setAttribute('src', img.getAttribute('data-src'))
  38. img.removeAttribute('data-src')
  39. img.classList.remove('lazy')
  40. }
  41. }
  42. },
  43. switcher: {
  44. inserted(el,binding){
  45. // switch images on mousemove
  46. el.addEventListener('mousemove', function(event) {
  47. let figs = this.querySelectorAll('figure')
  48. // console.log('mousemove', this, event, figs.length);
  49. // let len = figs.length
  50. // let w = this.clientWidth;
  51. // let g = w / len;
  52. // let delta = Math.floor(event.layerX / g)
  53. let delta = Math.floor(event.layerX / (this.clientWidth / figs.length))
  54. // console.log('delta', delta);
  55. figs.forEach((fig, index) => {
  56. // console.log(index);
  57. if(index == delta){
  58. fig.style.display = "block"
  59. }else{
  60. fig.style.display = "none"
  61. }
  62. })
  63. })
  64. }
  65. }
  66. },
  67. mounted() {
  68. // lazy load images on mouseover
  69. this.$el.addEventListener('mouseover', function(event) {
  70. let imgs = this.querySelectorAll('.images figure img.lazy')
  71. // console.log('mouseover', this, imgs);
  72. imgs.forEach((img) => {
  73. // console.log('forEach img',img);
  74. img.setAttribute('src', img.getAttribute('data-src'))
  75. img.removeAttribute('data-src')
  76. img.classList.remove('lazy')
  77. })
  78. }, {once : true})
  79. }
  80. }
  81. </script>
  82. <style lang="scss" scoped>
  83. </style>