javascript - 显示更多/显示更少 CSS JS 动画不起作用
问题描述
我的网站中有一些文本,我只想在用户单击标题然后文本以动画形式出现时显示,当用户再次单击标题时文本消失。我在下面制作了这段代码,它对“显示更多”非常有用,但由于某种原因它不适用于“显示更少”!
function showMore(resp) {
if (document.getElementById(resp).style.maxHeight == "400") {
document.getElementById(resp).style.animation = "showmore 2s ease-in-out reverse";
document.getElementById(resp).style.maxHeight = "0";
} else {
document.getElementById(resp).style.animation = "showmore 2s ease-in-out";
document.getElementById(resp).style.maxHeight = "400";
}
}
@keyframes showmore {
0% {
opacity: 0;
max-height: 0;
}
100% {
opacity: 1;
max-height: 400px;
}
}
.awr {
text-align: left;
margin-left: 10px;
font-size: 17;
color: dimgrey;
max-height: 0;
overflow: hidden;
}
<div class="question" onclick="showMore('awr1')">
<h3>How it Works?</h3>
</div>
<div class="awr" id="awr1">
<p>this is the text that must be hidden.</p>
</div>
谁能帮我?
解决方案
我用 jQuery 实现,但你可以很容易地转换成 Javascript。
您可以简单地添加一个类并切换它。
function showMore(resp) {
$("#" + resp).toggleClass("show");
}
.awr {
text-align: left;
margin-left: 10px;
font-size: 17;
color: dimgrey;
height: 0px;
visibility: hidden;
transition: all 0.3s linear;
background-color: #F1F1F1;
}
.show {
visibility: visible;
height: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="question" onclick="showMore('awr1')">
<h3>How it Works?</h3>
</div>
<div class="awr" id="awr1">
<p>this is the text that must be hidden.</p>
</div>
推荐阅读
- c++ - 将 mqtt_cpp 库添加到 cmake 项目
- java - 如何在不知道列的情况下将“io.vertx.mutiny.sqlclient.Rowset”转换为地图列表?
- java - 部署到 jar 后获取 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
- reactjs - 在 React 中访问浏览器扩展
- python - 为什么代理服务器无法收到服务器的响应
- typo3 - 带有 cs_seo 扩展的 Typo3 tx_news 扩展在详细视图中显示 titleTag
- ruby-on-rails - 是否可以导出数据行,包括每行的图像
- android - Firebase 动态链接不适用于版本构建
- python - 如何制作带有色调的 3D 散点图?
- java - 从配置文件的活动列表中动态加载属性值并在配置 XML 文件中设置值