jquery - SlideToggle 列表关闭时仅显示列表的一部分
问题描述
我有一长串项目
我只想默认显示其中的一部分
并在按钮单击时显示所有项目
我有这个jsfiddle,但是如何使用 jQuery slideToggle() 对其进行动画处理:
HTML:
<ul class="faded part">
<li>List item</li>
<li>List item</li>
<li>List item</li>
<li>List item</li>
<li>List item</li>
</ul>
<button class="expand">Open</button>
CSS:
.part li:nth-of-type(-n+3) {
display: inherit !important;
}
.part li {
display: none;
}
JS:
$(document).ready(function() {
$(".expand").click(function() {
if ($(this).text() != "Close") {
$(this).parent().children("ul").removeClass("part");
$(this).text("Close");
} else {
$(this).parent().children("ul").addClass("part");
$(this).text("Open");
}
});
});
解决方案
你可以尝试这样的事情。
检查此链接以获取更多信息。
$(document).ready(function() {
$(".expand").click(function() {
if ($(this).text() != "Close") {
$(this).parent().children("ul").removeClass("closed");
$(this).text("Close");
} else {
$(this).parent().children("ul").addClass("closed");
$(this).text("Open");
}
});
});
li {
display: inherit;
}
.slider.closed {
max-height: 20px;
}
.slider {
overflow-y: hidden;
max-height: 500px; /* approximate max height */
transition-property: all;
transition-duration: .5s;
transition-timing-function: cubic-bezier(0, 1, 0.5, 1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="slider closed">
<li>List item</li>
<li>List item</li>
<li>List item</li>
<li>List item</li>
<li>List item</li>
<li>List item</li>
<li>List item</li>
<li>List item</li>
<li>List item</li>
</ul>
<button class="expand">Open</button>
推荐阅读
- arrays - 颠簸转换 JSON 数组
- python - 对于运行“python myapp.py images/*”的 Flask 应用程序,我应该为 Docker CMD 和 ENTRYPOINT 放置什么
- sql-server - 识别和禁用链接到特定表的所有触发器
- c# - 循环遍历 MongoDB 集合并在 C# 中加入它们
- javascript - 如何优化此 Google Apps 脚本?它所做的只是读取和写入数据,但它需要永远
- azure - 新 AzureStorageContext:端点与环境
- javascript - 使用 JavaScript 从 HTML 表单执行 2 个操作
- javascript - 如何告诉网站正在使用电子?
- android - 删除未使用的资源
- javascript - PHP 会话数据未在 HTTPS 环境的同一域内从页面传输到页面