How to find the highest z-index using jQuery

后端 未结 11 1143
终归单人心
终归单人心 2020-11-28 07:42

I have a number of div elements with different z-index. And I want to find the highest z-index among these divs - how can I achieve it?

CSS:

#layer-1         


        
11条回答
  •  暖寄归人
    2020-11-28 08:21

    This is taken directly from jquery-ui, it works really well:

    (function ($) {
      $.fn.zIndex = function (zIndex) {
          if (zIndex !== undefined) {
            return this.css("zIndex", zIndex);
          }
    
          if (this.length) {
            var elem = $(this[ 0 ]), position, value;
            while (elem.length && elem[ 0 ] !== document) {
              // Ignore z-index if position is set to a value where z-index is ignored by the browser
              // This makes behavior of this function consistent across browsers
              // WebKit always returns auto if the element is positioned
              position = elem.css("position");
              if (position === "absolute" || position === "relative" || position === "fixed") {
                // IE returns 0 when zIndex is not specified
                // other browsers return a string
                // we ignore the case of nested elements with an explicit value of 0
                // 
    value = parseInt(elem.css("zIndex"), 10); if (!isNaN(value) && value !== 0) { return value; } } elem = elem.parent(); } } return 0; } })(jQuery);

提交回复
热议问题