gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:32:17 24 4
gpt4 key购买 nike

我正在构建一个 Chrome 扩展程序,单击固定在浏览器右侧的黄色栏后,该扩展程序会从浏览器窗口的右侧滑动。当我点击黄色条时,绿色和黄色条将会滑动。黄色的正确地位于页面的“顶部”——它覆盖了页面的内容。不幸的是,滑动后绿色的部分位于某些页面元素的下方。例如,在 stackoverflow 网站上,搜索窗口、提问按钮和类似问题部分下方有一个绿色栏。我该如何修复它?

list .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"]
}

测试.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;
}

注入(inject).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);

最佳答案

为什么会发生这种情况?

不正确分层的元素被插入到正文之前 .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注入(inject)元素的值,以绝对确保它们始终位于顶部并且...

确保这两个元素都插入到正文中 .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);

关于Chrome 扩展中的 CSS 固定定位/z-index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31787455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com