javascript - 使用 + 和 - 号创建显示更多/显示更少的信息框
问题描述
我想创建一个包含信息的框,该框在右上角单击“+”图标时展开。该框应展开并包含有关该特定部分的更多信息,并且“+”应转换为“-”以允许用户将框折叠为之前的原始格式。
动画将类似于 https://www.w3schools.com/howto/howto_css_menu_icon.asp
简单来说:我需要一个“显示更多/显示更少”的框,它可以展开/折叠,但带有“+”和“-”符号而不是“显示更多”或“显示更少”
我怎样才能做到这一点?
解决方案
这是JS的基础知识但是这里是修改W3代码的答案位
CSS
.container {
display: inline-block;
cursor: pointer;
}
.bar1, .bar2 {
width: 15px;
height: 3px;
background-color: #333;
transition: 0.4s;
}
.bar1 {
-webkit-transform: rotate(180deg) translate(0px, -3px);
transform: rotate(180deg) translate(0px, -3px);
}
.bar2 {
-webkit-transform: rotate(90deg) translate(0px, -0px);
transform: rotate(90deg) translate(0px, 0px);
}
.change .bar1 {
opacity: 0;
}
.change .bar2 {
-webkit-transform: rotate(180deg) translate(0px, -0px);
transform: rotate(180deg) translate(0px, 0px);
}
.info{
display: none;
transition: 0.4s;
}
.change .info{
display: inherit;
transition: 0.4s;
}
HTML
<div class="container" onclick="myFunction(this)">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="info">info</div>
</div>
JS(写在body标签里面)
<script>
function myFunction(x) {
x.classList.toggle("change");
}
</script>
推荐阅读
- amazon-web-services - AWS Cognito:如何防止使用同一个电子邮件地址进行多次注册?
- javascript - 圆角甜甜圈图 - 有可能吗?
- java - Scikit-learn vs. WEKA 分类模型实现
- python - 使用 python k8 的 api 客户端库获取所有正确的命名空间和入口路由
- java - 在“Spring”中检索文件ID属于特定用户的文件信息
- javascript - 如何修复我的标题在移动浏览器上被截断?
- php - 在字符串中搜索 URL,并为每个问题添加查询字符串
- r - 使用用户定义的函数删除缺失值
- python - 将大量图像加载到 numpy 数组
- android - 颤振医生 --android-licenses 错误:无法找到或加载主类 Studio