和 attr() 方法,这样当 z-index 改变时,位置也会按比例改变。-6ren">
gpt4 book ai didi

html - 我如何(可以)使用 attr() 获取 z-index,一个 CSS 属性,以便在我的样式定义中与 calc() 一起使用?

转载 作者:太空宇宙 更新时间:2023-11-04 09:00:17 25 4
gpt4 key购买 nike

我正在尝试使用 CSS calc() 设置页面上特定 div 元素 id="test" 相对于其 z-index 的位置> 和 attr() 方法,这样当 z-index 改变时,位置也会按比例改变。

但是,它似乎在所有情况下都将 attr(zIndex) 评估为值 1,导致尽管 zIndex 的值已修改,但所需元素仍保留在原位。是否有从样式定义本身访问 CSS 值的正确方法,还是我必须在 JavaScript 中完成所有这些操作?

<!DOCTYPE html>
<html>
<head>
<title>zLinkTestPage</title>
<style>
body { background-color: #000000; }
div { margin: 0px; padding: 0px; border-width: 0px; display: block; }
#test {
position: absolute;
top: 0px;
left: calc( attr(zIndex) * 10px );/*This line is the one in question*/
background-color: #ff0000;
z-index: 0;
width: 144px; height: 288px;
}
#ref {
position: absolute;
top: 0px;
left: 0px;
background-color: #00ff00;
z-index: 1;
width: 288px; height: 144px;
}
</style>
</head>
<body>
<div id="ref">Reference div</div>
<div id="test" onmouseenter="moveZ(2);" onmouseleave="moveZ(-2);"><br /><br /><br /><br /><br /><br /><br /><br />Test div</div>
</body>
<script type="text/javascript">
function moveZ(amount) {
var box = document.getElementById("test");
var curZ = box.style.zIndex;
if(curZ == "") {
//console.log((typeof curZ) + " curZ=" + curZ);
curZ = 0; //because, for some reason, the initial value of any CSS property when accessed by JavaScript always seems to be null of some sort...
//console.log((typeof curZ) + " curZ=" + curZ);
}
else {
//console.log((typeof curZ) + " curZ=" + curZ);
curZ = parseInt(curZ);
//console.log((typeof curZ) + " curZ=" + curZ);
}
var newZ = curZ + amount;
box.style.zIndex = newZ;
}
</script>
</html>

这是我自己的一个练习,以后只会用来做更酷的东西。

最佳答案

据我所知,仅使用 CSS 是不可能的。但是,您可以在 CSS 属性 content 中引用元素的 HTML 属性,并在 CSS 选择器中引用 HTML 属性。

因此,使用您的代码,您实际上可以在元素上创建一个 zIndex(不是 CSS z-index)属性属性(或者我创建了一个 data- zIndex),然后在 content 属性中引用它

#test:after {
content: attr(data-zIndex);
background: white;
color: black;
padding: 1em;
display: inline-block;
}

或者您可以使用 CSS 选择器根据该属性的值来定位元素,例如...

#test[data-zIndex="0"]:after {
border-radius: 1em;
}

#test[data-zIndex="2"]:after {
background: yellow;
}

这是包含这些更改的演示。

function moveZ(amount) {
var box = document.getElementById("test");
var curZ = box.getAttribute('data-zIndex');
if (curZ == "") {
//console.log((typeof curZ) + " curZ=" + curZ);
curZ = 0; //because, for some reason, the initial value of any CSS property when accessed by JavaScript always seems to be null of some sort...
//console.log((typeof curZ) + " curZ=" + curZ);
} else {
//console.log((typeof curZ) + " curZ=" + curZ);
curZ = parseInt(curZ);
//console.log((typeof curZ) + " curZ=" + curZ);
}
var newZ = curZ + amount;
box.setAttribute('data-zIndex',newZ);
}
body {
background-color: #000000;
}

div {
margin: 0px;
padding: 0px;
border-width: 0px;
display: block;
}

#test {
position: absolute;
top: 0px;
left: calc( attr(zIndex) * 10px);
/*This line is the one in question*/
background-color: #ff0000;
z-index: 0;
width: 144px;
height: 288px;
}

#test:after {
content: attr(data-zIndex);
background: white;
color: black;
padding: 1em;
display: inline-block;
}

#test[data-zIndex="0"]:after {
border-radius: 1em;
}

#test[data-zIndex="2"]:after {
background: yellow;
}

#ref {
position: absolute;
top: 0px;
left: 0px;
background-color: #00ff00;
z-index: 1;
width: 288px;
height: 144px;
}
<div id="ref">Reference div</div>
<div id="test" data-zIndex='0' onmouseenter="moveZ(2);" onmouseleave="moveZ(-2);"><br /><br /><br /><br /><br /><br /><br /><br />Test div</div>

关于html - 我如何(可以)使用 attr() 获取 z-index,一个 CSS 属性,以便在我的样式定义中与 calc() 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42815754/

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