首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将父母分部缩小为指定的儿童

将父母分部缩小为指定的儿童
EN

Stack Overflow用户
提问于 2018-07-28 19:36:40
回答 1查看 41关注 0票数 0

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

“我的树”是一系列列表元素,所有元素都向左浮动。

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

现在发生的情况是,如果树的宽度大于#c1,那么树将自己包装,使其不可读。

我临时修正了这个问题,在#c1中添加了一个名为#c2的div,宽度为1000000 of。基本上,这棵树永远不会变大,所以它永远不会自己卷进来,用户可以在树上滚动。

我想要的是,在加载javascript时,以某种方式将#c2的宽度与树相匹配,或者以某种方式找到一个css解决方案。

我试过使用空白:现在,和许多其他解决方案,我已经找到了类似的问题,但似乎没有什么对我有用。

代码语言:javascript
复制
* {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;
}
代码语言:javascript
复制
<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>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-28 20:19:24

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

CSS中的3个步骤:

  1. 删除 #c2 {宽度: 100000px;} (您可以将#c2div完全从HTML中删除)
  2. 添加 .tree {空白: nowrap;}
  3. 更改.tree li选择器中的规则,使代码块看起来如下所示: .tree li {显示:内联块;垂直对齐:顶部;文本对齐:中心;列表样式类型:无;位置:相对;填充:20 px5px0 5px;-webkit-转换:所有0.5s;-moz-转换:全部0.5s;转换:所有0.5s;

这将使用display: inline-block将树的碎片水平地放置在一起,使用vertical-align: top将它们垂直对齐到容器的顶部,并在容器上放置white-space: nowrap,以确保它们不会流到新的线路上。请注意,保持div #c1上的overflow: auto很重要,或者将overflow-x: scroll放在div #树上,以启用您想要的水平滚动行为。

下面是工作片段:

代码语言:javascript
复制
* {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;
}
代码语言:javascript
复制
<!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>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51574617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档