将家长Div缩小为有子女的孩子

时间:2018-07-28 19:36:41

标签: javascript html css

我正在尝试创建一棵家谱,该功能的最后一部分给了我一些麻烦。

我的树是一系列列表元素,所有这些元素都浮动到左侧。

我想要的是,如果树扩展到超过屏幕大小,则父div#c1应该允许用户在树上水平滚动。

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

我通过在#c1内添加一个宽度为100000px的div来临时解决此问题。基本上,树永远不会变大,因此它不会自动包裹,用户可以在树上滚动。

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

我已经尝试使用空格:nowrap,以及针对类似问题的许多其他解决方案,但是似乎没有任何效果。

<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>
{{1}}

1 个答案:

答案 0 :(得分:1)

这是一个纯CSS解决方案,如果您不喜欢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则在容器上确保它们不会流到新行中。请注意,将overflow: auto保持在div#c1上,或者将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>