簡體   English   中英

檢查元素是否設置了css position屬性

[英]check if 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')
}

像這樣的跨瀏覽器可以工作嗎?

請按照以下方法:

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 
}

因此,基於@Danish建議(復制粘貼自: 通過jQuery檢測元素是否具有position:fixed(可能由父元素固定)

我這樣寫:

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