横向滚动条

64 阅读1分钟
<template>
  <div>
    <div class="echarts-container" :style="{ width: containerWidth + 'px' }">
      <div class="center-div">
        <div v-for="n in 20" class="rectangle bg-blue-500"></div>
      </div>
    </div>
    <div class="button-group">
      <button @click="increaseWidth">增加宽度</button>
      <button @click="decreaseWidth">减少宽度</button>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const containerWidth = ref(600)

const increaseWidth = () => {
  containerWidth.value += 50
}

const decreaseWidth = () => {
  containerWidth.value = Math.max(50, containerWidth.value - 50)
}
</script>

<style scoped>
.echarts-container {
  margin: 0 auto;
  height: 300px;
  background-color: pink;
  display: flex;
  align-items: center;
  justify-content: center;
}

.center-div {
  height: 200px;
  width: calc(100% - 20px);
  background-color: #fff;
  margin: 0 10px;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  overflow-x: auto;
  flex-wrap: nowrap;

  /* Firefox 滚动条样式 */
  scrollbar-width: thin;
  /* 设置滚动条宽度 */
  scrollbar-color: #888 #f1f1f1;
  /* 设置滑块和轨道颜色 */

  /* Webkit (Chrome/Safari) 滚动条样式 */
  &::-webkit-scrollbar {
    height: 8px;
  }

  &::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 4px;
  }

  &::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 4px;
  }

  &::-webkit-scrollbar-thumb:hover {
    background: #555;
  }
}

.button-group {
  margin-top: 20px;
  text-align: center;
}

.button-group button {
  margin: 0 10px;
  padding: 8px 16px;
}

.rectangle {
  width: 20px;
  height: 100px;
  flex-shrink: 0;
}
</style>