我正在尝试,在不使用更多 javascript 和 CSS 的情况下,弄清楚为什么单击其中一个列表项不会通过淡入淡出而不是立即出现来显示展开的文本。该代码使用过渡持续时间,然后使展开的元素可见。
我知道你不能有从隐藏到可见的过渡持续时间,但你可以在不透明度上 - 但它仍然没有过渡。
有人可以解释这里发生了什么,并提出最好的非 JavaScript 解决方案吗?
在此感谢您的帮助 - 找到下面的代码和代码笔:
let moduleDescriptions = document.querySelectorAll('.course-lesson');
for (let description of moduleDescriptions) {
description.addEventListener("click", function(){
description.classList.toggle("open");
description.querySelector('.expanded-description').classList.toggle("open");
});
}
.course-lesson {
flex-basis: 100%;
text-align: left;
padding: .5em;
margin: 2px 5px 0 5px;
background: #fff;
position: relative;
font-weight: 300;
transition-duration: 0.3s;
transition: 0.3s ease all;
}
course-lesson * {
transition-duration: 0.3s;
transition: 0.3s ease all;
}
.expanded-description.open {
display: block;
visibility: visible;
transition-duration: 0.3s;
opacity: 1;
transition: visibility 0.3s linear,opacity 0.3s linear;
}
/* Relevant code above */
.course-section {
list-style-type: none;
padding: 0;
background: #15a669;
display: flex;
flex-flow: row wrap;
}
.course-section:not(:first-of-type) {
margin-top: 25px;
}
.course-section h3 {
padding: .5em;
text-align: left;
font-size: 18px;
font-weight: 400;
flex-basis: 100%;
background: #15a669;
margin: 5px 5px 0 5px;
color: #fff;
}
.course-lesson:hover {
background: #f2f2f2;
cursor: pointer;
}
.course-lesson:first-of-type {
margin-top: 0;
}
.course-lesson:last-of-type {
margin-bottom: 5px;
}
.course-lesson.type-video:before {
content: '\f144';
font-family: FontAwesome;
margin-right: .5em;
}
.course-lesson.type-file:before {
content: '\f15c';
font-family: FontAwesome;
margin-right: .5em;
}
.course-lesson:after {
content: '\f0d7';
position: absolute;
right: .5em;
top: 50%;
transform: translate(-50%, -50%);
font-family: FontAwesome;
}
.course-lesson.open:after {
content: '\f0d8';
}
.expanded-description {
padding: .5em 1em 0 0;
display: none;
}
<ul class="course-section">
<h3>title</h3>
<li class="course-lesson type-video">Intro
<div class="expanded-description">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</li>
<li class="course-lesson type-video">more info here
<div class="expanded-description">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</li>
</ul>
代码笔:https://codepen.io/anon/pen/dxVzyX
更新:任何想法,任何人?
您正在将显示从无更改为 block ,并且过渡将不起作用。真正的代码是:`
.course-lesson {
flex-basis: 100%;
text-align: left;
padding: .5em;
margin: 2px 5px 0 5px;
background: #fff;
position: relative;
font-weight: 300;
transition-duration: 0.3s;
transition: 0.3s ease all;
height: 20px;
}
.expanded-description {
padding: .5em 1em 0 0;
opacity: 0;
visibility: hidden;
}
.course-lesson.open {
height: 70px;
}
`
我是一名优秀的程序员,十分优秀!