Vue轮播图(swiper)

骑猪看日落 2023-03-04 06:46 104阅读 0赞

Vue轮播图(swiper)

注意:swiper版本为5.x.x时,import才能引入成功

1. 在terminal中输入,引入swiper依赖

  1. -- npm install swiper

2. 新建一个Banner.vue文件

  1. <template>
  2. <div class="banner">
  3. <div class="swiper-container">
  4. <div class="swiper-wrapper">
  5. <div class="swiper-slide" v-for="(item,index) in banners" :key="index">
  6. <img :src="item.pic" />
  7. </div>
  8. <!-- 如果需要分页器 -->
  9. <div class="swiper-pagination"></div>
  10. <!-- 如果需要导航按钮 -->
  11. <div class="swiper-button-prev"></div>
  12. <div class="swiper-button-next"></div>
  13. <!-- 如果需要滚动条 -->
  14. <div class="swiper-scrollbar"></div>
  15. </div>
  16. </div>
  17. </div>
  18. </template>
  19. <script>
  20. //引入依赖
  21. import Swiper from "swiper";
  22. export default {
  23. props: {
  24. 'banners': {
  25. type: Array,
  26. default: () => [],
  27. },
  28. },
  29. methods: {
  30. initBanner() {
  31. new Swiper(".swiper-container", {
  32. direction: "horizontal", // 垂直切换选项
  33. loop: true, // 循环模式选项
  34. // 如果需要分页器
  35. pagination: {
  36. el: ".swiper-pagination",
  37. },
  38. navigation: {
  39. nextEl: ".swiper-button-next",
  40. prevEl: ".swiper-button-prev",
  41. },
  42. // 如果需要滚动条
  43. scrollbar: {
  44. el: ".swiper-scrollbar",
  45. },
  46. });
  47. },
  48. },
  49. mounted() {
  50. this.initBanner();
  51. },
  52. watch: {
  53. banners() {
  54. //banners改变 网络请求数据已经成功 之后进行初始化操作
  55. this.$nextTick(() => {
  56. this.initBanner();
  57. });
  58. },
  59. },
  60. };
  61. </script>
  62. <style lang="less" scoped>
  63. @import "../../style/index.less";
  64. @import "~swiper/css/swiper.min.css";
  65. .swiper-container {
  66. .w(375);
  67. .h(150);
  68. background: green;
  69. img {
  70. .w(375);
  71. .h(150);
  72. }
  73. }
  74. </style>

发表评论

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

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

相关阅读