0%

JQuery實作 — 卡片收合

JQuery實作 — 卡片收合

jquery

HTML

引用 Bootstrap card component

1
2
3
4
5
6
7
8
9
<div class="card" style="width: 18rem; margin: 30px auto">
<img src="https://images.unsplash.com/photo-1541167760496-1628856ab772?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1037&q=80" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">Amy Coffee</h5>
<p class="card-text mb-2">Some quick example text to build on the card title and make up the bulk of the card's content.
</p>
<a href="#" class="btn btn-primary">Read More</a>
</div>
</div>

CSS

1
2
3
4
5
6
7
8
9
.card-text {
max-height: 50px;
overflow: hidden;
transition: all ease 0.8s;
}

.card-text.active {
max-height: 72px;
}

jQuery 寫法

1
2
3
4
5
6
7
8
$( document ).ready(function() {
$('.btn').on('click', function () {
$(this).siblings('.card-text').toggleClass('active')
$(this).siblings('.card-text').hasClass('active')
? $(this).text('Read Less')
: $(this).text('Read More')
})
});