所以我有一个网页,我在其中编写了一个插件,允许客户购买其他产品,但我不希望所有这些产品在进入页面时都显示出来。所以我正在寻找某种"显示更多"
<div class="wrapper">
<div class="ty-compact-list" style="padding: 5px 5px 5px 0px; float: left; width: 100%;">Product 1</div>
<div class="ty-compact-list" style="padding: 5px 5px 5px 0px; float: left; width: 100%;">Product 2</div>
<div class="ty-compact-list" style="padding: 5px 5px 5px 0px; float: left; width: 100%;">Product 3</div>
<div class="ty-compact-list" style="padding: 5px 5px 5px 0px; float: left; width: 100%;">Product 4</div>
<div class="ty-compact-list" style="padding: 5px 5px 5px 0px; float: left; width: 100%;">Product 5</div>
<div class="ty-compact-list" style="padding: 5px 5px 5px 0px; float: left; width: 100%;">Product 6</div>
</div>
&#13;
因此,当我进入页面时,我正在寻找只显示3种产品的方式,但是如果按下显示更多按钮,它将显示接下来的3种产品。
我已经看过很多关于此的帖子,但我找不到与此相关的任何内容。
聚苯乙烯。如果按下&#34;显示更多&#34;可能会有动画,那就太棒了。
答案 0 :(得分:2)
你有课,所以使用它并避免内联样式。另请使用jquery :gt()
检查以下方法
//this will execute on page load(to be more specific when document ready event occurs)
if ($('.ty-compact-list').length > 3) {
$('.ty-compact-list:gt(2)').hide();
$('.show-more').show();
}
$('.show-more').on('click', function() {
//toggle elements with class .ty-compact-list that their index is bigger than 2
$('.ty-compact-list:gt(2)').toggle();
//change text of show more element just for demonstration purposes to this demo
$(this).text() === 'Show more' ? $(this).text('Show less') : $(this).text('Show more');
});
.ty-compact-list {
padding: 5px 5px 5px 0px;
float: left;
width: 100%;
}
.show-more {
display: none;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
<div class="ty-compact-list">Product 1</div>
<div class="ty-compact-list">Product 2</div>
<div class="ty-compact-list">Product 3</div>
<div class="ty-compact-list">Product 4</div>
<div class="ty-compact-list">Product 5</div>
<div class="ty-compact-list">Product 6</div>
<div class="show-more">Show more</div>
</div>
答案 1 :(得分:0)
我喜欢亚历克斯提出的基于班级的方式,但你也可以这样做:
function showFirstThree()
{
$('.ty-compact-list').hide();
$('.ty-compact-list').slice(0,3).show();
}
function showAll(){
$('.ty-compact-list').show();
}
答案 2 :(得分:0)
这是一个使用切换的非常简单的答案。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(function() {
$('.showmoreButton').on('click', function() {
$('.showmore').slideToggle("fast");
});
});
</script>
<div class="showmoreButton">Toggle Results</div>
<div class="wrapper">
<div class="ty-compact-list" style="padding: 5px 5px 5px 0px; float: left; width: 100%;">Product 1</div>
<div class="ty-compact-list" style="padding: 5px 5px 5px 0px; float: left; width: 100%;">Product 2</div>
<div class="ty-compact-list" style="padding: 5px 5px 5px 0px; float: left; width: 100%;">Product 3</div>
<div class="showmore" style="display: none;">
<div class="ty-compact-list" style="padding: 5px 5px 5px 0px; float: left; width: 100%;">Product 4</div>
<div class="ty-compact-list" style="padding: 5px 5px 5px 0px; float: left; width: 100%;">Product 5</div>
<div class="ty-compact-list" style="padding: 5px 5px 5px 0px; float: left; width: 100%;">Product 6</div>
</div>
</div>
&#13;