【问题标题】:CSS Transitions and animations not workingCSS 过渡和动画不起作用
【发布时间】:2021-05-15 12:53:59
【问题描述】:

我正在尝试实现一个动画,如果用户点击一个标签,(最初隐藏的)div 将从上到下打开,如果他们再次点击它,div 将从下到上很好地关闭。

/* Firstly I tried using css transitions but it does not work 
at all as the entire div just opened without as transition*/
input.trigger+div.mapInfo {
  display: none; /* div originally hidden */
  height: 0px;
  max-height: 0px;
  transition: all 300ms;
}

input.trigger:checked+div.mapInfo {
  display: block;/*div shown*/
  height: auto;
  max-height: 500px;
}

/*Then I tried using max-height,min-height, and height for 
transitions but it still doesn't work*/

input.trigger + div.mapInfo{
   display:none;
   min-height:0px;
   max-height:0px;
   transition:all 300ms;
}




input.trigger:checked + div.mapInfo{
   display:block;
   min-height:300px;
   max-height:500px;
   height:auto;
}


/*Hence, I deleted the transition code and tried css 
animations now the div opens up nicely  but still closed 
without animations*/
input.trigger+div.mapInfo {
  display: none;
  animation: close-up 300ms linear forwards;/*this line of code appears to be not working*/
}

input.trigger:checked+div.mapInfo {
  display: block;
  animation: open-up 300ms linear forwards;
}

@keyframes close-up {
  0% {
    max-height: 500px;
  }
  100% {
    max-height: 0px;
  }
}

@keyframes open-up {
  0% {
    max-height: 0px;
  }
  100% {
    max-height: 500px;
  }
}
/* Further more I tried using transform:scaleY() and 
replacing the max-height, but closing the div still has no animations */
@keyframes close-up{
0%{
   transform:scaleY(1)
}
   100%{
   transform:scaleY(0);
}
}
@keyframes open-up{
   0%{
transform:scaleY(0);
   }
   100%{
transform:scaleY(1);
   }
}
<!-- the label that user clicks on -->
<label id="marker6" class="battleMarkers" for="trigger6"><img src="ss_marker.png" alt="" /></label>

<input class="trigger" type="checkbox" id="trigger6">
<!-- the div to be opened-->
<div class="mapInfo" id="info6">
  <h2>Sword Beach</h2>
  <p>Sword was the easternmost landing site of the Normandy Invasion. Sword was divided into several sectors with each sector divided into beaches. Sword is located about 9 miles from Caen, the goal of the 3rd Infantry Division.</p>
  <p>The Sword landing suffered few casualties but it's route inland suffered from traffic congestion that limited the invasion's effectiveness. Troops at Sword experienced the only armour counter-attack of D-Day, mounted by the 21st Panzer Division.</p>
</div>

注意:这个问题与 `this one 不同,因为他/她的 div 的起始高度为 10px,而我的从一开始就隐藏。

【问题讨论】:

  • 您介意使用 JS 还是打算使用 CSS?
  • @jacc_01 JS 是不是很困难/令人困惑?现在我知道了一些 js 基础知识,比如变量数据类型循环、函数
  • 我注意到你的问题是让 div 从上到下打开,但你尝试的代码是从下到上打开的。从上到下要容易得多,并且提供的两个答案都可以让您做到这一点。如果您确实想要从下到上,那么您将需要 JavaScript。

标签: css css-selectors css-animations


【解决方案1】:

我不确定这是否是您想要做的。但我只是更改了过渡而不是显示,我更改了文本颜色。如果要添加图像,也可以使用可见性。根据 W3schools.com,显示属性是不可动画的。这就是为什么当您取消选中该框时,它会立即转到display:none 而没有过渡效果。

input.trigger+div.mapInfo {
  display:hidden; /* div originally hidden */
  color:rgba(0,0,0,0);
  height: 0px;
  overflow:hidden;
  transition:1s;
}

input.trigger:checked+div.mapInfo {
  display: block;/*div shown*/
  color:rgba(0,0,0,1);
  height: 500px;
  transition:1s;
}
<!-- the label that user clicks on -->
<label id="marker6" class="battleMarkers" for="trigger6"><img src="ss_marker.png" alt="" /></label>

<input class="trigger" type="checkbox" id="trigger6">
<!-- the div to be opened-->
<div class="mapInfo" id="info6">
  <h2>Sword Beach</h2>
  <p>Sword was the easternmost landing site of the Normandy Invasion. Sword was divided into several sectors with each sector divided into beaches. Sword is located about 9 miles from Caen, the goal of the 3rd Infantry Division.</p>
  <p>The Sword landing suffered few casualties but it's route inland suffered from traffic congestion that limited the invasion's effectiveness. Troops at Sword experienced the only armour counter-attack of D-Day, mounted by the 21st Panzer Division.</p>
</div>

【讨论】:

  • 非常感谢您!修改您的代码后,我设法得到我想要的!
【解决方案2】:

您需要做的最少的事情就是改变高度。确保您还将溢出设置为隐藏。

/* Firstly I tried using css transitions but it does not work 
at all as the entire div just opened without as transition*/

.mapInfo {
  height: 0px;
  overflow: hidden;
  transition: height 300ms;
}

.trigger:checked ~ .mapInfo {
  height: 500px;
}
<!-- the label that user clicks on -->
<label id="marker6" class="battleMarkers" for="trigger6"><img src="ss_marker.png" alt="" /></label>

<input class="trigger" type="checkbox" id="trigger6">
<!-- the div to be opened-->
<div class="mapInfo" id="info6">
  <h2>Sword Beach</h2>
  <p>Sword was the easternmost landing site of the Normandy Invasion. Sword was divided into several sectors with each sector divided into beaches. Sword is located about 9 miles from Caen, the goal of the 3rd Infantry Division.</p>
  <p>The Sword landing suffered few casualties but it's route inland suffered from traffic congestion that limited the invasion's effectiveness. Troops at Sword experienced the only armour counter-attack of D-Day, mounted by the 21st Panzer Division.</p>
</div>

【讨论】:

    猜你喜欢
    • 2016-09-22
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 2018-02-28
    • 2022-01-16
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多