【问题标题】:CSS how to use calc with css variable (css properties) on left, translate etc propertiesCSS 如何在左侧使用 calc 和 css 变量(css 属性),翻译等属性
【发布时间】: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 是的,你是对的。我的错。谢谢

标签: css tabs


【解决方案1】:

将 --index 属性设置为错误的元素,当兄​​弟的值改变时它不会生效

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-28
    • 2012-08-11
    • 1970-01-01
    • 2014-03-27
    相关资源
    最近更新 更多