banner.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <template>
  2. <div id="index-banner">
  3. <div
  4. class="swiper swiperBox"
  5. v-swiper:swiper="bannerSwiperOption"
  6. :cleanup-styles-on-destroy="false"
  7. @ready="onSwiperRedied">
  8. <div class="swiper-wrapper">
  9. <div v-for="(item, index) in bannerData" :key="index" class="swiper-slide">
  10. <a :href="item.href" target="_blank">
  11. <img :src="item.bannerSrc" :alt="item.bannerAlt">
  12. </a>
  13. </div>
  14. </div>
  15. <div class="channelAdvantage btnPrev" slot="button-prev" @click="prev">
  16. <div class="el-icon-arrow-left"></div>
  17. </div>
  18. <div class="channelAdvantage btnNext" slot="button-next" @click="next">
  19. <div class="el-icon-arrow-right"></div>
  20. </div>
  21. </div>
  22. <div class="banner-swiper-pagination" slot="pagination"></div>
  23. </div>
  24. </template>
  25. <script>
  26. export default {
  27. name: "VBanner",
  28. props: {
  29. //bannerData为对象,里面的data属性包含着图片的数组
  30. bannerData: {
  31. type: Array,
  32. required: true,
  33. },
  34. },
  35. data() {
  36. return {
  37. isLeft: true,
  38. currentIndex: 0,
  39. timer: "",
  40. clickTime: 0,
  41. bannerSwiperOption: {
  42. // 配置说明直接看官网
  43. slidesPerView: "auto",
  44. paginationClickable: true,
  45. // slidesOffsetBefore: 100,
  46. // slidesOffsetAfter: 100,
  47. autoplay: {
  48. delay: 3500,
  49. disableOnInteraction: false,
  50. },
  51. speed: 1000,
  52. loop: true,
  53. observer: true,
  54. observeParents: true,
  55. autoplayDisableOnInteraction: false,
  56. notNextTick: true,
  57. // centeredSlides: true,
  58. navigation: {
  59. nextEl: ".btnNext",
  60. prevEl: ".btnPrev",
  61. },
  62. pagination: {
  63. el: ".banner-swiper-pagination",
  64. clickable: true,
  65. },
  66. },
  67. };
  68. },
  69. created() {
  70. //在DOM加载完成后,下个tick中开始轮播
  71. this.$nextTick(() => {
  72. this.timer = setInterval(() => {
  73. this.autoPlay();
  74. }, 4000);
  75. });
  76. },
  77. methods: {
  78. go() {
  79. this.timer = setInterval(() => {
  80. this.autoPlay();
  81. }, 4000);
  82. },
  83. stop() {
  84. clearInterval(this.timer);
  85. this.timer = null;
  86. },
  87. change(index) {
  88. this.currentIndex = index;
  89. },
  90. autoPlay() {
  91. this.isLeft = true;
  92. this.currentIndex++;
  93. if (this.currentIndex > this.bannerData.length - 1) {
  94. this.currentIndex = 0;
  95. }
  96. },
  97. prev() {
  98. if (new Date() - this.clickTime > 850) {
  99. this.isLeft = false;
  100. this.currentIndex--;
  101. if (this.currentIndex < 0) {
  102. this.currentIndex = this.bannerData.length - 1;
  103. }
  104. this.clickTime = new Date();
  105. }
  106. },
  107. next() {
  108. if (new Date() - this.clickTime > 850) {
  109. this.isLeft = true;
  110. this.currentIndex++;
  111. if (this.currentIndex > this.bannerData.length - 1) {
  112. this.currentIndex = 0;
  113. }
  114. this.clickTime = new Date();
  115. }
  116. },
  117. onSwiperRedied(swiper) {
  118. console.log('Swiper redied!', swiper)
  119. },
  120. },
  121. };
  122. </script>
  123. <style lang="scss">
  124. #index-banner {
  125. position: relative;
  126. overflow: hidden;
  127. width: 100%;
  128. // height: 540px;
  129. height: 595px;
  130. .swiperBox {
  131. height: 100%;
  132. &:hover {
  133. .channelAdvantage {
  134. transform: 0.3s;
  135. display: block;
  136. }
  137. }
  138. .swiper-wrapper {
  139. width: 100%;
  140. img {
  141. width: 100%;
  142. height: 100%;
  143. object-fit: cover;
  144. }
  145. }
  146. }
  147. .banner-swiper-pagination {
  148. position: absolute;
  149. left: 50%;
  150. transform: translateX(-50%);
  151. bottom: 65px;
  152. z-index: 66;
  153. .swiper-pagination-bullet {
  154. width: 12px;
  155. height: 12px;
  156. background: #FFFFFF;
  157. opacity: 0.50;
  158. margin: 0 11px;
  159. &:focus {
  160. outline: none;
  161. }
  162. &.swiper-pagination-bullet-active {
  163. width: 45px;
  164. height: 12px;
  165. background: #FFFFFF;
  166. border-radius: 100px;
  167. opacity: 1;
  168. }
  169. }
  170. }
  171. }
  172. .channelAdvantage {
  173. display: none;
  174. position: absolute;
  175. width: 84px;
  176. height: 84px;
  177. top: 50%;
  178. transform: translateY(-50%);
  179. z-index: 2;
  180. outline: 0;
  181. cursor: pointer;
  182. text-align: center;
  183. line-height: 84px;
  184. color: #FFFFFF;
  185. background: rgba(0,0,0,.2);
  186. &.btnNext {
  187. right: 0;
  188. }
  189. &.btnPrev {
  190. left: 0;
  191. }
  192. div {
  193. height: 84px;
  194. line-height: 84px;
  195. font-size: 32px;
  196. }
  197. }
  198. .i-b-container {
  199. position: relative;
  200. width: 1200px;
  201. height: 100%;
  202. margin: 0 auto;
  203. }
  204. .i-b-tab {
  205. position: absolute;
  206. left: 50%;
  207. bottom: 27px;
  208. width: 62px;
  209. height: 10px;
  210. margin-left: -30px;
  211. }
  212. .b-c-part {
  213. position: absolute;
  214. top: 0;
  215. left: 50%;
  216. overflow: hidden;
  217. width: 1920px;
  218. height: 100%;
  219. margin-left: -960px;
  220. }
  221. .b-c-part img {
  222. display: block;
  223. width: 100%;
  224. height: 100%;
  225. }
  226. .i-b-btn {
  227. position: relative;
  228. width: 1200px;
  229. margin: 0 auto;
  230. }
  231. .btn-pre {
  232. position: absolute;
  233. top: -340px;
  234. left: -155px;
  235. width: 18px;
  236. height: 21px;
  237. background: url('~assets/images/index/banner-btn-pre.png') center center no-repeat;
  238. cursor: pointer;
  239. }
  240. .btn-next {
  241. position: absolute;
  242. top: -340px;
  243. right: -155px;
  244. width: 18px;
  245. height: 21px;
  246. background: url('~assets/images/index/banner-btn-next.png') center center no-repeat;
  247. cursor: pointer;
  248. }
  249. .btn-pre:hover {
  250. background: url('~assets/images/index/banner-btn-pre-cur.png') center center no-repeat;
  251. }
  252. .btn-next:hover {
  253. background: url('~assets/images/index/banner-btn-next-cur.png') center center no-repeat;
  254. }
  255. .listNext-enter-to {
  256. transition: all 1s ease;
  257. transform: translateX(0);
  258. }
  259. .listNext-leave-active {
  260. transition: all 1s ease;
  261. transform: translateX(-100%);
  262. }
  263. .listNext-enter {
  264. transform: translateX(100%);
  265. }
  266. .listNext-leave {
  267. transform: translateX(0);
  268. }
  269. .listPrev-enter-to {
  270. transition: all 1s ease;
  271. transform: translateX(0);
  272. }
  273. .listPrev-leave-active {
  274. transition: all 1s ease;
  275. transform: translateX(100%);
  276. }
  277. .listPrev-enter {
  278. transform: translateX(-100%);
  279. }
  280. .listPrev-leave {
  281. transform: translateX(0);
  282. }
  283. </style>