的高度变换在我的codepen.io上没有发生。

huangapple go评论51阅读模式
英文:

My transition for a <div>'s height is not taking place in my pen on codepen.io

问题

所以,我在codepen.io上有自己的代码,使用过渡来改变高度,但我不知道发生了什么。有人能帮帮我吗?我完全不知道为什么,但出于某种原因其中一个起作用了?我非常困惑,这对我来说不像是正常的网页开发时间.. 我已经尽力想出它为什么会起作用,但我一点运气也没有 D-:

所以我尝试了这段代码:
HTML:

&lt;div class=&quot;card&quot;&gt;
  &lt;div class=&quot;text&quot;&gt;
    &lt;h3&gt;标题&lt;/h3&gt;
    &lt;p&gt;故事的描述&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;

CSS:

html, body {
  margin: 0;
  font-family: &quot;Rubik&quot;, sans-serif;
  width: 100%;
  height: 100%;
  background: hsl(0, 0%, 10%);
  overflow: hidden;
}

.card {
  position: relative;
  background-image: url(&quot;/images/MyImage.png&quot;);
  background-size: cover;
  width: calc(66vw - 4rem);
  height: 50vh;
  padding: 20px;
  border-radius: 1rem;
  margin: 2rem;
  overflow: hidden;
}

.text {
  background: black;
  box-shadow: 0 0 2rem 5rem black;
  position: absolute;
  left: 0;
  bottom: 0;
  height: auto;
  width: inherit;
  transition-timing-function: ease;
  transition-duration: .5s;
}

.card:hover .text {
  height: 3rem;
}

.text h3 {
  margin: 0;
  position: absolute;
  bottom: 1rem;
  left: 1rem;
  color: white;
  transition-timing-function: ease;
  transition-duration: .5s;
  cursor: default;
}

.card:hover h3 {
  position: absolute;
  bottom: 50vh;
}

.text p {
  position: absolute;
  display: none;
  bottom: 1rem;
  left: 1rem;
  color: white;
  margin: 0;
  transition-timing-function: ease;
  transition-duration: .5s;
}

.card:hover .text p {
  display: block;
}

h3::selection, p::selection {
  background: transparent;
}
英文:

So, I've got my own pen on codepen.io using a transition to change the height, and I don't know what's happening. CAN SOMEBODY HELP ME Please? I've got a single clue why but for some reason 1 of them is working?? I'm SUPER confused, this is not like a normal web-developing time to me.. sort of I've tried my best to think of why it might work myself but I've managed to get 0 luck D-:

So I tried this code:
HTML:

&lt;div class=&quot;card&quot;&gt;
  &lt;div class=&quot;text&quot;&gt;
    &lt;h3&gt;A Title&lt;/h3&gt;
    &lt;p&gt;A Description for the story&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;

CSS:

html, body {
  margin: 0;
  font-family: &quot;Rubik&quot;, sans-serif;
  width: 100%;
  height: 100%;
  background: hsl(0, 0%, 10%);
  overflow: hidden;
}

.card {
  position: relative;
  background-image: url(&quot;/images/MyImage.png&quot;);
  background-size: cover;
  width: calc(66vw - 4rem);
  height: 50vh;
  padding: 20px;
  border-radius: 1rem;
  margin: 2rem;
  overflow: hidden;
}

.text {
  background: black;
  box-shadow: 0 0 2rem 5rem black;
  position: absolute;
  left: 0;
  bottom: 0;
  height: auto;
  width: inherit;
  transition-timing-function: ease;
  transition-duration: .5s;
}

.card:hover .text {
  height: 3rem;
}

.text h3 {
  margin: 0;
  position: absolute;
  bottom: 1rem;
  left: 1rem;
  color: white;
  transition-timing-function: ease;
  transition-duration: .5s;
  cursor: default;
}

.card:hover h3 {
  position: absolute;
  bottom: 50vh;
}

.text p {
  position: absolute;
  display: none;
  bottom: 1rem;
  left: 1rem;
  color: white;
  margin: 0;
  transition-timing-function: ease;
  transition-duration: .5s;
}

.card:hover .text p {
  display: block;
}

h3::selection, p::selection {
  background: transparent;
}

can anybody tell me why the paragraph and the text just won't come up with a transition, I gave you everything in case anything isn't supposed to be like that. and by the way the image is a tropical beach with a non-used name by me, I just wanted you to know that there's ACTUALLY an image there just incase it has anything to do with it's children's transitions.

答案1

得分: 0

要创建过渡效果,您需要一个起始点和一个结束点。但是,并非所有属性都会"创建"平滑的过渡效果,例如从 display: none;display: block;,没有过渡效果会产生(同样,从 visibility: hiddenvisible 也是如此)。

因此,如果您想要创建平滑的过渡效果,您必须使用可以实现这一效果的属性。在这种情况下,对于 p 元素,您必须使用 opacity 属性,而对于 div 元素,您不能auto 过渡到 3rem,因此您将被迫设置一个不是autoheight 属性。

html, body {
  margin: 0;
  font-family: "Rubik", sans-serif;
  width: 100%;
  height: 100%;
  background: hsl(0, 0%, 10%);
  overflow: hidden;
}

.card {
  position: relative;
  background-image: url("/images/MyImage.png");
  background-size: cover;
  width: calc(66vw - 4rem);
  height: 50vh;
  padding: 20px;
  border-radius: 1rem;
  margin: 2rem;
  overflow: hidden;
}

.text {
  background: black;
  box-shadow: 0 0 2rem 5rem black;
  position: absolute;
  left: 0;
  bottom: 0;
  height: .5rem;
  width: inherit;
  transition-timing-function: ease;
  transition-duration: .5s;
}

.card:hover .text {
  height: 3rem;
}

.text h3 {
  margin: 0;
  position: absolute;
  bottom: 1rem;
  left: 1rem;
  color: white;
  transition-timing-function: ease;
  transition-duration: .5s;
  cursor: default;
}

.card:hover h3 {
  position: absolute;
  bottom: 50vh;
}

.text p {
  position: absolute;
  opacity: 0;
  visibility: hidden;
  bottom: 1rem;
  left: 1rem;
  color: white;
  margin: 0;
  transition-timing-function: ease;
  transition-duration: .5s;
}

.card:hover .text p {
  visibility: visible;
  opacity: 1;
}

h3::selection, p::selection {
  background: transparent;
}
<div class="card">
  <div class="text">
    <h3>A Title</h3>
    <p>A Description for the story</p>
  </div>
</div>
英文:

To make transitions you need a starting point and an ending point. However, not all properties "create" a smooth transition, for example from display: none; to display: block; no transition will be created (same from visibility: hidden to visible).

For this reason, if you want to create smooth transitions you have to use properties that allow you to do this. In this case for the p you have to use the opacity, while for the div you can't make a smooth transition from auto to 3rem, so you will be forced to set a height other than auto.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

html, body {
margin: 0;
font-family: &quot;Rubik&quot;, sans-serif;
width: 100%;
height: 100%;
background: hsl(0, 0%, 10%);
overflow: hidden;
}
.card {
position: relative;
background-image: url(&quot;/images/MyImage.png&quot;);
background-size: cover;
width: calc(66vw - 4rem);
height: 50vh;
padding: 20px;
border-radius: 1rem;
margin: 2rem;
overflow: hidden;
}
.text {
background: black;
box-shadow: 0 0 2rem 5rem black;
position: absolute;
left: 0;
bottom: 0;
height: .5rem;
width: inherit;
transition-timing-function: ease;
transition-duration: .5s;
}
.card:hover .text {
height: 3rem;
}
.text h3 {
margin: 0;
position: absolute;
bottom: 1rem;
left: 1rem;
color: white;
transition-timing-function: ease;
transition-duration: .5s;
cursor: default;
}
.card:hover h3 {
position: absolute;
bottom: 50vh;
}
.text p {
position: absolute;
opacity: 0;
visibility: hidden;
bottom: 1rem;
left: 1rem;
color: white;
margin: 0;
transition-timing-function: ease;
transition-duration: .5s;
}
.card:hover .text p {
visibility: visible;
opacity: 1;
}
h3::selection, p::selection {
background: transparent;
}

<!-- language: lang-html -->

&lt;div class=&quot;card&quot;&gt;
  &lt;div class=&quot;text&quot;&gt;
    &lt;h3&gt;A Title&lt;/h3&gt;
    &lt;p&gt;A Description for the story&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月7日 04:38:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定