curry-slide.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <view class="carousel-3d-slide" :style="slideStyle" :class="computedClasses" @click="goTo()">
  3. <slot :index="index" :isCurrent="isCurrent" :leftIndex="leftIndex" :rightIndex="rightIndex"/>
  4. </view>
  5. </template>
  6. <script>
  7. /* eslint-disable */
  8. export default {
  9. name: 'curry-slide',
  10. props: {
  11. index: {
  12. type: Number
  13. }
  14. },
  15. data () {
  16. return {
  17. parent: this.$parent,
  18. styles: {},
  19. zIndex: 999
  20. }
  21. },
  22. computed: {
  23. isCurrent () {
  24. return this.index === this.parent.currentIndex
  25. },
  26. leftIndex () {
  27. return this.getSideIndex(this.parent.leftIndices)
  28. },
  29. rightIndex () {
  30. return this.getSideIndex(this.parent.rightIndices)
  31. },
  32. slideStyle () {
  33. let styles = {}
  34. if (!this.isCurrent) {
  35. const rIndex = this.leftIndex
  36. const lIndex = this.rightIndex
  37. if (rIndex >= 0 || lIndex >= 0) {
  38. styles = rIndex >= 0 ? this.calculatePosition(rIndex, true, this.zIndex) : this.calculatePosition(lIndex, false, this.zIndex)
  39. styles.opacity = 1
  40. styles.visibility = 'visible'
  41. }
  42. if (this.parent.hasHiddenSlides) {
  43. if (this.matchIndex(this.parent.leftOutIndex)) {
  44. styles = this.calculatePosition(this.parent.leftIndices.length - 1, false, this.zIndex)
  45. } else if (this.matchIndex(this.parent.rightOutIndex)) {
  46. styles = this.calculatePosition(this.parent.rightIndices.length - 1, true, this.zIndex)
  47. }
  48. }
  49. }
  50. return Object.assign(styles, {
  51. 'border-width': this.parent.border + 'px',
  52. 'width': this.parent.slideWidth + 'px',
  53. 'height': this.parent.slideHeight + 'px',
  54. 'transition': ' transform ' + this.parent.animationSpeed + 'ms, ' +
  55. ' opacity ' + this.parent.animationSpeed + 'ms, ' +
  56. ' visibility ' + this.parent.animationSpeed + 'ms'
  57. })
  58. },
  59. computedClasses () {
  60. return {
  61. [`left-${this.leftIndex + 1}`]: this.leftIndex >= 0,
  62. [`right-${this.rightIndex + 1}`]: this.rightIndex >= 0,
  63. 'current': this.isCurrent
  64. }
  65. }
  66. },
  67. methods: {
  68. getSideIndex (array) {
  69. let index = -1
  70. array.forEach((pos, i) => {
  71. if (this.matchIndex(pos)) {
  72. index = i
  73. }
  74. })
  75. return index
  76. },
  77. matchIndex (index) {
  78. return (index >= 0) ? this.index === index : (this.parent.total + index) === this.index
  79. },
  80. calculatePosition (i, positive, zIndex) {
  81. const z = !this.parent.disable3d ? parseInt(this.parent.inverseScaling) + ((i + 1) * 100) : 0
  82. const y = !this.parent.disable3d ? parseInt(this.parent.perspective) : 0
  83. const leftRemain = (this.parent.space === 'auto')
  84. ? parseInt((i + 1) * (this.parent.width / 1.2), 10) // 1.5
  85. : parseInt((i + 1) * (this.parent.space), 10)
  86. const transform = (positive)
  87. ? 'translateX(' + (leftRemain) + 'px) translateZ(-' + z + 'px) ' +
  88. 'rotateY(-' + y + 'deg)'
  89. : 'translateX(-' + (leftRemain) + 'px) translateZ(-' + z + 'px) ' +
  90. 'rotateY(' + y + 'deg)'
  91. const top = this.parent.space === 'auto' ? 0 : parseInt((i + 1) * (this.parent.space))
  92. return {
  93. transform: transform,
  94. top: top,
  95. zIndex: zIndex - (Math.abs(i) + 1)
  96. }
  97. },
  98. goTo () {
  99. if (!this.isCurrent) {
  100. if (this.parent.clickable === true) {
  101. this.parent.goFar(this.index)
  102. }
  103. } else {
  104. this.parent.onMainSlideClick()
  105. }
  106. }
  107. }
  108. }
  109. </script>
  110. <style>
  111. .carousel-3d-slide {
  112. position: absolute;
  113. opacity: 0;
  114. visibility: hidden;
  115. overflow: hidden;
  116. top: 0;
  117. border-color: #023c41;
  118. border-style: solid;
  119. background-size: cover;
  120. background-color: #ccc;
  121. display: block;
  122. margin: 0;
  123. box-sizing: border-box;
  124. }
  125. .carousel-3d-slide {
  126. text-align: left;
  127. }
  128. .carousel-3d-slide img {
  129. width: 100%;
  130. }
  131. .carousel-3d-slide.current {
  132. opacity: 1 !important;
  133. visibility: visible !important;
  134. transform: none !important;
  135. z-index: 999;
  136. }
  137. </style>