【发布时间】:2022-12-11 14:23:23
【问题描述】:
Please check my code, i want to use --index for each item, and when tab is active, it should have correct offset, then i can animate it.但是只有font-size属性可以带参数,left和translate都不行。有没有办法做到这一点?
这是我的代码笔。 https://codepen.io/geforcesong/pen/QWxPwZa
const items = document.querySelectorAll('.item')
const tabClicked = (e)=>{
if(e.target.classList.contains('active')){
return;
}
items.forEach(item=>{
item.classList.remove('active')
})
e.target.classList.add('active')
}
items.forEach(item=>{
item.addEventListener('click', tabClicked)
})
.container {
display: flex;
flex-direction: column;
position: relative;
}
.item-container {
width: 800px;
background: gray;
display: flex;
height: 50px;
}
.container .item {
width: 150px;
background-color: red;
display: grid;
place-items: center;
font-size: calc(5px * var(--index, 1));
}
.container .item.active {
background-color: yellow;
}
.indicator {
width: 150px;
height: 5px;
background-color: blue;
/* translate: calc(150px * var(--index)); */
position:absolute;
bottom:0;
left: calc(150px * var(--index));
}
<div class="container">
<div class="item-container">
<div class="item" style="--index:1">A sample</div>
<div class="item active" style="--index:2">B sample</div>
<div class="item" style="--index:3">C sample</div>
<div class="item" style="--index:4">D sample</div>
</div>
<div class="indicator"></div>
</div>
【问题讨论】:
-
--index 没有为具有类指示符的 div 定义。
-
@Haworth 是的,你是对的。我的错。谢谢