Vue中使用swiper插件实现轮播图

爱被打了一巴掌 2022-10-09 02:56 341阅读 0赞

最终效果如图:

20210623143509101.gif

为了更稳定不建议使用最新版本swiper

下载指定版本如下图:

用到vue-awesome-swiper组件支持vue的写法:

终端输入如下命令下载vue-awesome-swiper和swiper

npm install vue-awesome-swiper@3 swiper@4 —save

组件内的使用: 重点是引入这两个文件:

import “swiper/dist/css/swiper.css”;

import { swiper, swiperSlide } from “vue-awesome-swiper”;

  1. <template>
  2. <!-- 帖子图片轮播 -->
  3. <div class="swiperImgPage">
  4. <swiper :options="swiperOption" ref="mySwiper">
  5. <swiper-slide>I'm Slide 1</swiper-slide>
  6. <swiper-slide>I'm Slide 2</swiper-slide>
  7. <swiper-slide>I'm Slide 3</swiper-slide>
  8. <div
  9. class="swiper-pagination"
  10. slot="pagination"
  11. ></div>
  12. <div class="swiper-button-prev" slot="button-prev"></div>
  13. <div class="swiper-button-next" slot="button-next"></div>
  14. </swiper>
  15. </div>
  16. </template>
  17. <script>
  18. import "swiper/dist/css/swiper.css";
  19. import { swiper, swiperSlide } from "vue-awesome-swiper";
  20. let vm = null;
  21. export default {
  22. name: "swiperImg",
  23. data() {
  24. return {
  25. activeSlide: 0, //获取当前的索引值
  26. //里面的自定义的需求配置去看swiper文档的 基本的样式都出来了
  27. swiperOption: {
  28. /* 设定初始化时slide的索引。Swiper默认初始化时显示第一个slide,有时想初始化时直接显示其他slide,可以做此设置。
  29. */
  30. initialSlide: 0, //初始化索引
  31. loop: true,
  32. // allowTouchMove:true,
  33. // 显示分页
  34. pagination: {
  35. el: ".swiper-pagination",
  36. clickable: true, //允许分页点击跳转
  37. },
  38. on: {
  39. //滑动触发的事件
  40. slideChange() {
  41. console.log(this.activeIndex);
  42. console.log(this);
  43. vm.activeSlide = this.activeIndex;
  44. },
  45. //点击触发事件
  46. tap(swiper, event) {
  47. console.log("你碰了Swiper", swiper.target.currentSrc, event);
  48. },
  49. },
  50. autoplay: {
  51. delay: 3000,
  52. stopOnLastSlide: false,
  53. disableOnInteraction: false,
  54. },
  55. // 设置点击箭头
  56. navigation: {
  57. nextEl: ".swiper-button-next",
  58. prevEl: ".swiper-button-prev",
  59. },
  60. },
  61. };
  62. },
  63. created() {
  64. vm = this;
  65. },
  66. computed: {
  67. swiper() {
  68. return this.$refs.mySwiper.swiper;
  69. },
  70. },
  71. components: {
  72. swiper,
  73. swiperSlide,
  74. },
  75. mounted() {
  76. // // 然后你就可以使用当前上下文内的swiper对象去做你想做的事了
  77. console.log("this is current swiper instance object", this.swiper);
  78. },
  79. methods: {
  80. },
  81. };
  82. </script>
  83. <!-- Add "scoped" attribute to limit CSS to this component only -->
  84. <style lang="scss">
  85. $myPadding: rpx(30);
  86. .swiperImgPage {
  87. box-sizing: border-box;
  88. width: 100%;
  89. position: relative;
  90. // 轮播图
  91. .swiper-container {
  92. width: 100%;
  93. // overflow: visible;
  94. .swiper-wrapper {
  95. display: flex;
  96. align-items: center;
  97. .swiper-slide {
  98. width: 100%;
  99. // display: none;
  100. height: rpx(400);
  101. color: #000;
  102. font-size: rpx(16);
  103. text-align: center;
  104. &:nth-child(1) {
  105. background: yellowgreen;
  106. }
  107. &:nth-child(2) {
  108. background: cyan;
  109. }
  110. &:nth-child(3) {
  111. background: orange;
  112. }
  113. &:nth-child(4) {
  114. background: rosybrown;
  115. }
  116. &:nth-child(5) {
  117. background: burlywood;
  118. }
  119. &:nth-child(6) {
  120. background: yellowgreen;
  121. }
  122. }
  123. }
  124. }
  125. }
  126. </style>

想要做逻辑处理看swiper文档 按照我说的基本就可以实现下面的效果 如果你没成功swiper和vue-awesome-swiper版本按照我说的来 (别自己发挥)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0NTA3OTAy_size_16_color_FFFFFF_t_70

发表评论

表情:
评论列表 (有 0 条评论,341人围观)

还没有评论,来说两句吧...

相关阅读