【问题标题】:CSS can I transition to flex-end?CSS 我可以过渡到 flex-end 吗?
【发布时间】:2021-07-26 12:05:53
【问题描述】:

我正在尝试将转换属性添加到复选框开关。它的起始位置是 flex-start,我希望它顺利过渡到 flex-end。我只是似乎无法让它在 flex-end 属性上工作,但奇怪的是,开关移动到 flex-end 并且背景颜色转换正确。

我看过以前类似问题的答案,但我就是无法破解这个问题!

有人可以告诉我我在这里缺少什么吗?

提前致谢。

/* the container controls the switch size */
.switch-container {
  width: 100%;
  height: 34px;
  background-color: coral;
}

/* hide the default checkbox */
.checkbox {
  position: absolute;
  left: -9999px;
}

/* target the label i.e. box around the slider */
.switch {
  position: relative;
  display: flex;
  width: 100%;
  height: 100%;
}

/* target the span element */
.slider {
  display: flex;
  position: absolute;
  cursor: pointer;
  /* set size of slider track */
  top: 10px;
  left: 0;
  right: 0;
  bottom: 10px;
  /* === end set size === */
  background-color: #ccc;
  align-items: center;
  justify-content: flex-start;
  transition: var(--transition);
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  background-color: rgb(199, 36, 36);
  box-shadow: 2px 2px cadetblue inset, -2px -2px cadetblue inset;
  transition: var(--transition);
}

input:checked + .slider:before {
  transform: translateX(129%);
  background-color: darkgreen;
  transition: var(--transition);
}

input:checked + .slider {
  background-color: #2196f3;
  transition: var(--transition);
}
<section class="switch-container">
      <label class="switch">
        <input type="checkbox" class="checkbox"/>
        <span class="slider"></span>
      </label>
    </section>
</html>

我遇到的问题是,如果我让开关大小响应父容器的大小,那么使用transform: translateX(some-value) 移动切换滑块,(some-value) 必须重新计算以适应当前轨道大小.

【问题讨论】:

  • 你好 fishbite 你想要什么,当你点击它时,它会随着过渡顺利移动?
  • align-items属性取离散值,不能对它使用transition属性
  • 希望对您有所帮助here
  • @KiranMistry 是的,我希望它能够顺利过渡到曲目的尽头。我的目标是让开关在尺寸上完全响应我正在制作的游戏。 transform: translateX(26px); 的问题在于它使用了特定的 px 值,我不想使用它。
  • @meine 说我别无选择,只能使用 transform: translateX(some-value);

标签: html css flexbox transition


【解决方案1】:

您不能为这些值设置动画。我建议,你改为动画 margin-left

.switch {
  --slider-width: 26px;
  --switch-width: calc(100% - var(--slider-width) / 2);
  /* --- */
  width: var(--switch-width);
  /* --- */
}
/* --- */
.slider:before {
  /* --- */
  width: var(--slider-width);
  /* --- */
}
/* --- */
input:checked + .slider {
  margin-left: calc(var(--switch-width) - var(--slider-width) / 2);
}

.checkbox {
  position: absolute;
  left: -9999px;
}

/* target the label i.e. box around the slider */
.switch {
  --slider-width: 26px;
  --switch-width: calc(100% - var(--slider-width) / 2);
  position: relative;
  display: flex;
  width: var(--switch-width);
  height: 34px;
}

/* target the span element */
.slider {
  display: flex;
  position: absolute;
  cursor: pointer;
  /* set size of slider track */
  top: 10px;
  left: 0;
  right: 0;
  bottom: 10px;
  /* === end set size === */
  background-color: #ccc;
  align-items: center;
  justify-content: flex-start;
  transition: 0.5s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: var(--slider-width);
  background-color: rgb(199, 36, 36);
  box-shadow: 2px 2px cadetblue inset, -2px -2px cadetblue inset;
  transition: 0.5s;
}

input:checked + .slider:before {
  /* transform: translateX(125%); */ /* note this transitions correctly */
  background-color: darkgreen;
  /* transition: var(--transition); */
  transition: 0.5s;
}

input:checked + .slider {
  background-color: #2196f3;
  margin-left: calc(var(--switch-width) - var(--slider-width) / 2);
  transition: 0.5s;
}
<label class="switch">
  <input type="checkbox" class="checkbox"/>
  <span class="slider"></span>
</label>

【讨论】:

  • 对不起,我试图理解开关宽度取决于用户的设备尺寸,因此是一个未知值
  • @Fishbite 在我的回答之后你大大改变了你的代码
  • 再次道歉,我正在编辑我的代码,在我提交更改之前没有看到您发布过
  • 我已经编辑了我的代码。这只是获得正确计算的问题
  • 我认为这是我的问题,我不知道如何计算它,如您所显示的。这有效,我将其标记为正确答案。非常感谢:)
【解决方案2】:

这是我的尝试。

/* the container controls the switch size */
.switch-container {
  width: 100%;
  height: 34px;
  background-color: coral;
  --transition: 300ms
}

/* hide the default checkbox */
.checkbox {
  position: absolute;
  left: -9999px;
}

/* target the label i.e. box around the slider */
.switch {
  position: relative;
  display: flex;
  width: 100%;
  height: 100%;
}

/* target the span element */
.slider {
  display: flex;
  position: absolute;
  cursor: pointer;
  /* set size of slider track */
  top: 10px;
  left: 0;
  right: 0;
  bottom: 10px;
  /* === end set size === */
  background-color: #ccc;
  align-items: center;
  transition: var(--transition);
}

.slider:before {
  content: "";
  flex: 0;
  transition: var(--transition);
}

input:checked + .slider:before {
  flex: 1 1 0%
}

.slider:after {
  content: "";
  height: 26px;
  width: 26px;
  background-color: rgb(199, 36, 36);
  box-shadow: 2px 2px cadetblue inset, -2px -2px cadetblue inset;
  transition: var(--transition);
}

input:checked + .slider:after {
  background-color: darkgreen;
  transition: var(--transition);
}

input:checked + .slider {
  background-color: #2196f3;
  transition: var(--transition);
}
<section class="switch-container">
  <label class="switch">
    <input type="checkbox" class="checkbox"/>
    <span class="slider"></span>
  </label>
</section>

【讨论】:

  • 嗯,这正是我想做的,并且不会影响设计:) 我真的需要研究这个flex: 1 1 0%,因为我不确定它是如何做的!!!我认为这是一个灵活增长的属性是对的吗?非常感谢您的意见,谢谢:)
  • 不客气@Fishbite。没错,我使用了 flex-grow 属性 :)
猜你喜欢
  • 1970-01-01
  • 2017-04-12
  • 1970-01-01
  • 2016-06-13
  • 2015-03-10
  • 2015-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多