使用max-height而不是overflow这样:
1) 最佳方式:
$("#more").click(function() {
$('.a').toggleClass('collapse');
var name = ($(this).text() =='See More') ? 'See Less' : 'See More';
$(this).text(name);
})
$("#more").click(function() {
$('.a').toggleClass('collapse');
var name = ($(this).text() =='See More') ? 'See Less' : 'See More';
$(this).text(name);
})
.a {
position: relative;
padding: 20px;
border: 1px solid black;
-webkit-box-sizing: border-box;
box-sizing: border-box;
max-height: 387px;
overflow: hidden;
-webkit-transition: max-height 4s;
-moz-transition: max-height 4s;
-ms-transition: max-height 4s;
-o-transition: max-height 4s;
transition: max-height 4s;
}
.b {
position: relative;
min-height: 50px;
border: 1px solid #aaa;
margin-bottom: 2px;
padding: 10px;
-webkit-box-sizing: border-box;
box-sizing: border-box;
text-align: center;
}
.collapse {
max-height: 550px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="a">
<div class="b"></div>
<div class="b"></div>
<div class="b"></div>
<div class="b"></div>
<div class="b"></div>
<div class="b"></div>
<div class="b"></div>
<div class="b"></div>
<div class="b"></div>
<div class="b"></div>
</div>
<p id="more">See More</p>
2)修正你的方式:
$("#more").click(function() {
$(".a").css({
WebkitTransition : 'max-height 4s ease-in-out',
MozTransition : 'max-height 4s ease-in-out',
MsTransition : 'max-height 4s ease-in-out',
OTransition : 'max-height 4s ease-in-out',
transition : 'max-height 4s ease-in-out',
'max-height':"550px"
});
$("#less").show();
$("#more").hide();
});
$("#less").click(function() {
$(".a").css("max-height","387px");
$("#more").show();
$("#less").hide();
});
$("#more").click(function() {
$(".a").css({
WebkitTransition : 'max-height 4s ease-in-out',
MozTransition : 'max-height 4s ease-in-out',
MsTransition : 'max-height 4s ease-in-out',
OTransition : 'max-height 4s ease-in-out',
transition : 'max-height 4s ease-in-out',
'max-height':"550px"
});
$("#less").show();
$("#more").hide();
});
$("#less").click(function() {
$(".a").css("max-height","387px");
$("#more").show();
$("#less").hide();
});
.a {
position: relative;
padding: 20px;
border: 1px solid black;
-webkit-box-sizing: border-box;
box-sizing: border-box;
max-height: 387px;
overflow: hidden;
}
.b {
position: relative;
min-height: 50px;
border: 1px solid #aaa;
margin-bottom: 2px;
padding: 10px;
-webkit-box-sizing: border-box;
box-sizing: border-box;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="a">
<div class="b"></div>
<div class="b"></div>
<div class="b"></div>
<div class="b"></div>
<div class="b"></div>
<div class="b"></div>
<div class="b"></div>
<div class="b"></div>
<div class="b"></div>
<div class="b"></div>
</div>
<p id="more">See More</p>
<p id="less">See Less</p>