overflow-y:auto/hidden/scroll和overflow-x:visible组合渲染异常

本文介绍了一种解决CSS中水平与垂直滚动条显示冲突的方法。通过调整容器宽度并利用overflow-y属性实现仅在需要时显示垂直滚动条,同时避免了水平滚动条的出现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近做项目想做一个这样的效果:就是我想要内部div x轴溢出div则显示y轴溢出div则出现滚动条
于是用到了overflow-y 和 overflow-x 这个css属性
原来以为css中直接设置就ok

{
overflow-y:scroll;
overflow-x: visible;
}

但是实际情况是并不好用 会出现两边都是scroll的情况上网上查了一下解决方案,很多都说试着将overflow-x和overflow-y放在不同的DOM元素上。但是会因为实际使用情况和逻辑上的复杂程度而变得并不好用。最终解决方案如下:
把容器的宽度去掉,让其内容自己撑开容器,这样不会出现滚动条,和横向溢出的最终目的是一样的;然后设置纵向overflow-y:scroll即可。
至于浏览器为什么会这样 就只搬运了 不详细解释


参考资料:
解决方案:https://power.baidu.com/quest...
原因:http://w3help.org/zh-cn/cause...

<template> <div class=“ScrollData_containe”> <div class=“item_header”> <p v-for=“(item, index) in header” :key=“index”> <span v-if=“item.icon” ><i class=“iconfont” :class=“[item.icon]”></i ></span> {{ item.text }} </p> </div> <div class=“item_cont” ref=“topbox”> <div :class=“[‘font-list’, dynamicCls]” :style=“dynamicSty”> <div class=“scrollBox” ref=“fontbox”> <p v-for=“(item, index) in data” :key=“index”> <!-- :class=“[Math.floor(Math.random() * 18) == 2 ? ‘bgChange’ : ‘’]” --> <span>{{ item.one }}</span> <span>{{ item.two }}</span> <span>{{ item.three }}</span> <span>{{ item.four }}</span> <span>{{ item.five }}</span> </p> </div> <div class=“scrollBox” ref=“doubleData”> <p v-for=“(item, index) in data” :key=“index”> <!-- :class=“[Math.floor(Math.random() * 18) == 2 ? ‘bgChange’ : ‘’]” --> <span>{{ item.one }}</span> <span>{{ item.two }}</span> <span>{{ item.three }}</span> <span>{{ item.four }}</span> <span>{{ item.five }}</span> </p> </div> </div> </div> </div> </template> <script> export default { props: { data: Object, header: Array, pageDuration: { type: Number, default: 12 } }, data() { return { doubleData: false, // 是否需要双份数据 fontBox: null, // 文字容器 topBox: null, // 文字父级容器 countFlag: 0, // 切换clss类标识符 dynamicCls: “”, // 动态class类,切换使用解决数据更新后动画不重新开始问题 dynamicSty: {}, scrollIndex: 0, newScrollData: null, scrollTimer: null, scrollBox: null, speed: 30 // 滚动速度 }; }, methods: { calcHeight() { // 判断内容是否超出容器,如果超出容器、追加一份数据 if (this.fontBox.offsetHeight > this.topBox.offsetHeight) { this.doubleData = true; this.countFlag += 1; // 切换动画类名 this.dynamicCls = scroll-cls${this.countFlag % 2}; // 动态计算动画时长 this.dynamicSty = { animationDuration: ${ (this.fontBox.offsetHeight / this.topBox.offsetHeight) * this.pageDuration }s }; } else { this.doubleData = false; this.dynamicCls = “”; this.dynamicSty = { animationDuration: “0s” }; } } }, watch: { // 监听内容变化 data: { deep: true, handler(_val) { this.calcHeight(); } } }, mounted() { this.fontBox = this.$refs.fontbox; this.topBox = this.$refs.topbox; this.$nextTick(() => { this.calcHeight(); }); }, beforeDestroy() {} }; </script> <style lang=“scss” scoped> .scroll-cls0 { animation: translateY1 8s 0.5s linear infinite; } .scroll-cls1 { animation: translateY2 8s 0.5s linear infinite; } // .font-list:hover { // animation-play-state: paused; // } @keyframes translateY1 { from { transform: translateY(0); } to { transform: translateY(-50%); } } @keyframes translateY2 { from { transform: translateY(0); } to { transform: translateY(-50%); } } .bgChange { background: #211141 !important; span { text-shadow: 0 0 10px #f00; } } .item_header { display: flex; justify-content: space-between; height: 0.2125rem; background: #111843; p { color: #fff; flex: 1; line-height: 0.2125rem; padding-left: 0.0625rem; font-family: “OPPOSANS-M”; span { display: inline-block; width: 0.125rem; height: 0.125rem; line-height: 0.125rem; background: #304bfa; text-align: center; box-shadow: 0 0 10px #bc6fea; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; vertical-align: middle; border-radius: 0.0125rem; } .iconfont { color: #fff !important; font-size: 0.0875rem; } } } .item_cont { height: 1.875rem; box-sizing: border-box; overflow: hidden; position: relative; .scrollBox { // position: absolute; width: 100%; left: 0; p { display: flex; justify-content: space-between; background: #060a24; height: 0.2rem; line-height: 0.2rem; margin: 0.0125rem 0; color: #fff; font-size: 0.0875rem; font-family: “OPPOSANS-M”; span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 0.0625rem; } } } } </style> 在该代码的基础上优化,麻烦给出完整的优化结果
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值