• vue3使用swiper6.7.0写轮播图,按钮在轮播图外面


    应用场景:需要在header区域,写24小时天气预测轮播,按钮在轮播图外面,默认隐藏左侧按钮,当点击右侧按钮后,左侧按钮显示,当点击到最后一个轮播图的显示时,隐藏右侧按钮。通过获取索引,监听索引的方法实现。

    项目里以前使用的是swiper6.7,所以这次写这个轮播还用了6版本,没有升级。

    效果:

    1. <script>
    2. import { Swiper, Navigation } from "swiper";
    3. import "swiper/swiper-bundle.css";
    4. import { onMounted, reactive, nextTick, toRefs, ref, watch } from "vue";
    5. import { getNowAnd24HoursData } from "@/api/RunMonitor/GroupMonitorSystem"
    6. import { getRefreshInterval } from '@/api/sourceMonitor/home.ts';
    7. Swiper.use([Navigation]);
    8. export default {
    9. props: {
    10. hoursData: {
    11. type: Array,
    12. default: [],
    13. },
    14. nextHourIndex: {
    15. type: Number,
    16. default: 0,
    17. }
    18. },
    19. setup(props) {
    20. const state = reactive({
    21. // data: [
    22. // { weatherTime: '00:00', weather: '晴', outTemp: "20.3℃" },
    23. // { weatherTime: '01:00', weather: '多云', outTemp: "20.3℃" },
    24. // { weatherTime: '02:00', weather: '晴', outTemp: "20.3℃" },
    25. // ],
    26. showPrevButton: false, // 控制左按钮的显示和隐藏
    27. showNextButton: true, // 控制右按钮的显示和隐藏
    28. index: 0
    29. });
    30. let GalleryTop;
    31. const initGalleryTop = () => {
    32. GalleryTop = new Swiper(".swiper-container", {
    33. navigation: {
    34. nextEl: "#next",
    35. prevEl: "#prev",
    36. },
    37. loop: false, // 禁止循环
    38. noSwiping: true, // 禁止滑动
    39. spaceBetween: 1, // 间距
    40. slidesPerView: 3, // 显示数量
    41. freeMode: false, // 不开启自由模式
    42. watchSlidesVisibility: true, // 监视slide的可见性
    43. watchSlidesProgress: true, // 监视slide的进度
    44. initialSlide: props.nextHourIndex, // 初始slide的索引值
    45. virtual: true, //开启虚拟slides
    46. centeredSlides: false, // 不居中显示slides
    47. });
    48. };
    49. const handlePrevClick = (() => {
    50. return () => {
    51. // 在此处处理右按钮点击事件
    52. // console.log("点击左按钮,当前item:", props.hoursData[state.index], "当前index:", state.index);
    53. state.index--; // 每次点击右按钮,增加index的值
    54. };
    55. })();
    56. const handleNextClick = (() => {
    57. return () => {
    58. // 在此处处理右按钮点击事件
    59. // console.log("点击右按钮,当前item:", props.hoursData[state.index], "当前index:", state.index);
    60. state.index++; // 每次点击右按钮,增加index的值
    61. };
    62. })();
    63. watch([() => props.hoursData, () => props.nextHourIndex], () => {
    64. if (props.hoursData || props.nextHourIndex) {
    65. nextTick(() => {
    66. initGalleryTop();
    67. })
    68. }
    69. if (props.hoursData.length < 1) {
    70. state.showNextButton = false; // 隐藏右按钮
    71. }
    72. }, { deep: true })
    73. watch(() => state.index, (n) => {
    74. if (n > 0) {
    75. state.showPrevButton = true; // 显示左按钮
    76. } else if (n === 0) {
    77. state.showPrevButton = false; // 隐藏左按钮
    78. }
    79. if (n === props.hoursData.length - 3) {
    80. state.showNextButton = false; // 隐藏右按钮
    81. } else {
    82. state.showNextButton = true; // 显示右按钮
    83. }
    84. })
    85. onMounted(() => {
    86. });
    87. return {
    88. ...toRefs(state),
    89. handleNextClick,
    90. handlePrevClick
    91. };
    92. },
    93. };
    94. script>
    95. <style lang="less" scoped>
    96. .swiper-container {
    97. position: relative;
    98. width: 520px;
    99. height: 100%;
    100. padding: 0 14px;
    101. --swiper-navigation-size: 13px;
    102. --swiper-theme-color: var(--gdky-placeholder-tip-color);
    103. overflow: hidden;
    104. }
    105. .swiper-wrapper {
    106. position: initial;
    107. }
    108. .swiper-slide {
    109. display: flex;
    110. align-items: center;
    111. justify-content: center;
    112. font-family: MicrosoftYaHei;
    113. height: 52px;
    114. .icon {
    115. display: flex;
    116. align-items: center;
    117. justify-content: center;
    118. width: 14px;
    119. height: 14px;
    120. padding-top: 1px;
    121. }
    122. .time {
    123. color: var(--gdky-placeholder-tip-color);
    124. display: flex;
    125. align-items: center;
    126. justify-content: center;
    127. font-size: 14px;
    128. }
    129. .temp,
    130. .day {
    131. font-size: 14px;
    132. margin-left: 2px;
    133. color: var(--gdky-third-content-color);
    134. display: flex;
    135. align-items: center;
    136. justify-content: center;
    137. text-align: center;
    138. }
    139. }
    140. .swiper-button-prev::after {
    141. margin-left: -10px;
    142. display: flex;
    143. align-items: center;
    144. justify-content: center;
    145. }
    146. .swiper-button-next::after {
    147. margin-left: 12px;
    148. display: flex;
    149. align-items: center;
    150. justify-content: center;
    151. }
    152. style>

  • 相关阅读:
    ASP.NET6 + Mongo + OData
    AI:业余时间打比赛—挣它个小小目标—【阿里安全×ICDM 2022】大规模电商图上的风险商品检测比赛
    【小游戏】Unity游戏愤怒的足球(小鸟)
    BI业务分析思维:供应链生产制造策略(四) MTS
    pandas学习(六) STATS
    golang 上传图片 --chatGPT
    Spring常见问题解决 - AOP调用被拦截类的属性报NPE
    JAVA基础 - Serializable的作用与用法
    Linux常用命令
    计算机等级考试—信息安全三级真题七
  • 原文地址:https://blog.csdn.net/m0_62323730/article/details/133902889