• vue-awesome-swiper导航内容(横向滚动条)与内容是联动的,导航内容始终在可视区域内


    需求分析

    导航内容(横向滚动条)与内容是联动的,导航内容始终在可视区域内

    在这里插入图片描述

    点击导航tab(猜你想搜)或者左右滑动到猜你想搜或者点击左右按钮到猜你想搜,导航tab(猜你想搜在可视区域内)

    在这里插入图片描述

    安装

    npm install vue-awesome-swiper@3 --save-dev
    
    • 1

    相关问题

    实现

    <template>
      <div class="recommendPage">
        <div class="tabCon">
          <div
            v-for="(item, index) in menuData"
            :key="index"
            :class="{ activeItem: activeIndex == index }"
            class="tabItem"
            @click="changeTab(index)"
          >
            {{ item.tabName }}
          div>
        div>
        <swiper
          :options="swiperOption"
          ref="mySwiper"
          @slide-change="slideChange"
        >
          <swiper-slide v-for="(item, index) in menuData"
            >{{ item.tabName }}内容swiper-slide
          >
          <div class="swiper-pagination" slot="pagination">div>
          <div class="swiper-button-prev" slot="button-prev">div>
          <div class="swiper-button-next" slot="button-next">div>
        swiper>
      div>
    template>
      
      <script>
    // 引入插件
    import { swiper, swiperSlide } from "vue-awesome-swiper";
    import "swiper/dist/css/swiper.css";
    
    export default {
      components: {
        swiper,
        swiperSlide,
      },
      data() {
        return {
          activeIndex: 0,
          menuData: [
            {
              tabName: "实时热榜",
            },
            {
              tabName: "全搜实时热榜",
            },
            {
              tabName: "猜你想搜-中台",
            },
            {
              tabName: "小说榜",
            },
          ],
          swiperOption: {
            // loop: true,
            // autoplay: {
            //   delay: 3000,
            //   stopOnLastSlide: false,
            //   disableOnInteraction: false,
            // },
            // 显示分页
            pagination: {
              el: ".swiper-pagination",
              clickable: true, //允许分页点击跳转
            },
            // 设置点击箭头
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
          },
        };
      },
      computed: {
        swiper() {
          return this.$refs.mySwiper.swiper;
        },
      },
      methods: {
        slideChange() {
          console.log("er1111111111111111111111111111", this.$refs.mySwiper.swiper.realIndex);
          const index = this.$refs.mySwiper.swiper.realIndex
          this.activeIndex = index
          this.changeScroll(index)
        },
        changeScroll(index) {
          let mySwiper = this.$refs.mySwiper.swiper;
          const scollDom = document.querySelector(".tabCon");
          console.log("scollDom", scollDom, mySwiper);
          let swiperWidth = scollDom.offsetWidth;
          let scrollItemDom = document.querySelectorAll(".tabItem");
          let DomLen = 0;
          scrollItemDom.forEach((item) => {
            console.log("item.offsetWidth", item.offsetLeft);
            DomLen += item.offsetWidth;
          });
          let maxTranslate = swiperWidth - DomLen;
          let maxWidth = -maxTranslate + swiperWidth / 2;
          let slide = scrollItemDom[index];
          let slideLeft = slide.offsetLeft;
          console.log("slideLeft", slide, slideLeft);
          let slideWidth = slide.clientWidth;
          let slideCenter = slideLeft + slideWidth / 2;
          // 被点击slide的中心点
    
          scollDom.style.transition = "3s";
    
          if (slideCenter < swiperWidth / 2) {
            console.log("为0", slideCenter, swiperWidth);
            scollDom.scrollLeft = 0;
          } else if (slideCenter > maxWidth) {
            console.log("最后一个", maxTranslate, maxWidth);
            scollDom.scrollLeft = -maxTranslate;
          } else {
            console.log("居中");
            let nowTlanslate = slideCenter - swiperWidth / 2;
            // mySwiper.setWrapperTranslate(-nowTlanslate);
            console.log("nowTlanslate", nowTlanslate);
            scollDom.scrollLeft = -nowTlanslate;
          }
        },
        changeTab(index) {
          this.$refs.mySwiper.swiper.slideTo(index);
          console.log("this.$refs.mySwiper.swiper", this.$refs.mySwiper.swiper);
        },
      },
      mounted() {
        // current swiper instance
        // 然后你就可以使用当前上下文内的swiper对象去做你想做的事了
        console.log("this is current swiper instance object", this.swiper);
        // this.swiper.slideTo(3, 1000, false);
      },
    };
    script>
    <style lang="scss" scoped>
    .tabCon {
      position: relative;
      text-align: center;
      overflow-x: auto;
      overflow-y: hidden;
      width: 300px;
      height: 60px;
      margin: auto;
      background: skyblue;
      display: flex;
      white-space: nowrap;
      .tabItem {
        padding: 20px;
      }
      .activeItem {
        border-bottom: 4px solid #333;
      }
    }
    style>
    <style scoped>
    .recommendPage .swiper-container {
      position: relative;
      width: 300px;
      height: 200px;
      background: pink;
    }
    .recommendPage .swiper-container .swiper-slide {
      width: 100%;
      line-height: 200px;
      background: yellowgreen;
      color: #000;
      font-size: 16px;
      text-align: center;
    }
    style>    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172

    参考

  • 相关阅读:
    计算机视觉——使用OpenCV GrabCut算法从图像中移除背景
    新一代相机传感器,三星Isocell HP3,更小像素更智能?
    node.js入门之 http.Agent 控制并发数量
    云原生的简单理解
    竞赛 基于深度学习的行人重识别(person reid)
    Java中静态常量和枚举类的区别
    mybatis学习(4) - SpringBoot中整合mybatis
    将自己本地项目上传到git,增加IDEA操作
    NodeJs实战-待办列表(6)-前端绘制表格显示待办事项详情
    【数据结构】顺序表与ArrayList
  • 原文地址:https://blog.csdn.net/ME_GIRL/article/details/127754950