我的前端坑
  • 关于本书
  • CSS JS 常用
    • 常用 CSS 样式
    • 坑爹的 CSS
    • sass 入门
    • canvas 总结
    • 常用 JS 函数
    • 表单和 FormData 对象
    • 水平滑动 tab 居中
    • js 中的 this
    • sse 和 fetch
    • js 原型链与 class 类
  • TypeScript
    • TS 概念
    • interface 与 type
    • interface 接口
    • Ts 配合 ESLint 和 prettier
  • 小程序
    • 常用小程序代码
  • VUE
    • VUE2 小技巧
    • VUE-CLI 中的 NODE_ENV
  • VUE3
    • VUE3 自行构建 sfc 遇到的坑
    • VUE3 v-model 的实现
    • VUE3 使用总结
    • VUE3 ref
  • vite
    • vite
  • http 请求
    • 前端实现下载
    • cors 跨域请求
    • windows hosts 文件
    • err_blocked_by_client 错误
  • 前端工具
    • npm 和 Node
      • 常见问题
      • npmTips
      • package.json 与 package-lock.json
      • npx
      • exports 与 module.exports
      • ESLint
    • VIM
      • vim 常用
    • Git
      • Git 常用命令
      • Git 小 tips
    • express
  • 后端工具
    • mysql 常见问题
    • docker 常见问题
    • docker
  • java
    • java 常用
    • lambda 表达式
    • java 字符串
    • java 泛型
    • java 反射
    • intellij IDEA
    • 多态
    • java 包管理
    • sql 查询语言
    • java 反射
    • java 异常
    • java 集合
    • spring
  • 命令行
    • 命令行 常用
  • 专利撰写 ppt
  • 后台简述
Powered by GitBook
On this page

Was this helpful?

  1. CSS JS 常用

水平滑动 tab 居中

Previous表单和 FormData 对象Nextjs 中的 this

Last updated 3 years ago

Was this helpful?

能够左右滑动的 tabs 标签页,有时候需要将处于 active 状态的 tab 居中显示,效果如下

html 文件如下:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>JS Bin</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  </head>

  <body>
    <div id="app">
      <div class="slide-wrap" id="parent">
        <div
          v-for="(item,index) in tabList"
          @focus="changeTab(index)"
          :key="index"
          :id="'item'+index"
          tabindex="0"
          :class="{item:true,'focus':activeIndex===index?true:false}"
        >
          {{item.name}}
        </div>
      </div>
    </div>
  </body>
</html>

CSS 文件如下:

.slide-wrap {
  overflow-x: scroll;
  display: flex;
}
.slide-wrap::-webkit-scrollbar {
  display: none;
}
.item {
  text-align: center;
  white-space: nowrap;
  width: 20%;
  flex-shrink: 0;
  outline: none;
}
.focus {
  box-sizing: border-box;
  transition: border 0.4s;
  box-shadow: none;
  outline: none;
}
.focus:focus {
  outline: none;
  background-color: #ccc;
}

JS 文件如下:

var app = new Vue({
  el: '#app',
  data: {
    activeIndex: 0,
    tabList: [
      {
        name: '分类0',
        value: 0
      },
      {
        name: '分类1',
        value: 1
      },
      {
        name: '分类2',
        value: 2
      },
      {
        name: '分类3',
        value: 3
      },
      {
        name: '分类4',
        value: 4
      },
      {
        name: '分类5',
        value: 5
      },
      {
        name: '分类6',
        value: 6
      },
      {
        name: '分类7',
        value: 7
      },
      {
        name: '分类8',
        value: 8
      },
      {
        name: '分类9',
        value: 9
      },
      {
        name: '分类10',
        value: 10
      }
    ]
  },
  methods: {
    changeTab(index) {
      this.activeIndex = index
      const father_dom = document.getElementById('parent')
      const son_dom = document.getElementById('item' + index)
      const clientWidth = father_dom.clientWidth
      const offsetLeft = son_dom.offsetLeft
      father_dom.scrollLeft = offsetLeft - clientWidth / 2 + son_dom.clientWidth / 2
    }
  }
})
在线查看
tab居中效果图