gpt4 book ai didi

html - 如何正确隐藏这个溢出的内容?

转载 作者:太空宇宙 更新时间:2023-11-04 10:07:45 25 4
gpt4 key购买 nike

$('.child_1').click(function(){
$(this).toggleClass('left').siblings().toggleClass('left');
});
* {box-sizing: border-box;}
.parent {
width: 500px;
position: relative;
border: 1px solid #ccc;
overflow: hidden;
}
.child_1 {
background-color: rgba(255,255,0,.6);
width: 100%;
float: left;
height: 100px;
z-index: 1;
transition: all .5s;
}
.child_1.left {
width: 30%;
}
.child_2 {
width: 70%;
padding-left: 15px;
left: 100%;
transition: all .5s;
}
.child_2.left {
left: 30%;
}
.child_2 > div {
background-color: rgba(0,255,255,.6);
height: 200px;
}

.child_2 > div > div {
overflow: hidden;
}
.relative {
position: relative;
}
.absolute {
position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<p>Relative</p>
<div class="parent">
<div class="child_1"></div>
<div class="child_2 relative">
<div>Content of child_2<div></div></div>
</div>
</div>
<hr />
<p>Absolute</p>
<div class="parent">
<div class="child_1"></div>
<div class="child_2 absolute">
<div>Content of child_2<div></div></div>
</div>
</div>
我遇到这样的麻烦:

  1. 当 child_2 被移出父级时,父级 div 必须有 hidden 的溢出来隐藏 child_2;

  2. 父 div 不能有声明的高度,因为 child_2 的高度未知(以为它在我的示例中给出)。

为了隐藏child_2 div,我尝试了两种方法:

  1. 第一个相对定位child_2,而可以看到,child_2的内容被推到了错误的位置;

  2. 第二个是绝对定位的child_2,但是绝对定位的元素不能扩展父元素的高度,所以当父div被赋予 overflow hidden 值时,child_2的内容被截断。

最佳答案

$(document).ready(function() {
$('.child_1').click(function(){
$(this).toggleClass('left').siblings().toggleClass('left');
});
});
* {box-sizing: border-box;}
.parent {
width: 500px;
position: relative;
border: 1px solid #ccc;
overflow: hidden;
height: 100px;
}
.child_1 {
background-color: rgba(255,255,0,.6);
width: 100%;
float: left;
height: 100px;
z-index: 1;
transition: all .5s;
}
.child_1.left {
width: 30%;
}
.child_2 {
width: 70%;
padding-left: 15px;
left: 100%;
transition: all .5s;
}
.child_2.left {
left: 30%;
}
.child_2 > div {
background-color: rgba(0,255,255,.6);
height: 200px;
}

.child_2 > div > div {
overflow: hidden;
}
.relative {
position: relative;
}
.absolute {
position: absolute;
}
<!DOCTYPE html>
<html>

<head>
<link rel="stylesheet" type="text/css" href="sample.css">
</head>

<body style="display:table">
<p>Relative</p>
<div class="parent">
<div class="child_1"></div>
<div class="child_2 relative">
<div>1
<div></div>
</div>
</div>
</div>
<hr />
<p>Absolute</p>
<div class="parent">
<div class="child_1"></div>
<div class="child_2 absolute">
<div>1
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
</body>

</html>

我已经为父 div 添加了高度,然后溢出就可以正常工作了

关于html - 如何正确隐藏这个溢出的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37850867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com