添加最顶层js广告,CSS / JavaScript:使元素最顶层的z-index /最顶层的模态元素

I would like to make an element (e.g. a

) be the top-most layer on the page.

My assumption is that the only way I can do this is to specify that the element has a style="z-index:" value that is the maximum the browser allows (int32?).

Is this correct?

Instead, would it be possible to somehow get the element's z-index whose is highest, and make this

's z-index the [highest element's value] + 1? For example:

$myDiv.css("z-index", $(document.body).highestZIndex() + 1);

How do modal JavaScript "windows" work?

解决方案

Here's how to do it :

var elements = document.getElementsByTagName("*");

var highest_index = 0;

for (var i = 0; i < elements.length - 1; i++) {

if (parseInt(elements[i].style.zIndex) > highest_index) {

highest_index = parseInt(elements[i].style.zIndex;

}

}

highest_index now contains the highest z-index on the page... just add 1 to that value and apply it wherever you want. You can apply it like so :

your_element.style.zIndex = highest_index + 1;

Here's another way of achieving the same thing using jQuery :

var highest_index = 0;

$("[z-index]").each(function() {

if ($(this).attr("z-index") > highest_index) {

highest_index = $(this).attr("z-index");

}

});

Again, same way to apply the new index to an element :

$("your_element").attr("z-index", highest_index + 1);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值