Expand div with "display: table-cell" on click?
Expand div with "display: table-cell" on click?
我的页面上有一系列的组件:
- 我希望他们都至少是
150px
高。 - 如果他们不是
150px
高,我想垂直居中他们的文字。 - 如果它们超过
150px
,我想在150px
切断文本并有一个显示更多按钮,如果需要,扩展它。
现在,第一种情况,他们不是150px
高的工作很好,文本居中。问题是我不能得到组件与height > 150px
切断他们的内容。
我试图做到这一点没有jQuery。
这是一个演示问题的代码。
HTML: <div class="containerDiv">
<div id="content1" class="contentDiv">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
<br />
<button class="showMoreButton" onclick="showMore()">Show More</button>
</div>
</div>
<br />
<div class="containerDiv">
<div id="content2" class="contentDiv">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
<br />
<button class="showMoreButton" onclick="showMore()">Show More</button>
</div>
<br />
</div>
CSS: .containerDiv {
display: table;
min-height: 150px;
width: 400px;
}
.contentDiv {
display: table-cell;
max-height: 150px;
overflow: hidden;
vertical-align: middle;
}
.showMoreButton {
display: none;
}
JS:
var contentDivs = document.getElementsByClassName("contentDiv");
for(var i = 0; i < contentDivs.length; i++) {
if(contentDivs[i].offsetHeight > 150) {
contentDivs[i].getElementsByTagName("button")[0].style.display = "inline-block";
}
}
function showMore() {
console.log(event.path[1].id);
document.getElementById(event.path[1].id).style.maxHeight = "none";
}
好的,我发现不能为显示为表格单元格的元素设置max-height
所以,我把我的文本包装在div
中,设置所有的max-height
和overflow
属性,它工作得很好。
这是一个更新的代码
HTML: <div class="containerDiv">
<div id="content1" class="contentDiv">
<div class="content">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
<button class="showMoreButton" onclick="showMore()">Show More</button>
</div>
<br />
</div>
</div>
<br />
<div class="containerDiv">
<div id="content2" class="contentDiv">
<div class="content">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
<br />
<button class="showMoreButton" onclick="showMore()">Show More</button>
</div>
<br />
</div>
CSS: .containerDiv {
display: table;
min-height: 150px;
overflow: hidden;
width: 400px;
}
.contentDiv {
display: table-cell;
vertical-align: middle;
}
.content {
max-height: 150px;
overflow: hidden;
text-overflow: ellipsis;
}
.showMoreButton {
display: none;
}
JS:
var contentDivs = document.getElementsByClassName("contentDiv");
for(var i = 0; i < contentDivs.length; i++) {
if(contentDivs[i].offsetHeight > 150) {
contentDivs[i].getElementsByTagName("button")[0].style.display = "inline-block";
}
}
function showMore() {
document.getElementById(event.path[1].id).getElementsByClassName("content")[0].style.maxHeight = "none";
}
尝试css样式word-wrap:ellipsis
。
相关文章:
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 铬:“;未捕获的语法错误:意外的标记:"
- 可以设置“;文件名"发生错误时显示的内联脚本标记的
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 使用“+="操作人员
- I'我设计了一个下拉菜单,onclick会出现,而on blur会消失
- window.on.scroll事件未启动
- //而不是在src=“”上使用http://"属性
- "未捕获的语法错误:意外的标记}"
- 可以<脚本类型=“;text/javascript”>window.location=“/"</
- "实例范围”;TypeScript类的getter/setter
- 我怎么能读“;.on()"jQuery中的方法参数
- Ruby on Rails屏幕抓取错误-“;您必须在浏览器中启用javascript才能使用此应用程序"
- 如何使用JQuery's”;.on()"方法来处理非本机jquery元素
- jQuery:"点击“;绕过了.on()函数中的param
- JS-angular为什么使用“ng click=”;f()"`属性,而不是`$(el).on('cl
- "上下文”;Firebase.on的参数不起作用
- jQuery-如何使用;on()"方法代替“;live()”;
- 是否存在使用“;JQuery.on()"大量地