banner.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. img {
  142. width: 100%;
  143. height: 100%;
  144. object-fit: cover;
  145. }
  146. }
  147. }
  148. .banner-swiper-pagination {
  149. position: absolute;
  150. left: 50%;
  151. transform: translateX(-50%);
  152. bottom: 65px;
  153. z-index: 66;
  154. .swiper-pagination-bullet {
  155. width: 12px;
  156. height: 12px;
  157. background: #FFFFFF;
  158. opacity: 0.50;
  159. margin: 0 11px;
  160. &:focus {
  161. outline: none;
  162. }
  163. &.swiper-pagination-bullet-active {
  164. width: 45px;
  165. height: 12px;
  166. background: #FFFFFF;
  167. border-radius: 100px;
  168. opacity: 1;
  169. }
  170. }
  171. }
  172. }
  173. .channelAdvantage {
  174. display: none;
  175. position: absolute;
  176. width: 84px;
  177. height: 84px;
  178. top: 50%;
  179. transform: translateY(-50%);
  180. z-index: 2;
  181. outline: 0;
  182. cursor: pointer;
  183. text-align: center;
  184. line-height: 84px;
  185. color: #FFFFFF;
  186. background: rgba(0,0,0,.2);
  187. &.btnNext {
  188. right: 0;
  189. }
  190. &.btnPrev {
  191. left: 0;
  192. }
  193. div {
  194. height: 84px;
  195. line-height: 84px;
  196. font-size: 32px;
  197. }
  198. }
  199. .i-b-container {
  200. position: relative;
  201. width: 1200px;
  202. height: 100%;
  203. margin: 0 auto;
  204. }
  205. .i-b-tab {
  206. position: absolute;
  207. left: 50%;
  208. bottom: 27px;
  209. width: 62px;
  210. height: 10px;
  211. margin-left: -30px;
  212. }
  213. .b-c-part {
  214. position: absolute;
  215. top: 0;
  216. left: 50%;
  217. overflow: hidden;
  218. width: 1920px;
  219. height: 100%;
  220. margin-left: -960px;
  221. }
  222. .b-c-part img {
  223. display: block;
  224. width: 100%;
  225. height: 100%;
  226. }
  227. .i-b-btn {
  228. position: relative;
  229. width: 1200px;
  230. margin: 0 auto;
  231. }
  232. .btn-pre {
  233. position: absolute;
  234. top: -340px;
  235. left: -155px;
  236. width: 18px;
  237. height: 21px;
  238. background: url('~assets/images/index/banner-btn-pre.png') center center no-repeat;
  239. cursor: pointer;
  240. }
  241. .btn-next {
  242. position: absolute;
  243. top: -340px;
  244. right: -155px;
  245. width: 18px;
  246. height: 21px;
  247. background: url('~assets/images/index/banner-btn-next.png') center center no-repeat;
  248. cursor: pointer;
  249. }
  250. .btn-pre:hover {
  251. background: url('~assets/images/index/banner-btn-pre-cur.png') center center no-repeat;
  252. }
  253. .btn-next:hover {
  254. background: url('~assets/images/index/banner-btn-next-cur.png') center center no-repeat;
  255. }
  256. .listNext-enter-to {
  257. transition: all 1s ease;
  258. transform: translateX(0);
  259. }
  260. .listNext-leave-active {
  261. transition: all 1s ease;
  262. transform: translateX(-100%);
  263. }
  264. .listNext-enter {
  265. transform: translateX(100%);
  266. }
  267. .listNext-leave {
  268. transform: translateX(0);
  269. }
  270. .listPrev-enter-to {
  271. transition: all 1s ease;
  272. transform: translateX(0);
  273. }
  274. .listPrev-leave-active {
  275. transition: all 1s ease;
  276. transform: translateX(100%);
  277. }
  278. .listPrev-enter {
  279. transform: translateX(-100%);
  280. }
  281. .listPrev-leave {
  282. transform: translateX(0);
  283. }
  284. </style>