banner.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. autoPlay: false,
  52. speed: 1000,
  53. loop: true,
  54. observer: true,
  55. observeParents: true,
  56. autoplayDisableOnInteraction: false,
  57. notNextTick: true,
  58. // centeredSlides: true,
  59. navigation: {
  60. nextEl: ".btnNext",
  61. prevEl: ".btnPrev",
  62. },
  63. pagination: {
  64. el: ".banner-swiper-pagination",
  65. clickable: true,
  66. },
  67. },
  68. };
  69. },
  70. created() {
  71. //在DOM加载完成后,下个tick中开始轮播
  72. this.$nextTick(() => {
  73. this.timer = setInterval(() => {
  74. this.autoPlay();
  75. }, 4000);
  76. });
  77. },
  78. methods: {
  79. go() {
  80. this.timer = setInterval(() => {
  81. this.autoPlay();
  82. }, 4000);
  83. },
  84. stop() {
  85. clearInterval(this.timer);
  86. this.timer = null;
  87. },
  88. change(index) {
  89. this.currentIndex = index;
  90. },
  91. autoPlay() {
  92. this.isLeft = true;
  93. this.currentIndex++;
  94. if (this.currentIndex > this.bannerData.length - 1) {
  95. this.currentIndex = 0;
  96. }
  97. },
  98. prev() {
  99. if (new Date() - this.clickTime > 850) {
  100. this.isLeft = false;
  101. this.currentIndex--;
  102. if (this.currentIndex < 0) {
  103. this.currentIndex = this.bannerData.length - 1;
  104. }
  105. this.clickTime = new Date();
  106. }
  107. },
  108. next() {
  109. if (new Date() - this.clickTime > 850) {
  110. this.isLeft = true;
  111. this.currentIndex++;
  112. if (this.currentIndex > this.bannerData.length - 1) {
  113. this.currentIndex = 0;
  114. }
  115. this.clickTime = new Date();
  116. }
  117. },
  118. onSwiperRedied(swiper) {
  119. // console.log('Swiper redied!', swiper)
  120. },
  121. },
  122. };
  123. </script>
  124. <style lang="scss">
  125. #index-banner {
  126. position: relative;
  127. overflow: hidden;
  128. width: 100%;
  129. // height: 540px;
  130. // height: 595px;
  131. max-height: 850px;
  132. .swiperBox {
  133. height: 100%;
  134. &:hover {
  135. .channelAdvantage {
  136. transform: 0.3s;
  137. display: block;
  138. }
  139. }
  140. .swiper-wrapper {
  141. width: 100%;
  142. img {
  143. width: 100%;
  144. height: 100%;
  145. object-fit: cover;
  146. }
  147. }
  148. }
  149. .banner-swiper-pagination {
  150. position: absolute;
  151. left: 50%;
  152. transform: translateX(-50%);
  153. bottom: 65px;
  154. z-index: 66;
  155. .swiper-pagination-bullet {
  156. width: 12px;
  157. height: 12px;
  158. background: #FFFFFF;
  159. opacity: 0.50;
  160. margin: 0 11px;
  161. &:focus {
  162. outline: none;
  163. }
  164. &.swiper-pagination-bullet-active {
  165. width: 45px;
  166. height: 12px;
  167. background: #FFFFFF;
  168. border-radius: 100px;
  169. opacity: 1;
  170. }
  171. }
  172. }
  173. }
  174. .channelAdvantage {
  175. display: none;
  176. position: absolute;
  177. width: 84px;
  178. height: 84px;
  179. top: 50%;
  180. transform: translateY(-50%);
  181. z-index: 2;
  182. outline: 0;
  183. cursor: pointer;
  184. text-align: center;
  185. line-height: 84px;
  186. color: #FFFFFF;
  187. background: rgba(0,0,0,.2);
  188. &.btnNext {
  189. right: 0;
  190. }
  191. &.btnPrev {
  192. left: 0;
  193. }
  194. div {
  195. height: 84px;
  196. line-height: 84px;
  197. font-size: 32px;
  198. }
  199. }
  200. .i-b-container {
  201. position: relative;
  202. width: 1200px;
  203. height: 100%;
  204. margin: 0 auto;
  205. }
  206. .i-b-tab {
  207. position: absolute;
  208. left: 50%;
  209. bottom: 27px;
  210. width: 62px;
  211. height: 10px;
  212. margin-left: -30px;
  213. }
  214. .b-c-part {
  215. position: absolute;
  216. top: 0;
  217. left: 50%;
  218. overflow: hidden;
  219. width: 1920px;
  220. height: 100%;
  221. margin-left: -960px;
  222. }
  223. .b-c-part img {
  224. display: block;
  225. width: 100%;
  226. height: 100%;
  227. }
  228. .i-b-btn {
  229. position: relative;
  230. width: 1200px;
  231. margin: 0 auto;
  232. }
  233. .btn-pre {
  234. position: absolute;
  235. top: -340px;
  236. left: -155px;
  237. width: 18px;
  238. height: 21px;
  239. background: url('~assets/images/index/banner-btn-pre.png') center center no-repeat;
  240. cursor: pointer;
  241. }
  242. .btn-next {
  243. position: absolute;
  244. top: -340px;
  245. right: -155px;
  246. width: 18px;
  247. height: 21px;
  248. background: url('~assets/images/index/banner-btn-next.png') center center no-repeat;
  249. cursor: pointer;
  250. }
  251. .btn-pre:hover {
  252. background: url('~assets/images/index/banner-btn-pre-cur.png') center center no-repeat;
  253. }
  254. .btn-next:hover {
  255. background: url('~assets/images/index/banner-btn-next-cur.png') center center no-repeat;
  256. }
  257. .listNext-enter-to {
  258. transition: all 1s ease;
  259. transform: translateX(0);
  260. }
  261. .listNext-leave-active {
  262. transition: all 1s ease;
  263. transform: translateX(-100%);
  264. }
  265. .listNext-enter {
  266. transform: translateX(100%);
  267. }
  268. .listNext-leave {
  269. transform: translateX(0);
  270. }
  271. .listPrev-enter-to {
  272. transition: all 1s ease;
  273. transform: translateX(0);
  274. }
  275. .listPrev-leave-active {
  276. transition: all 1s ease;
  277. transform: translateX(100%);
  278. }
  279. .listPrev-enter {
  280. transform: translateX(-100%);
  281. }
  282. .listPrev-leave {
  283. transform: translateX(0);
  284. }
  285. </style>