html - 检测旁边有右对齐元素的溢出
问题描述
我有带有右对齐向下箭头的表格标题,以指示表格何时按该列排序。这些列可以调整大小,如果标题溢出并因此被截断,我需要在鼠标悬停时显示工具提示...
。但是,我无法弄清楚在这种情况下如何正确检测溢出。我尝试了两种方法:向右浮动箭头和使用 flexbox 定位。
这是两种方法的小提琴。
该小提琴中的第 2 列有一个右浮动箭头,第 3 列使用 flex。问题:
- 使用 float 不会导致和之间的差异
offsetWidth
,scrollWidth
直到列太窄以至于溢出超出浮动元素。您可以通过将#col-2
宽度更改为60px
. 文本被正确截断,但不能可靠地使用这些宽度。 - 使用 flex,无论列宽如何,都始终相同
offsetWidth
。scrollWidth
使用 正确截断文本...
,但这两个属性永远不会不同。
如何使用该示例中给出的布局正确检测溢出?我不想要任何涉及克隆元素的方法。我的样式表太复杂了,这种方法无法正常工作,而且它的性能也很糟糕。
小提琴的代码在下面,所以它也包含在这个问题中。
document.querySelectorAll('span.ellipsis-overflow').forEach((el) => {
if (el.offsetWidth < el.scrollWidth) {
el.style.color = 'red';
}
});
#col-2 {
width: 80px; /* set to 60px to trigger overflow detection */
}
#col-3 {
width: 80px;
}
.float i {
float: right;
}
span.flex-container {
display: flex;
flex-direction: row;
}
span.flex-container i {
margin-left: auto;
}
.ellipsis-overflow {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.ellipsis-overflow * {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* below this is just style stuff to make the problem easier to see */
table {
border-collapse: collapse;
text-align: left;
table-layout: fixed;
width: 400px;
}
th {
border: 1px solid black;
}
i.material-icons {
font-size: 16px !important;
display: inline-flex;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<p>Column text will be red if <code>offsetWidth</code> is less than
<code>scrollWidth</code>, indicating overflow. Trigger this in column 2 by setting
its width to 60px.</p>
<table>
<thead>
<tr>
<th>Column 1</th>
<th id="col-2" class="float">
<span class="ellipsis-overflow">
<i class="material-icons">arrow_downward</i>
Column 2
</span>
</th>
<th id="col-3">
<span class="ellipsis-overflow">
<span class="flex-container">
<span>Column 3</span>
<i class="material-icons">arrow_downward</i>
</span>
</span>
</th>
</tr>
</thead>
</table>
解决方案
原始答案
您可以设置overflow: auto
条件之前并将其设置回hidden
条件之后。:)
请参阅下面的片段(我已将col-2
宽度设置为60px
):
document.querySelectorAll('span.ellipsis-overflow').forEach((el) => {
el.style.overflow = "auto";
if (el.offsetWidth < el.scrollWidth) {
el.style.color = 'red';
}
el.style.overflow = "hidden";
});
#col-2 {
width: 60px; /* set to 60px to trigger overflow detection */
}
#col-3 {
width: 80px;
}
.float i {
float: right;
}
span.flex-container {
display: flex;
flex-direction: row;
}
span.flex-container i {
margin-left: auto;
}
.ellipsis-overflow {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.ellipsis-overflow * {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* below this is just style stuff to make the problem easier to see */
table {
border-collapse: collapse;
text-align: left;
table-layout: fixed;
width: 400px;
}
th {
border: 1px solid black;
}
i.material-icons {
font-size: 16px !important;
display: inline-flex;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<p>Column text will be red if <code>offsetWidth</code> is less than
<code>scrollWidth</code>, indicating overflow. Trigger this in column 2 by setting
its width to 60px.</p>
<table>
<thead>
<tr>
<th>Column 1</th>
<th id="col-2" class="float">
<span class="ellipsis-overflow">
<i class="material-icons">arrow_downward</i>
Column 2
</span>
</th>
<th id="col-3">
<span class="ellipsis-overflow">
<span class="flex-container">
<span>Column 3</span>
<i class="material-icons">arrow_downward</i>
</span>
</span>
</th>
</tr>
</thead>
</table>
更新的答案
我认为您可以对父高度进行相同的检查。
请参阅下面的片段:
document.querySelectorAll('span.ellipsis-overflow').forEach((el) => {
var initialHeight = el.parentElement.clientHeight;
el.classList.add("letMeCheckWidth");
if (initialHeight < el.parentElement.clientHeight) {
el.style.color = 'red';
}
el.classList.remove("letMeCheckWidth");
});
#col-2 {
width: 80px; /* set to 60px to trigger overflow detection */
}
#col-3 {
width: 80px;
}
.float i {
float: right;
}
span.flex-container {
display: flex;
flex-direction: row;
}
span.flex-container i {
margin-left: auto;
}
.ellipsis-overflow {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.ellipsis-overflow * {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.ellipsis-overflow.letMeCheckWidth {
display: block;
overflow: auto;
text-overflow: inherit;
white-space: normal;
}
.ellipsis-overflow.letMeCheckWidth * {
overflow: auto;
text-overflow: inherit;
white-space: normal;
}
/* below this is just style stuff to make the problem easier to see */
table {
border-collapse: collapse;
text-align: left;
table-layout: fixed;
width: 400px;
}
th {
border: 1px solid black;
}
i.material-icons {
font-size: 16px !important;
display: inline-flex;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<p>Column text will be red if <code>offsetWidth</code> is less than <code>scrollWidth</code>, indicating overflow. Trigger this in column 2 by setting its width to 60px.</p>
<table>
<thead>
<tr>
<th>Column 1</th>
<th id="col-2" class="float">
<span class="ellipsis-overflow">
<i class="material-icons">arrow_downward</i>
Column 2
</span>
</th>
<th id="col-3">
<span class="ellipsis-overflow">
<span class="flex-container">
<span>Column 3</span>
<i class="material-icons">arrow_downward</i>
</span>
</span>
</th>
</tr>
</thead>
</table>
您也可以在jsfiddle上对其进行测试..
推荐阅读
- selenium - 想要点击 naukri.com 中的第一份工作链接以获取任何关键字,例如通过 selenium 说 java,但它给了我错误
- python - 需要将字典值附加到字典
- javascript - 否则不改变和读取图像大小上传jQuery
- javascript - ReactJS:从 API 中排序选项选择
- c - C中的重音和fopen问题
- jquery - 从控制器获取值并显示警报,因为至少一个值应该处于活动状态
- android - 如何在 coordinatorLayout 内的 RelativeLayout 底部对齐 LinearLayout
- python - 将少量包裹装入固定数量的箱中
- python - 当用户输入不正确时如何从某个特定点重复代码
- javascript - 猫鼬不保存对文档的更改