SwipeCard.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <!-- -->
  2. <template>
  3. <div
  4. :class="`cardCom ${keyval}`"
  5. v-touch:swipe.left="moveSwiper"
  6. v-touch:swipe.right="moveSwiper"
  7. >
  8. <div
  9. v-for="(_, index) in cardData"
  10. :key="index"
  11. class="card"
  12. :style="`top:${index * 15}px;width:${100 - index * 5}%;opacity:${
  13. 1 - index * 0.2 <= 0 ? 0.1 : 1 - index * 0.2
  14. }; z-index: ${cardData.length - index};`"
  15. >
  16. <template v-if="index === 0">
  17. <slot :item="cardData[cardInd]" />
  18. <span class="page">{{ cardInd + 1 }} / {{ cardData.length }}</span>
  19. </template>
  20. </div>
  21. </div>
  22. </template>
  23. <script>
  24. export default {
  25. name: "cardCom",
  26. props: {
  27. cardData: {
  28. type: Array,
  29. default: () => [],
  30. },
  31. keyval: {
  32. type: String,
  33. default: "",
  34. },
  35. },
  36. components: {},
  37. data() {
  38. //这里存放数据
  39. return {
  40. cardInd: 0,
  41. };
  42. },
  43. //监听属性 类似于data概念
  44. computed: {},
  45. //监控data中的数据变化
  46. watch: {},
  47. //方法集合
  48. methods: {
  49. // 封装一个滑动的方法
  50. moveSwiper(val) {
  51. let dom = document.querySelector(`.${this.keyval}`);
  52. if (!dom) return;
  53. dom.style.opacity = 0;
  54. setTimeout(() => {
  55. if (val === "right") {
  56. // 右滑减小
  57. if (this.cardInd === 0) this.cardInd = this.cardData.length - 1;
  58. else this.cardInd--;
  59. } else {
  60. //左滑增加
  61. if (this.cardInd < this.cardData.length - 1) this.cardInd++;
  62. else this.cardInd = 0;
  63. }
  64. dom.style.opacity = 1;
  65. }, 300);
  66. },
  67. },
  68. //生命周期 - 创建完成(可以访问当前this实例)
  69. created() {},
  70. //生命周期 - 挂载完成(可以访问DOM元素)
  71. mounted() {
  72. // 动态控制cardCom元素的高度
  73. this.$nextTick(() => {
  74. setTimeout(() => {
  75. let dom = document.querySelector(`.${this.keyval}`);
  76. dom.style.height = 535 + (this.cardData.length - 1) * 15 + "px";
  77. }, 300);
  78. });
  79. },
  80. beforeCreate() {}, //生命周期 - 创建之前
  81. beforeMount() {}, //生命周期 - 挂载之前
  82. beforeUpdate() {}, //生命周期 - 更新之前
  83. updated() {}, //生命周期 - 更新之后
  84. beforeDestroy() {}, //生命周期 - 销毁之前
  85. destroyed() {}, //生命周期 - 销毁完成
  86. activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
  87. };
  88. </script>
  89. <style lang="scss" scoped>
  90. .txt::-webkit-scrollbar {
  91. /*滚动条整体样式*/
  92. width: 8px; /*高宽分别对应横竖滚动条的尺寸*/
  93. height: 2px;
  94. }
  95. .txt::-webkit-scrollbar-thumb {
  96. /*滚动条里面小方块*/
  97. -webkit-box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.2);
  98. background: #d9cdb6;
  99. }
  100. .txt::-webkit-scrollbar-track {
  101. /*滚动条里面轨道*/
  102. width: 0px;
  103. background: transparent;
  104. }
  105. .cardCom {
  106. margin-top: 80px;
  107. width: 100%;
  108. position: relative;
  109. transition: all 0.3s;
  110. }
  111. .card {
  112. background-color: #fff;
  113. box-shadow: 0px 2px 8px 6px #ccc;
  114. position: absolute;
  115. top: 0;
  116. left: 50%;
  117. transform: translateX(-50%);
  118. width: 100%;
  119. margin: 0 auto;
  120. display: flex;
  121. flex-direction: column;
  122. .page {
  123. opacity: 0.3;
  124. color: #b79550;
  125. position: absolute;
  126. font-weight: 700;
  127. right: 10px;
  128. bottom: 10px;
  129. font-size: 32px;
  130. }
  131. }
  132. </style>