【发布时间】:2013-10-28 18:04:00
【问题描述】:
更新我想避免遍历所有元素的父元素,依次测试每个元素,但这是迄今为止我设法找到的最佳解决方案,如 this answer .
在事件处理程序中,我想检测目标元素的位置是相对于视口(固定)还是文档(静态、相对或绝对)。
考虑到一个元素的位置可能是固定的,因为它具有“position:fixed”,或者因为它的父元素之一是“position:fixed”,那么检测元素固定位置的有效方法是什么?
例如:
CSS
#one {position:relative; height:10px; width:10px}
#two-wrapper {position:fixed; bottom:0; height:10px; width:10px}
#two {height:10px; width:10px}
HTML
<div id="one" class="trigger-event">element one</div>
<div id="two-wrapper">
<div id="two" class="trigger-event">element two</div>
</div>
JS
$(document).ready(function(){
$('.trigger-event').on('mouseenter', function(event){
var isFixed = .... ????
if (isFixed) {
$(event.target).css('background','red');
} else {
$(event.target).css('background','green');
}
});
});
【问题讨论】:
标签: javascript jquery html css css-position