项目需要,单行显示多个div,超过显示上下翻页,组件内容如下:
<div class="scroll-pane">
<div v-if="translateX" class="arrow-content left-icon-content" @click="slideLeft()">
<i class="el-icon-arrow-left"></i>
</div>
<div v-if="showRightIcon" class="arrow-content right-icon-content" @click="slideRight()">
<i class="el-icon-arrow-right"></i>
</div>
<div
ref="container"
v-resize="scollOrContentResize"
class="scroll-container"
@scroll="scollOrContentResize"
>
<div class="scoll-slot" ref="scollSlot" :style="{ transform: `translateX(${translateX}px)` }">
<slot />
</div>
</div>
</div>
</template>
<script>
export default {
name: "rowSet",
data() {
return {
showLeftIcon: false,
showRightIcon: false,
container: null,
translateX:0,
tx:110
};
},
created() {
},
mounted() {
this.scollOrContentResize();
},
methods: {
// 监测是否出现右键
scollOrContentResize() {
this.container = this.$refs.container
const { clientWidth, scrollWidth, scrollLeft } = this.container;
this.showRightIcon = scrollLeft + clientWidth < scrollWidth - 1;
},
slideLeft() {
if (this.translateX < 0) {
this.translateX += this.tx;
}
},
slideRight() {
const sliderWidth = this.$refs.scollSlot.offsetWidth;
if (this.translateX > this.container.offsetWidth - sliderWidth) {
this.translateX -= this.tx;
}
}
}
};
</script>
<style lang="scss" scoped>
.scroll-pane {
position: relative;
width: 100%;
.arrow-content {
position: absolute;
text-align: center;
line-height: 60px;
font-size: 20px;
color: #fff;
width: 26px;
cursor: pointer;
background: #bb9762;
z-index: 1;
&:hover {
color: #ffd8d8 !important;
}
}
.left-icon-content {
left: 0px;
}
.right-icon-content {
right: 0px;
}
.scroll-container {
position: relative;
width: 100%;
overflow-x: auto;
&::-webkit-scrollbar {
display: none;
}
.scoll-slot {
display: inline-block;
// 超出窗口长度时,显示滚动条
white-space: nowrap;
transition: transform 0.3s;
}
}
}
</style>
参考借鉴了这文同学的样式 juejin.cn/post/705744…