3 回答
TA贡献1794条经验 获得超7个赞
这是解决方法: 1. 更改 html 结构:
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
使用伪元素来定位 div 的属性。(在css之前添加)
删除位置:工具提示所依赖的相对位置。
移除了 top 和 left 属性,因为它们依赖于 position relative 并替换为 margin 。
下面是固定代码:
.ResultsWrapper {
width:150px;
height:314px;
text-align:center;
overflow-x:hidden;
overflow-y:scroll;
border:1px solid black;
}
.ResultsTable {
width:86px;
border-collapse:collapse;
table-layout:fixed;
}
.ResultsTable th, .ResultsTable td {
border:1px solid black;
text-overflow:ellipsis;
}
.ColumnSerialNo {
width:81px;
}
.SerialNumberContainer {
z-index: 10;
}
.SerialNumber {
width: 80px;
overflow: hidden;
}
.SerialNumber:hover::before {
position: absolute;
content:attr(data-before-content);
margin:10px 0 0 50px;
background-color: #fff;
border: 1px solid #CCC;
display: block;
z-index:9999;
}
.SerialNumber:hover{}
<div class="ResultsWrapper">
<table class="ResultsTable">
<thead>
<tr>
<th class="ColumnSerialNo">Serial Number</th>
</tr>
</thead>
<tbody>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="aaaaaaa3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr><tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber" data-before-content="3119985815206">3119985815206</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
TA贡献1806条经验 获得超8个赞
您不能使用 Position absolute 和 z-index 将元素定位在滚动条上。如果要在滚动条顶部显示元素,则需要使用位置固定元素。
.ResultsWrapper {
width:150px;
height:314px;
text-align:center;
overflow-y:scroll;
border:1px solid black;
z-index:-1;
}
.ResultsTable {
width:86px;
border-collapse:collapse;
table-layout:fixed;
}
.ResultsTable th, .ResultsTable td {
border:1px solid black;
text-overflow:ellipsis;
}
.ColumnSerialNo {
width:81px;
}
.SerialNumberContainer {
position: relative;
z-index: 10;
}
.SerialNumber {
width: 80px;
overflow: hidden;
}
.SerialNumberTooltip {
position: fixed;
top: 10px;
left: 70px;
background-color: #FFF;
border: 1px solid #CCC;
display: none;
z-index:99999999999;
}
.SerialNumberContainer:hover {
z-index: 20;
}
.SerialNumberContainer:hover .SerialNumberTooltip {
display: block;
z-index: 40;
}
<div class="ResultsWrapper">
<table class="ResultsTable">
<thead>
<tr>
<th class="ColumnSerialNo">Serial Number</th>
</tr>
</thead>
<tbody>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
检查下面的jsfiddle。
http://jsfiddle.net/rajeevRF/eoc8yv5a/5/
TA贡献1851条经验 获得超4个赞
我认为您可以使用包装类( HTML 中的.TooltipWrapper),如下所示:
document.querySelectorAll('.SerialNumber').forEach(function(el){
el.addEventListener('mousemove',function(ele){
var x = ele.pageY;
el.parentNode.querySelector('.TooltipWrapper').style.top = `${x}px`;
});
});
.ResultsWrapper {
width:150px;
height:314px;
text-align:center;
overflow-x:hidden;
overflow-y:scroll;
border:1px solid black;
}
.ResultsTable {
width:86px;
border-collapse:collapse;
table-layout:fixed;
}
.ResultsTable th, .ResultsTable td {
border:1px solid black;
}
.ColumnSerialNo {
width:81px;
}
.SerialNumber {
width: 80px;
overflow: hidden;
}
.TooltipWrapper {
position: absolute;
visibility: hidden;
}
.hasTooltip:hover .TooltipWrapper {
visibility: visible;
opacity: 1;
}
.SerialNumberTooltip {
display: block;
--margin-top: -20px;
margin-left: 85px;
background-color: #FFF;
border: 1px solid #CCC;
}
<div class="ResultsWrapper">
<table class="ResultsTable">
<thead>
<tr>
<th class="ColumnSerialNo">Serial Number</th>
</tr>
</thead>
<tbody>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815206</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815206</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815207</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815207</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815208</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815208</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815209</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815209</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815210</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815210</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815211</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815211</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815212</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815212</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815213</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815213</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815214</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815214</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815215</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815215</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815216</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815216</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815217</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815217</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815218</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815218</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815219</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815219</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815220</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815220</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815221</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815221</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815222</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815222</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815223</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815223</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815224</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815224</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815225</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815225</div>
</div>
</div>
</td>
</tr>
<tr>
<td class="hasTooltip">
<div class="SerialNumberContainer">
<div class="SerialNumber">3119985815226</div>
<div class="TooltipWrapper">
<div class="SerialNumberTooltip">3119985815226</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
添加回答
举报