简体   繁体   English

设置溢出-y 可见,而溢出-x 是自动的,以便内容可以垂直溢出父容器

[英]Set overflow-y visible while overflow-x is auto so that content can vertically overflow parent's container

I'm using position: absolute and position: relative to display text over an image if its parent is hovered over.我正在使用position: absoluteposition: relative来在图像上显示文本,如果其父悬停在图像上。 The container that the text and images parent div is in is set to overflow-x: auto , causing it to have a horizontal scrollbar.文本和图像父 div 所在的容器设置为overflow-x: auto ,使其具有水平滚动条。

I want the text that appears to vertically overflow the #container element (and the horizontal scrollbar), but this is not happening - instead, a vertical scrollbar is appearing in the container.我想要看起来垂直溢出#container元素(和水平滚动条)的文本,但这并没有发生 - 相反,容器中出现了垂直滚动条。

I do not want the containers height to expand to the height of the text.我不希望容器高度扩展到文本的高度。

I've tried applying overflow-y: visible to #container but this hasn't resolved the problem.我试过应用overflow-y: visible#container overflow-y: visible ,但这并没有解决问题。 If I remove overflow-x: auto from #container it fixes the problem, but removes the horizontal scrollbar from #container and puts it on body (which I don't want)如果我从#container删除overflow-x: auto它可以解决问题,但会从#container删除水平滚动条并将其放在body (我不想要)

 function textVisibility(name) { var p = document.getElementById(name); if (p.style.display == "block") { p.style.display = "none"; } else { p.style.display = "block"; } }
 .div { margin: 5px; flex: 0 0 100px; position: relative; } img { width: 70%; } p { margin: 0; position: absolute; left: 0; top: 0; right: 0; bottom: 0; display: none; } #container { display: flex; flex-wrap: nowrap; overflow-x: auto; overflow-y: visible; }
 <div id="container"> <div class="div" onmouseenter="textVisibility(1);" onmouseleave="textVisibility(1)"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p id="1">Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div" onmouseenter="textVisibility(2);" onmouseleave="textVisibility(2)"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p id="2">Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div" onmouseenter="textVisibility(3);" onmouseleave="textVisibility(3)"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p id="3">Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div" onmouseenter="textVisibility(4);" onmouseleave="textVisibility(4)"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p id="4">Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div" onmouseenter="textVisibility(5);" onmouseleave="textVisibility(5)"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p id="5">Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div" onmouseenter="textVisibility(6);" onmouseleave="textVisibility(6)"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p id="6">Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div>

JsFiddle: https://jsfiddle.net/r3Lja69h/ JsFiddle: https ://jsfiddle.net/r3Lja69h/

Consider the use of position:fixed and adjust the position dynamically on hover:考虑使用position:fixed并在悬停时动态调整位置:

 document.querySelectorAll('.div').forEach((div) => { div.addEventListener('mouseover', () => { var r = div.getBoundingClientRect(); div.style.setProperty("--t", r.top+"px"); div.style.setProperty("--l", r.left+"px"); div.style.setProperty("--w", r.width+"px"); }); });
 .div { margin: 5px; flex: 0 0 100px; text-align:center; border:1px solid; } img { width: 70%; } p { margin: 0; position: fixed; top: var(--t,0); left:var(--l,0); width:var(--w,0); display:none; } #container { display: flex; flex-wrap: nowrap; overflow-x: auto; } .div:hover p { display:block; }
 <div id="container"> <div class="div"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p>Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p>Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p>Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p>Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p>Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p>Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p>Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p>Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p>Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> </div>

If either overflow-x or overflow-y property is neither visible nor clip , then visible / clip is calculated as auto / hidden , respectively.如果overflow-xoverflow-y属性既不是visible也不是clip ,则visible / clip分别计算为auto / hidden

That is, if you specify overflow-x: auto;也就是说,如果你指定了overflow-x: auto; , overflow-y property will also be auto (because the default value is visible ). , overflow-y属性也将是auto (因为默认值是visible )。

3. Scrolling and Clipping Overflow: the overflow-x, overflow-y, and overflow properties ref 3. Scrolling 和 Clipping Overflow:overflow-x、overflow-y 和 overflow 属性ref

as specified, except with visible/clip computing to auto/hidden (respectively) if one of overflow-x or overflow-y is neither visible nor clip如指定的那样,除了可见/剪辑计算自动/隐藏(分别)如果溢出-x 或溢出-y 之一既不可见也不剪辑

One solution is to make the absolute placement of the image instead of the text, so that the text determines the height of the container.一种解决方案是让图片绝对放置而不是文字,让文字决定容器的高度。

 .div { margin: 5px; flex: 0 0 100px; position: relative; } img { width: 70%; position: absolute; left: 0; top: 0; right: 0; bottom: 0; z-index: -1; } p { margin: 0; } #container { display: flex; flex-wrap: nowrap; overflow-x: scroll; }
 <div id="container"> <div class="div"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p>Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p>Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p>Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p>Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p>Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> <div class="div"> <img src="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png" /> <p>Some very long text Some very long text Some very long text Some very long text Some very long text </p> </div> </div> <button onclick="toggleText();">Click me </button>

If you want X-Axis scrolling and Y-Axis hidden than you write :-如果你想 X 轴滚动和 Y 轴隐藏比你写的: -

overflow-x: auto;
overflow-y: hidden;

If you want Y-Axis scrolling and X-Axis hidden than you write:-如果您希望 Y 轴滚动和 X 轴隐藏而不是您写的:-

overflow-x: hidden;
overflow-y: auto;

If you want all Axis scrollable than you write:-如果您希望所有 Axis 可滚动而不是您写的:-

overflow:auto;

All Axis hidden than: -所有轴隐藏比: -

overflow: hidden;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM