Chrome扩展中的CSS固定定位/ z-index

时间:2015-08-03 12:36:52

标签: css google-chrome-extension css-position z-index

我点击固定在浏览器右侧的黄色条后,构建一个从浏览器窗口右侧滑动的Chrome扩展程序。当我点击黄色条时,绿色和黄色条会滑动。黄色的是正确的"在顶部"页面 - 它涵盖了页面的内容。不幸的是,滑动后的绿色是在某些页面的元素下。例如,在stackoverflow站点上,绿色栏位于搜索窗口下,询问问题按钮和类似问题部分。我该如何解决?

的manifest.json

{
"manifest_version":         2,
"content_scripts":          [ {
    "js":       [ "injection.js", "jquery.js",  "jquery-ui.min.js"],
    "matches":  [   "http://stackoverflow.com/questions/*"
    ]
} ],
"description":              "Inject a complete, premade web page",
"name":                     "Inject whole web page",
"version":                  "1",
"web_accessible_resources": ["test.css", "jquery-ui.min.css"]
}

test.css

#intro_button {
background-color: yellow;
height : 100%;
width : 100px;
top: 0;
right: 0;
position : fixed;
cursor: pointer;    
}
#extension {
height: 100%;
width: 200px;
position: fixed;
right: -200px;
background-color: green;
}

injection.js

// Injecting button (div)
var divElement = document.createElement("DIV");
divElement.id = "intro_button";
divElement.style.right = "0px";
divElement.onclick = function () {
    if (this.style.right === "0px") {
        $(this).animate({ "right": "+=200" }, 800);
        $("#extension").animate({ "right": "+=200" }, 800);
    }
    else if (this.style.right === "200px") {
        $(this).animate({ "right": "-=200" }, 800);
        $("#extension").animate({ "right": "-=200" }, 800);
    }
}
var textnode = document.createTextNode("zzz");    
divElement.appendChild(textnode);
document.body.appendChild(divElement, document.body.firstChild);

// Injecting extenion (div)
var divExtension = document.createElement("DIV");
divExtension.id = "extension";
var textnode = document.createTextNode("yyy");
divExtension.appendChild(textnode);
document.body.insertBefore(divExtension, document.body.firstChild);

// Injecting CSS files
var head  = document.getElementsByTagName('head')[0];
var link  = document.createElement('link');
link.rel  = 'stylesheet';
link.type = 'text/css';
link.href = chrome.extension.getURL('test.css');
head.appendChild(link);

var head  = document.getElementsByTagName('head')[0];
var link  = document.createElement('link');
link.rel  = 'stylesheet';
link.type = 'text/css';
link.href = chrome.extension.getURL('jquery-ui.min.css');
head.appendChild(link);

// Injecting Jqueries
var s  = document.createElement('script');
s.src = "jquery.js";
document.getElementsByTagName("head")[0].appendChild(s);

var s  = document.createElement('script');
s.src = "jquery-ui.min.js";
document.getElementsByTagName("head")[0].appendChild(s);

2 个答案:

答案 0 :(得分:3)

为什么会这样?

错误分层的元素将插入到具有.insertBefore的正文之前:

document.body.insertBefore(divExtension, document.body.firstChild);

这会将元素置于<body>之前(之前),并导致其他z-index / position: absolute元素出现fixed个问题。

正确分层的元素将.appendChild附加在正文中:

document.body.appendChild(divElement, document.body.firstChild);

这是将元素放在 <body>(应该在哪里)和放在所有其他孩子之后。

使用自然z分层,absolute / fixed元素将与其后的类似元素重叠

div {
  position: absolute;
  width: 100px;
  height: 100px;
}
.red {
  background: red;
  left: 20px;
  top: 10px;
}
.green {
  background: green;
  left: 30px;
  top: 20px;
}
.blue {
  background: blue;
  left: 40px;
  top: 30px;
}
.purple {
  background: purple;
  left: 50px;
  top: 40px;
}
<div class="red">Red is underneath all its siblings</div>
<div class="green">Green overlaps red</div>
<div class="blue">Blue overlaps green</div>
<div class="purple">Purple overlaps blue</div>

重新创建问题

注意.incorrect div如何放在body标签之前。它的行为与示例中未正确插入的元素的行为相同,并且与<body>

中的内容div重叠

// Injecting button (div)
var divElement = document.createElement("DIV");
divElement.id = "intro_button";
var textnode = document.createTextNode("zzz");
divElement.appendChild(textnode);
document.body.appendChild(divElement, document.body.firstChild);

// Injecting extenion (div)
var divExtension = document.createElement("DIV");
divExtension.id = "extension";
var textnode = document.createTextNode("yyy");
divExtension.appendChild(textnode);
document.body.appendChild(divExtension, document.body.firstChild);

// Injecting CSS files
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = chrome.extension.getURL('test.css');
head.appendChild(link);

var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = chrome.extension.getURL('jquery-ui.min.css');
head.appendChild(link);

// Injecting Jqueries
var s = document.createElement('script');
s.src = "jquery.js";
document.getElementsByTagName("head")[0].appendChild(s);

var s = document.createElement('script');
s.src = "jquery-ui.min.js";
document.getElementsByTagName("head")[0].appendChild(s);
.content {
  height: 500px;
  width: 500px;
  margin: 100px;
  background: #F00;
  position: absolute;
}
#intro_button {
  background-color: yellow;
  height: 100%;
  width: 100px;
  top: 0;
  right: 0;
  position: fixed;
  cursor: pointer;
}
#extension {
  height: 100%;
  width: 100px;
  position: fixed;
  background-color: green;
}
.incorrect {
  position: fixed;
  height: 200px;
  width: 500px;
  background: orange;
}
<div class="incorrect"></div>

<body>
  <div class="content">Content</div>
</body>

我们如何解决这个问题?

您应该在注入的元素上使用较大的z-index值,以确保它们始终位于顶部并且......

确保使用.appendChild

将两个元素插入到体内
// Injecting button (div)
var divElement = document.createElement("DIV");
divElement.id = "intro_button";
divElement.style.right = "0px";
divElement.onclick = function () {
    if (this.style.right === "0px") {
        $(this).animate({ "right": "+=200" }, 800);
        $("#extension").animate({ "right": "+=200" }, 800);
    }
    else if (this.style.right === "200px") {
        $(this).animate({ "right": "-=200" }, 800);
        $("#extension").animate({ "right": "-=200" }, 800);
    }
}
var textnode = document.createTextNode("zzz");    
divElement.appendChild(textnode);
document.body.appendChild(divElement, document.body.firstChild);

// Injecting extenion (div)
var divExtension = document.createElement("DIV");
divExtension.id = "extension";
var textnode = document.createTextNode("yyy");
divExtension.appendChild(textnode);
document.body.appendChild(divExtension, document.body.firstChild);
// ## Changed to appendChild here ##

// Injecting CSS files
var head  = document.getElementsByTagName('head')[0];
var link  = document.createElement('link');
link.rel  = 'stylesheet';
link.type = 'text/css';
link.href = chrome.extension.getURL('test.css');
head.appendChild(link);

var head  = document.getElementsByTagName('head')[0];
var link  = document.createElement('link');
link.rel  = 'stylesheet';
link.type = 'text/css';
link.href = chrome.extension.getURL('jquery-ui.min.css');
head.appendChild(link);

// Injecting Jqueries
var s  = document.createElement('script');
s.src = "jquery.js";
document.getElementsByTagName("head")[0].appendChild(s);

var s  = document.createElement('script');
s.src = "jquery-ui.min.js";
document.getElementsByTagName("head")[0].appendChild(s);

答案 1 :(得分:2)

通常可以通过向定位元素添加z-index: 9999999 CSS属性来修复此问题(如果您可以控制其他元素的z顺序,则可以使用更合理的数字)。