如何使用 javascript 获取我们知道其偏移量顶部的元素
How to get element whose offset top we are knowing using javascript
假设我们知道特定元素的顶部偏移量,并且我们想要获取位于该点的元素,那么有什么方法可以获取它吗?
例如,我们有 DOM 个具有 offset top = 6000 的元素,我们想要找到位于该偏移顶点的元素。
示例:
<ul>
<li>first list</li>
<li>second list</li>
<li>third list</li>
</ul>
<h3>Skills:</h3>
<p>This are my skills. xyz, abc, pqr</p>
现在,假设 offsetTop of <p>
element 是 10我们希望整个元素的意思是 <p>This are my skills. xyz, abc, pqr</p>
,只需使用它的 offsetTop.
如 this article 所述,无法查询应用了特定样式的元素。您可以查询页面中的所有元素并循环遍历它们,检查它们的样式,但这会导致大量开销。
不过,如果您可以根据父元素、类型等缩小可能的元素范围,这可能是可行的。
我的问题似乎是寻找特定点的元素。如果是这样,您可以使用 document.elementFromPoint()
.
let x = 10;
let y = 140;
let el = document.elementFromPoint(x, y);
el.style.background = "red";
// or $(el).css("background", "red");
<ul>
<li>first list</li>
<li>second list</li>
<li>third list</li>
</ul>
<h3>Skills:</h3>
<p>This are my skills. xyz, abc, pqr</p>
如果您只想通过顶部偏移,例如因为您不想指定 x
坐标,所以您必须遍历元素。
红线显示要检查的顶部偏移量。添加并返回在此行之前开始和之后结束的所有元素。它们以蓝色背景显示。
/**
* Get all children that start before `offset_top` and end after
* `offset_top`.
*
* @param {(HTMLElement|jQuery|string)} parent
* The parent as the element or as the selector
* @param {number} offset_top
* The top offset to check
*
* @return {jQuery}
* All elements that are on the specific location.
*/
function findChildrenWithOffset(parent, offset_top){
let found = $([]);
$(parent).children().each(function(){
let t = $(this);
let o = t.offset();
let start = o["top"];
let end = start + t.outerHeight();
if(start <= offset_top && offset_top <= end){
found = found.add(t);
}
if(t.children().length > 0){
t.children().each(function(){
found = found.add(findChildrenWithOffset(this, offset_top));
});
}
});
return found;
}
let c = findChildrenWithOffset(document, 140);
$(c).css("background", "rgba(0, 0, 255, 0.2)");
.absolute{
position: absolute;
height: 20px;
width: 20px;
border: 1px solid green;
}
.offset-line{
position: absolute;
top: 139px;
left: 0;
right: 0;
background: red!important;
height: 2px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li>first list</li>
<li>second list</li>
<li>third list</li>
</ul>
<h3>Skills:</h3>
<p><span style="text-decoration: underline;">This</span> are my skills. xyz, abc, pqr</p>
<div class="absolute" style="left: 220px; top: 110px;">
A1
</div>
<div class="absolute" style="left: 240px; top: 120px;">
A2
</div>
<div class="absolute" style="left: 260px; top: 130px;">
A3
</div>
<div class="absolute" style="left: 280px; top: 140px;">
A4
</div>
<div class="absolute" style="left: 300px; top: 150px;">
A5
</div>
<div class="offset-line"></div>
假设我们知道特定元素的顶部偏移量,并且我们想要获取位于该点的元素,那么有什么方法可以获取它吗?
例如,我们有 DOM 个具有 offset top = 6000 的元素,我们想要找到位于该偏移顶点的元素。
示例:
<ul>
<li>first list</li>
<li>second list</li>
<li>third list</li>
</ul>
<h3>Skills:</h3>
<p>This are my skills. xyz, abc, pqr</p>
现在,假设 offsetTop of <p>
element 是 10我们希望整个元素的意思是 <p>This are my skills. xyz, abc, pqr</p>
,只需使用它的 offsetTop.
如 this article 所述,无法查询应用了特定样式的元素。您可以查询页面中的所有元素并循环遍历它们,检查它们的样式,但这会导致大量开销。
不过,如果您可以根据父元素、类型等缩小可能的元素范围,这可能是可行的。
我的问题似乎是寻找特定点的元素。如果是这样,您可以使用 document.elementFromPoint()
.
let x = 10;
let y = 140;
let el = document.elementFromPoint(x, y);
el.style.background = "red";
// or $(el).css("background", "red");
<ul>
<li>first list</li>
<li>second list</li>
<li>third list</li>
</ul>
<h3>Skills:</h3>
<p>This are my skills. xyz, abc, pqr</p>
如果您只想通过顶部偏移,例如因为您不想指定 x
坐标,所以您必须遍历元素。
红线显示要检查的顶部偏移量。添加并返回在此行之前开始和之后结束的所有元素。它们以蓝色背景显示。
/**
* Get all children that start before `offset_top` and end after
* `offset_top`.
*
* @param {(HTMLElement|jQuery|string)} parent
* The parent as the element or as the selector
* @param {number} offset_top
* The top offset to check
*
* @return {jQuery}
* All elements that are on the specific location.
*/
function findChildrenWithOffset(parent, offset_top){
let found = $([]);
$(parent).children().each(function(){
let t = $(this);
let o = t.offset();
let start = o["top"];
let end = start + t.outerHeight();
if(start <= offset_top && offset_top <= end){
found = found.add(t);
}
if(t.children().length > 0){
t.children().each(function(){
found = found.add(findChildrenWithOffset(this, offset_top));
});
}
});
return found;
}
let c = findChildrenWithOffset(document, 140);
$(c).css("background", "rgba(0, 0, 255, 0.2)");
.absolute{
position: absolute;
height: 20px;
width: 20px;
border: 1px solid green;
}
.offset-line{
position: absolute;
top: 139px;
left: 0;
right: 0;
background: red!important;
height: 2px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li>first list</li>
<li>second list</li>
<li>third list</li>
</ul>
<h3>Skills:</h3>
<p><span style="text-decoration: underline;">This</span> are my skills. xyz, abc, pqr</p>
<div class="absolute" style="left: 220px; top: 110px;">
A1
</div>
<div class="absolute" style="left: 240px; top: 120px;">
A2
</div>
<div class="absolute" style="left: 260px; top: 130px;">
A3
</div>
<div class="absolute" style="left: 280px; top: 140px;">
A4
</div>
<div class="absolute" style="left: 300px; top: 150px;">
A5
</div>
<div class="offset-line"></div>