简体   繁体   English

如何在不依赖页面中使用的结构/样式的情况下,在页面上的每个其他元素之上呈现元素?

[英]How to render an element on top of every other element on page without depending on the structure/styles used in the page?

I wish to display a button at a fixed position(sticky) on the page and it should always be on top of other elements on the page, assuming I have no knowledge of the structure and styles used on that page. 我希望在页面上的固定位置(粘性)上显示一个按钮,并且假设我不了解该页面上使用的结构和样式,那么它应该始终位于页面上其他元素的顶部。 The solution can only use Javascript(no jQuery), CSS3 and HTML5. 该解决方案只能使用Javascript(无jQuery),CSS3和HTML5。

The solution has to be dynamic/adaptive ie not directly dependent on the z-index values used on the page. 解决方案必须是动态/自适应的,即不直接取决于页面上使用的z-index值。

Demo: http://jsfiddle.net/lotusgodkk/sf1fukm5/ 演示: http//jsfiddle.net/lotusgodkk/sf1fukm5/

CSS: CSS:

.a {
    position:fixed;
    right:10px;   //optional
    top:10px;    //optional
    z-index:1;
    background:grey; //optional
    color:#000;   //optional
    padding:20px;  //optional
}

HTML: HTML:

<div>--Content--</div>
<div class="a">Fixed</div> //Fixed div
<div>--Content--</div>....

For dynamic z-index using jQuery: 对于使用jQuery的动态z-index:

var highest = -999;
$("*").each(function () {
    var current = parseInt($(this).css("z-index"), 10);
    if (current && highest < current) highest = current;
});
$('your-element-selector').css('z-index',highest);

Using javascript: How can you figure out the highest z-index in your document? 使用javascript: 如何找出文档中最高的z-index?

Refer: How to find the highest z-index within a document no matter what tags they are? 请参阅: 无论它们是什么标签,如何找到文档中最高的z索引?

JSFIDDLE JSFIDDLE

CSS: CSS:

.fixed-button {   
      width: 125px; 
      background: #FFFFFF;
      border-style: solid;
      border-width: 1px;
      border-radius: 3px;
      padding: 5px;
      margin-left: 0px;
      position: fixed;
      z-index: 999;
      top: 70px;
  }

HTML: HTML:

<button class="fixed-button"> fixed-button </button>

I hope you got the answer. 我希望你能得到答案。 You can change the position of button where ever you need 您可以在任何需要的地方更改按钮的位置

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

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