gpt4 book ai didi

javascript - 将 Parent Div 收缩为 Foated Children

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

我正在尝试创建家谱,但功能的最后一部分给我带来了一些困难。

我的树是一系列向左浮动的列表元素。

我想要的是,如果树展开超过屏幕尺寸,父 div #c1 应该允许用户在树上水平滚动。

现在发生的事情是,如果树的宽度大于#c1,那么树会 self 包裹,使其无法读取。

我通过在 #c1 中添加一个名为 #c2 的 div 来临时解决这个问题,它的宽度为 100000px。基本上树永远不会变得这么大,所以它永远不会自己包裹起来,用户可以在树上滚动。

我想要的是在使用 javascript 加载时以某种方式使 #c2 的宽度适合树,或者以某种方式找到 css 解决方案。

我尝试过使用 white-space: nowrap,以及针对类似问题发现的许多其他解决方案,但似乎对我没有任何效果。

* {margin: 0; padding: 0;}

.scrollbtn {
float: right;
cursor: pointer;
margin: 5px;
padding: 5px;
border: 1px solid #ccc;
color: #ccc;

border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;

-webkit-user-select: none; /* Chrome/Safari */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* IE10+ */
}
.scrollbtn:hover {
background: #c8e4f8; color: #000; border: 1px solid #94a0b4;
}

#c1 {
width: 96%;
overflow: auto;
padding: 2%;

}

#c2 {
width: 100000px;
}

.tree ul {
padding-top: 20px; position: relative;

-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
transition: all 0.5s;
}

.tree li {
float: left; text-align: center;
list-style-type: none;
position: relative;
padding: 20px 5px 0 5px;

-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
transition: all 0.5s;
}

/*We will use ::before and ::after to draw the connectors*/

.tree li::before, .tree li::after{
content: '';
position: absolute; top: 0; right: 50%;
border-top: 1px solid #ccc;
width: 50%; height: 32px;
z-index: -1;
}
.tree li::after{
right: auto; left: 50%;
border-left: 1px solid #ccc;
}

/*We need to remove left-right connectors from elements without
any siblings*/
.tree li:only-child::after, .tree li:only-child::before {
display: none;
}

/*Remove space from the top of single children*/
.tree li:only-child{ padding-top: 0;}
.tree li li:only-child{ float: none;}
/*Remove left connector from first child and
right connector from last child*/
.tree li:first-child::before, .tree li:last-child::after{
border: 0 none;
}
/*Adding back the vertical connector to the last nodes*/
.tree li:last-child::before{
border-right: 1px solid #ccc;
border-radius: 0 5px 0 0;

-webkit-transform: translateX(1px);
-moz-transform: translateX(1px);
transform: translateX(1px);

-webkit-border-radius: 0 5px 0 0;
-moz-border-radius: 0 5px 0 0;
border-radius: 0 5px 0 0;
}
.tree li:first-child::after{
border-radius: 5px 0 0 0;
-webkit-border-radius: 5px 0 0 0;
-moz-border-radius: 5px 0 0 0;
}

/*Time to add downward connectors from parents*/
.tree ul ul::before{
content: '';
position: absolute; top: -12px; left: 50%;
border-left: 1px solid #ccc;
width: 0; height: 32px;
z-index: -1;
}

.tree li a{
border: 1px solid #ccc;
padding: 5px 10px;
text-decoration: none;
color: #666;
font-family: arial, verdana, tahoma;
font-size: 11px;
display: inline-block;
background: white;

-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;

-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
transition: all 0.5s;
}
.tree li a+a {
margin-left: 20px;
position: relative;
}
.tree li a+a::before {
content: '';
position: absolute;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
top: 47%; left: -21px;
width: 20px;
height: 6%;
}

/*Time for some hover effects*/
/*We will apply the hover effect the the lineage of the element also*/
.tree li a:hover, .tree li a:hover~ul li a {
background: #c8e4f8; color: #000; border: 1px solid #94a0b4;
}
/*Connector styles on hover*/
.tree li a:hover~ul li::after,
.tree li a:hover~ul li::before,
.tree li a:hover~ul::before,
.tree li a:hover~ul ul::before
{
border-color: #94a0b4;
}

a {
width: 130px;
}
<html>
<head>
<link rel="stylesheet" type="text/css" href="tree.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="c1">
<div id="c2">
<div class="tree" id="tree"><ul><li><a href = "?Family=Haverlah&ID=43&Gen=5">Up Tree</a><ul><li><a href = "info.php?Family=Haverlah&ID=45&Gen=5">Rosa Haverlah</a><a href = "info.php?Family=Haverlah&ID=54&Gen=5">Charles Carl Schroeder</a><ul><li><a href = "info.php?Family=Haverlah&ID=55&Gen=5">William J. Schroeder</a><a href = "info.php?Family=Haverlah&ID=94&Gen=5">Pauline Stulken</a><ul><li><a href = "info.php?Family=Haverlah&ID=95&Gen=5">William J. Schroeder Jr.</a></li><li><a href = "info.php?Family=Haverlah&ID=96&Gen=5">Clarence Schroeder</a><a href = "info.php?Family=Haverlah&ID=216&Gen=5">Grace Nixon</a><ul><li><a href = "info.php?Family=Haverlah&ID=217&Gen=5">Marion Schroeder</a></li></ul></li></ul></li><li><a href = "info.php?Family=Haverlah&ID=56&Gen=5">Rosalinda Schroeder</a><a href = "info.php?Family=Haverlah&ID=97&Gen=5">John Pfeil</a><ul><li><a href = "info.php?Family=Haverlah&ID=98&Gen=5">Esther Hertha Pfeil</a><a href = "info.php?Family=Haverlah&ID=218&Gen=5">Theodor Frederick Winter</a><ul><li><a href = "info.php?Family=Haverlah&ID=219&Gen=5">Florence Winter</a></li><li><a href = "info.php?Family=Haverlah&ID=220&Gen=5">Sandra Jean Winter</a></li></ul></li><li><a href = "info.php?Family=Haverlah&ID=221&Gen=5">William Goode</a><a href = "info.php?Family=Haverlah&ID=99&Gen=5">Edna Eltice Pfeil</a><a href = "info.php?Family=Haverlah&ID=222&Gen=5">Waldemar Otto Rehfeld</a></li><li><a href = "info.php?Family=Haverlah&ID=100&Gen=5">Walton James Pfeil</a></li><li><a href = "info.php?Family=Haverlah&ID=101&Gen=5">John Wallis Pfeil</a><a href = "info.php?Family=Haverlah&ID=228&Gen=5">Mae Jeanette Ermler</a><ul><li><a href = "info.php?Family=Haverlah&ID=229&Gen=5">Darrell Kent Pfeil</a></li><li><a href = "info.php?Family=Haverlah&ID=230&Gen=5">Paul Wallis Pfeil</a></li><li><a href = "info.php?Family=Haverlah&ID=231&Gen=5">Leona Rose Pfeil</a></li><li><a href = "info.php?Family=Haverlah&ID=232&Gen=5">Julie Gay Pfeil</a></li></ul></li></ul></li><li><a href = "info.php?Family=Haverlah&ID=57&Gen=5">Charles William Schroeder</a><a href = "info.php?Family=Haverlah&ID=102&Gen=5">Natalie Brinkoeter</a><ul><li><a href = "info.php?Family=Haverlah&ID=103&Gen=5">Charles Astor Schroeder</a><a href = "info.php?Family=Haverlah&ID=233&Gen=5">Sue Rose Jones</a></li></ul></li><li><a href = "info.php?Family=Haverlah&ID=58&Gen=5">G. Adolph Schroeder</a><a href = "info.php?Family=Haverlah&ID=104&Gen=5">Helen Stribeck</a><ul><li><a href = "info.php?Family=Haverlah&ID=105&Gen=5">Lorena Schroeder</a></li><li><a href = "info.php?Family=Haverlah&ID=106&Gen=5">Helen Schroeder</a></li><li><a href = "info.php?Family=Haverlah&ID=107&Gen=5">Glen Schroeder</a></li></ul></li></ul></li></ul></li></ul></div> </div>
</div>
</body>
</html>

最佳答案

这是一个纯 CSS 解决方案,如果你没有附加到 float (这可能会给你带来其他困难 - 因为只有 float 子元素的父元素将有一个折叠的内容框,你的 #c2 div 有一个高度0 的,其内容是“卡在底部”)。

CSS 中的 3 个步骤:

  1. 移除

    #c2 {
    width: 100000px;
    }

    (您可以将 #c2 div 完全从您的 HTML 中取出)

  2. 添加

    .tree {
    white-space: nowrap;
    }
  3. 更改 .tree li 选择器中的规则,使该代码块看起来像这样:

    .tree li {
    display: inline-block;
    vertical-align: top;
    text-align: center;
    list-style-type: none;
    position: relative;
    padding: 20px 5px 0 5px;
    -webkit-transition: all 0.5s;
    -moz-transition: all 0.5s;
    transition: all 0.5s;
    }

这将使用 display: inline-block 将树的各个部分水平并排放置,使用 vertical-align: top 使它们垂直对齐在容器顶部,并在容器上放置 white-space: nowrap 以确保它们不会溢出到新行上。请注意,在您的 div #c1 上保留 overflow: auto 很重要,或者将 overflow-x: scroll 放在 div #tree 上,以启用水平滚动行为你想要的。

下面的工作片段:

* {margin: 0; padding: 0;}

.scrollbtn {
float: right;
cursor: pointer;
margin: 5px;
padding: 5px;
border: 1px solid #ccc;
color: #ccc;

border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;

-webkit-user-select: none; /* Chrome/Safari */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* IE10+ */
}
.scrollbtn:hover {
background: #c8e4f8; color: #000; border: 1px solid #94a0b4;
}

#c1 {
width: 96%;
overflow: auto;
padding: 2%;

}

/*modified*/
#c2 {
width: auto;
}

/*modified*/
.tree {
/*overflow-x: scroll;*/
white-space: nowrap;
}

.tree ul {
padding-top: 20px; position: relative;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
transition: all 0.5s;
}

/*modified*/
.tree li {
display: inline-block;
vertical-align: top;
text-align: center;
list-style-type: none;
position: relative;
padding: 20px 5px 0 5px;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
transition: all 0.5s;
}

/*We will use ::before and ::after to draw the connectors*/

.tree li::before, .tree li::after{
content: '';
position: absolute; top: 0; right: 50%;
border-top: 1px solid #ccc;
width: 50%; height: 32px;
z-index: -1;
}
.tree li::after{
right: auto; left: 50%;
border-left: 1px solid #ccc;
}

/*We need to remove left-right connectors from elements without
any siblings*/
.tree li:only-child::after, .tree li:only-child::before {
display: none;
}

/*Remove space from the top of single children*/
.tree li:only-child{ padding-top: 0;}
.tree li li:only-child{ float: none;}
/*Remove left connector from first child and
right connector from last child*/
.tree li:first-child::before, .tree li:last-child::after{
border: 0 none;
}
/*Adding back the vertical connector to the last nodes*/
.tree li:last-child::before{
border-right: 1px solid #ccc;
border-radius: 0 5px 0 0;

-webkit-transform: translateX(1px);
-moz-transform: translateX(1px);
transform: translateX(1px);

-webkit-border-radius: 0 5px 0 0;
-moz-border-radius: 0 5px 0 0;
border-radius: 0 5px 0 0;
}
.tree li:first-child::after{
border-radius: 5px 0 0 0;
-webkit-border-radius: 5px 0 0 0;
-moz-border-radius: 5px 0 0 0;
}

/*Time to add downward connectors from parents*/
.tree ul ul::before{
content: '';
position: absolute; top: -12px; left: 50%;
border-left: 1px solid #ccc;
width: 0; height: 32px;
z-index: -1;
}

.tree li a{
border: 1px solid #ccc;
padding: 5px 10px;
text-decoration: none;
color: #666;
font-family: arial, verdana, tahoma;
font-size: 11px;
display: inline-block;
background: white;

-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;

-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
transition: all 0.5s;
}
.tree li a+a {
margin-left: 20px;
position: relative;
}
.tree li a+a::before {
content: '';
position: absolute;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
top: 47%; left: -21px;
width: 20px;
height: 6%;
}

/*Time for some hover effects*/
/*We will apply the hover effect the the lineage of the element also*/
.tree li a:hover, .tree li a:hover~ul li a {
background: #c8e4f8; color: #000; border: 1px solid #94a0b4;
}
/*Connector styles on hover*/
.tree li a:hover~ul li::after,
.tree li a:hover~ul li::before,
.tree li a:hover~ul::before,
.tree li a:hover~ul ul::before
{
border-color: #94a0b4;
}

a {
width: 130px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8" />
<meta name="viewport" content="initial-scale=1.0, width=device-width" />
<title>fiddle</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<html>
<head>
<link rel="stylesheet" type="text/css" href="tree.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="c1">
<div id="c2">
<div class="tree" id="tree"><ul><li><a href = "?Family=Haverlah&ID=43&Gen=5">Up Tree</a><ul><li><a href = "info.php?Family=Haverlah&ID=45&Gen=5">Rosa Haverlah</a><a href = "info.php?Family=Haverlah&ID=54&Gen=5">Charles Carl Schroeder</a><ul><li><a href = "info.php?Family=Haverlah&ID=55&Gen=5">William J. Schroeder</a><a href = "info.php?Family=Haverlah&ID=94&Gen=5">Pauline Stulken</a><ul><li><a href = "info.php?Family=Haverlah&ID=95&Gen=5">William J. Schroeder Jr.</a></li><li><a href = "info.php?Family=Haverlah&ID=96&Gen=5">Clarence Schroeder</a><a href = "info.php?Family=Haverlah&ID=216&Gen=5">Grace Nixon</a><ul><li><a href = "info.php?Family=Haverlah&ID=217&Gen=5">Marion Schroeder</a></li></ul></li></ul></li><li><a href = "info.php?Family=Haverlah&ID=56&Gen=5">Rosalinda Schroeder</a><a href = "info.php?Family=Haverlah&ID=97&Gen=5">John Pfeil</a><ul><li><a href = "info.php?Family=Haverlah&ID=98&Gen=5">Esther Hertha Pfeil</a><a href = "info.php?Family=Haverlah&ID=218&Gen=5">Theodor Frederick Winter</a><ul><li><a href = "info.php?Family=Haverlah&ID=219&Gen=5">Florence Winter</a></li><li><a href = "info.php?Family=Haverlah&ID=220&Gen=5">Sandra Jean Winter</a></li></ul></li><li><a href = "info.php?Family=Haverlah&ID=221&Gen=5">William Goode</a><a href = "info.php?Family=Haverlah&ID=99&Gen=5">Edna Eltice Pfeil</a><a href = "info.php?Family=Haverlah&ID=222&Gen=5">Waldemar Otto Rehfeld</a></li><li><a href = "info.php?Family=Haverlah&ID=100&Gen=5">Walton James Pfeil</a></li><li><a href = "info.php?Family=Haverlah&ID=101&Gen=5">John Wallis Pfeil</a><a href = "info.php?Family=Haverlah&ID=228&Gen=5">Mae Jeanette Ermler</a><ul><li><a href = "info.php?Family=Haverlah&ID=229&Gen=5">Darrell Kent Pfeil</a></li><li><a href = "info.php?Family=Haverlah&ID=230&Gen=5">Paul Wallis Pfeil</a></li><li><a href = "info.php?Family=Haverlah&ID=231&Gen=5">Leona Rose Pfeil</a></li><li><a href = "info.php?Family=Haverlah&ID=232&Gen=5">Julie Gay Pfeil</a></li></ul></li></ul></li><li><a href = "info.php?Family=Haverlah&ID=57&Gen=5">Charles William Schroeder</a><a href = "info.php?Family=Haverlah&ID=102&Gen=5">Natalie Brinkoeter</a><ul><li><a href = "info.php?Family=Haverlah&ID=103&Gen=5">Charles Astor Schroeder</a><a href = "info.php?Family=Haverlah&ID=233&Gen=5">Sue Rose Jones</a></li></ul></li><li><a href = "info.php?Family=Haverlah&ID=58&Gen=5">G. Adolph Schroeder</a><a href = "info.php?Family=Haverlah&ID=104&Gen=5">Helen Stribeck</a><ul><li><a href = "info.php?Family=Haverlah&ID=105&Gen=5">Lorena Schroeder</a></li><li><a href = "info.php?Family=Haverlah&ID=106&Gen=5">Helen Schroeder</a></li><li><a href = "info.php?Family=Haverlah&ID=107&Gen=5">Glen Schroeder</a></li></ul></li></ul></li></ul></li></ul></div> </div>
</div>
</body>
</html>
</body>
</html>

关于javascript - 将 Parent Div 收缩为 Foated Children,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51574617/

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