banner.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <div id="index-banner">
  3. <div class="i-b-container">
  4. <transition-group tag="div" class="part-box" name="listNext" v-show="isLeft">
  5. <div
  6. class="b-c-part"
  7. v-for="(list,index) in bannerData"
  8. :key="list.bannerId"
  9. v-show="index === currentIndex"
  10. @mouseenter="stop"
  11. @mouseleave="go"
  12. >
  13. <a :href="list.href" target="_blank" :rel="list.ref">
  14. <img :src="list.bannerSrc" :alt="list.bannerAlt">
  15. </a>
  16. </div>
  17. </transition-group>
  18. <transition-group tag="div" class="part-box" name="listPrev" v-show="!isLeft">
  19. <div
  20. class="b-c-part"
  21. v-for="(list,index) in bannerData"
  22. :key="list.bannerId"
  23. v-show="index === currentIndex"
  24. @mouseenter="stop"
  25. @mouseleave="go"
  26. >
  27. <a :href="list.href" target="_blank" :rel="list.ref">
  28. <img :src="list.bannerSrc" :alt="list.bannerAlt">
  29. </a>
  30. </div>
  31. </transition-group>
  32. </div>
  33. <div class="i-b-tab">
  34. <ul>
  35. <li
  36. v-for="(item,index) in bannerData"
  37. :key="index"
  38. :class="{active: index === currentIndex}"
  39. @click="change(index)"
  40. ></li>
  41. </ul>
  42. </div>
  43. <div class="i-b-btn">
  44. <div class="btn-pre" @click="prev" @mouseenter="stop" @mouseleave="go"></div>
  45. <div class="btn-next" @click="next" @mouseenter="stop" @mouseleave="go"></div>
  46. </div>
  47. </div>
  48. </template>
  49. <script>
  50. export default {
  51. name: "VBanner",
  52. props: {
  53. //bannerData为对象,里面的data属性包含着图片的数组
  54. bannerData: {
  55. type: Array,
  56. required: true,
  57. },
  58. },
  59. data() {
  60. return {
  61. isLeft: true,
  62. currentIndex: 0,
  63. timer: "",
  64. clickTime: 0,
  65. };
  66. },
  67. created() {
  68. //在DOM加载完成后,下个tick中开始轮播
  69. this.$nextTick(() => {
  70. this.timer = setInterval(() => {
  71. this.autoPlay();
  72. }, 4000);
  73. });
  74. },
  75. methods: {
  76. go() {
  77. this.timer = setInterval(() => {
  78. this.autoPlay();
  79. }, 4000);
  80. },
  81. stop() {
  82. clearInterval(this.timer);
  83. this.timer = null;
  84. },
  85. change(index) {
  86. this.currentIndex = index;
  87. },
  88. autoPlay() {
  89. this.isLeft = true;
  90. this.currentIndex++;
  91. if (this.currentIndex > this.bannerData.length - 1) {
  92. this.currentIndex = 0;
  93. }
  94. },
  95. prev() {
  96. if (new Date() - this.clickTime > 850) {
  97. this.isLeft = false;
  98. this.currentIndex--;
  99. if (this.currentIndex < 0) {
  100. this.currentIndex = this.bannerData.length - 1;
  101. }
  102. this.clickTime = new Date();
  103. }
  104. },
  105. next() {
  106. if (new Date() - this.clickTime > 850) {
  107. this.isLeft = true;
  108. this.currentIndex++;
  109. if (this.currentIndex > this.bannerData.length - 1) {
  110. this.currentIndex = 0;
  111. }
  112. this.clickTime = new Date();
  113. }
  114. },
  115. },
  116. };
  117. </script>
  118. <style scoped>
  119. #index-banner {
  120. position: relative;
  121. overflow: hidden;
  122. width: 100%;
  123. height: 700px;
  124. }
  125. .i-b-container {
  126. position: relative;
  127. width: 1200px;
  128. height: 100%;
  129. margin: 0 auto;
  130. }
  131. .i-b-tab {
  132. position: absolute;
  133. left: 50%;
  134. bottom: 27px;
  135. width: 62px;
  136. height: 10px;
  137. margin-left: -30px;
  138. }
  139. .b-c-part {
  140. position: absolute;
  141. top: 0;
  142. left: 50%;
  143. overflow: hidden;
  144. width: 1920px;
  145. height: 100%;
  146. margin-left: -960px;
  147. }
  148. .b-c-part img {
  149. display: block;
  150. width: 100%;
  151. height: 100%;
  152. }
  153. .i-b-btn {
  154. position: relative;
  155. width: 1200px;
  156. margin: 0 auto;
  157. }
  158. .btn-pre {
  159. position: absolute;
  160. top: -340px;
  161. left: -155px;
  162. width: 18px;
  163. height: 21px;
  164. background: url('~assets/images/index/banner-btn-pre.png') center center no-repeat;
  165. cursor: pointer;
  166. }
  167. .btn-next {
  168. position: absolute;
  169. top: -340px;
  170. right: -155px;
  171. width: 18px;
  172. height: 21px;
  173. background: url('~assets/images/index/banner-btn-next.png') center center no-repeat;
  174. cursor: pointer;
  175. }
  176. .btn-pre:hover {
  177. background: url('~assets/images/index/banner-btn-pre-cur.png') center center no-repeat;
  178. }
  179. .btn-next:hover {
  180. background: url('~assets/images/index/banner-btn-next-cur.png') center center no-repeat;
  181. }
  182. .listNext-enter-to {
  183. transition: all 1s ease;
  184. transform: translateX(0);
  185. }
  186. .listNext-leave-active {
  187. transition: all 1s ease;
  188. transform: translateX(-100%);
  189. }
  190. .listNext-enter {
  191. transform: translateX(100%);
  192. }
  193. .listNext-leave {
  194. transform: translateX(0);
  195. }
  196. .listPrev-enter-to {
  197. transition: all 1s ease;
  198. transform: translateX(0);
  199. }
  200. .listPrev-leave-active {
  201. transition: all 1s ease;
  202. transform: translateX(100%);
  203. }
  204. .listPrev-enter {
  205. transform: translateX(-100%);
  206. }
  207. .listPrev-leave {
  208. transform: translateX(0);
  209. }
  210. </style>