英文:
My transition for a <div>'s height is not taking place in my pen on codepen.io
问题
所以,我在codepen.io上有自己的代码,使用过渡来改变高度,但我不知道发生了什么。有人能帮帮我吗?我完全不知道为什么,但出于某种原因其中一个起作用了?我非常困惑,这对我来说不像是正常的网页开发时间.. 我已经尽力想出它为什么会起作用,但我一点运气也没有 D-:
所以我尝试了这段代码:
HTML:
<div class="card">
<div class="text">
<h3>标题</h3>
<p>故事的描述</p>
</div>
</div>
CSS:
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: 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:
<div class="card">
<div class="text">
<h3>A Title</h3>
<p>A Description for the story</p>
</div>
</div>
CSS:
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: 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: hidden
到 visible
也是如此)。
因此,如果您想要创建平滑的过渡效果,您必须使用可以实现这一效果的属性。在这种情况下,对于 p
元素,您必须使用 opacity
属性,而对于 div
元素,您不能从 auto
过渡到 3rem
,因此您将被迫设置一个不是auto的 height
属性。
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: "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;
}
<!-- language: lang-html -->
<div class="card">
<div class="text">
<h3>A Title</h3>
<p>A Description for the story</p>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论