Skip to content

Commit 5e89481

Browse files
committed
Added zIndex method. Fixes #4709 - Add zIndex setter/getter method.
1 parent 44d3893 commit 5e89481

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

tests/unit/core/core.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@
9393
</div>
9494

9595
<div id="aria"></div>
96+
97+
<div id="zIndex100" style="z-index: 100;">
98+
<div id="zIndexAutoWithParent"></div>
99+
</div>
100+
<div id="zIndexAutoNoParent"></div>
96101
</div>
97102

98103
</body>

tests/unit/core/core.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,13 @@ test('focus', function() {
4848
other.focus();
4949
});
5050

51+
test('zIndex', function() {
52+
var el = $('#zIndexAutoWithParent');
53+
equals(el.zIndex(), 100, 'zIndex traverses up to find value');
54+
equals(el.zIndex(200), el, 'zIndex setter is chainable');
55+
equals(el.zIndex(), 200, 'zIndex setter changed zIndex');
56+
57+
equals($('#zIndexAutoNoParent').zIndex(), 0, 'zIndex never explicitly set in hierarchy');
58+
});
59+
5160
})(jQuery);

ui/ui.core.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,26 @@ $.fn.extend({
184184
}
185185

186186
return (/fixed/).test(this.css('position')) || !scrollParent.length ? $(document) : scrollParent;
187+
},
188+
189+
zIndex: function(zIndex) {
190+
if (zIndex !== undefined) {
191+
return this.css('zIndex', zIndex);
192+
}
193+
194+
var elem = this[0];
195+
while (elem && elem.style) {
196+
// IE returns 0 when zIndex is not specified
197+
// other browsers return an empty string
198+
// we ignore the case of nested elements with an explicit value of 0
199+
// <div style="z-index: -10;"><div style="z-index: 0;"></div></div>
200+
if (elem.style.zIndex !== '' && elem.style.zIndex !== 0) {
201+
return +elem.style.zIndex;
202+
}
203+
elem = elem.parentNode;
204+
}
205+
206+
return 0;
187207
}
188208
});
189209

0 commit comments

Comments
 (0)