如何确定父元素是否设置了css位置属性?
if(!$('#element').parent().css('position')){
//here I need to be sure to avoid no rewrite the old position if position is already setted up
//absolute fixed etc....
$('#element').parent().css('position','relative')
}
有些人喜欢这种跨浏览器的工作吗?
答案 0 :(得分:0)
请按照以下方法:
function elementOrParentIsFixed(element) {
var $element = $(element);
var $checkElements = $element.add($element.parents());
var isFixed = false;
$checkElements.each(function(){
if ($(this).css("position") === "relative") {
isFixed = true;
return false;
}
});
return
}
答案 1 :(得分:0)
因此基于@Danish建议(复制粘贴自:Detect whether an element has position:fixed (possibly by parent element) via jQuery)
我写这个:
function parentHasPosition(element){
var parent = element.parent();
if(
parent.css('position') === 'static' ||
parent.css('position') === 'absolute' ||
parent.css('position') === 'fixed' ||
parent.css('position') === 'sticky'
){
return true;
}
return false;
}