简体   繁体   English

检查元素是否设置了css position属性

[英]check if element has css position property setted up

How to be sure if parent element has css position property setted up ? 如何确定父元素是否设置了css position属性?

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')
}

Some like this work on cross browsers ? 像这样的跨浏览器可以工作吗?

Please follow the below method : 请按照以下方法:

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 
}

So based on @Danish suggestion (copy-pasted from : Detect whether an element has position:fixed (possibly by parent element) via jQuery ) 因此,基于@Danish建议(复制粘贴自: 通过jQuery检测元素是否具有position:fixed(可能由父元素固定)

I write this: 我这样写:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM