我的小提琴。如果您单击this fiddle中的项目,它将滑动到特定高度。
小提琴显示带有一些“摘要文本”的部分(例如,当您单击打开它时预览完整加载的文本)。
目的是,在点击项目时加载“完整消息”,然后仅在内容到达的时间内将其滑动打开。
我的第一个想法是:
嗯,我想这会奏效。但我的程序员的感官告诉我,这是错误的做法。我不知道“长篇文章”会有多长,所以它可能只是几行,但它也可以是一篇完整的文章。
那么,只要内容有效,<section>
打开哪种方法最好?
由于关闭的<section>
具有固定的高度,因此再次关闭它并不困难。
HTML:
<section class="mbox mbox_closed" id="effect1_1">
<h2>BUSINESS PC</h2>
<div class="mbox_content" id="effect2_1">
<h3>Lorem ipsum dolor sit amet, consetetur sadipscing elitr!
<span>sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam</span>
</h3>
<p>Erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
</div>
<hr>
<img src="http://www.club-3d.com/tl_files/club3d/uploads/en/content/Accesories/CAC-1052/cac-1052_use_01.png" alt="Testbild">
<!--<div class="mbox_bar"></div>-->
</section>
CSS:
@import url(http://fonts.googleapis.com/css?family=Open+Sans);
body, html {
height:100%;
padding:0;
margin:0;
background:#5cafff;
font-family:'Open Sans', sans-serif;
}
.mbox {
z-index: 1;
margin: 2em;
position:relative;
background: #ff1a00;
background: linear-gradient(to right, #ffffff 33%, #ff1a00 100%);
border: 1px solid #fff;
height:200px;
clear:both;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=90)";
filter: alpha(opacity=90);
-moz-opacity: 0.9;
-khtml-opacity: 0.9;
opacity: 0.9;
}
.mbox_closed:hover {
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
filter: alpha(opacity=100);
-moz-opacity: 1.0;
-khtml-opacity: 1.0;
opacity: 1.0;
-moz-box-shadow: 0px 0px 12px 8px rgb(128,128,128);
-webkit-box-shadow: 0px 0px 12px 8px rgb(128,128,128);
box-shadow: 0px 0px 12px 8px rgb(128,128,128);
}
.mbox_active {
z-index: 1;
margin: 2em;
position:relative;
background: #ff1a00;
background: linear-gradient(to right, #ffffff 33%, #ff1a00 100%);
border: 1px solid #fff;
height:200px;
clear:both;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
filter: alpha(opacity=100);
-moz-opacity: 1.0;
-khtml-opacity: 1.0;
opacity: 1.0;
}
.mbox * {
padding:0;
margin:0;
}
.mbox_content {
z-index: 2;
float:left;
padding: 20px;
background: #ffffff;
width:60%;
height:160px;
overflow: hidden;
overflow-y: hidden;
}
.mbox img {
z-index:-1;
position:absolute;
right:0;
max-height:100%;
}
.mbox hr {
z-index: 2;
float:left;
width: 0;
height: 0;
border-style: solid;
margin-top: 30px;
border-width: 15px 0 15px 20px;
border-color: transparent transparent transparent #ffffff;
}
.mbox_bar {
z-index: 2;
float:right;
width: 30;
height: 100%;
border-style: solid;
border-width: 0 10px 3px 3px;
border-color: #ffffff #ffffff #ffffff #ffffff;
}
.mbox h2 {
text-transform: uppercase;
font-weight:normal;
font-size:1em;
top:-0.7em;
left: 0.5em;
position:absolute;
color:#fff;
background:#ff1a00;
padding:0 1em;
border:1px solid #fff;
border-radius:1em;
}
.mbox_content h3 {
font-size:1.4em;
color:#666;
border-left:2px solid #ccc;
padding-left: 1em;
margin-bottom:0.5em;
}
.mbox_content h3 span {
margin-top:0.5em;
display:block;
font-size:0.7em;
text-transform: uppercase;
font-weight:normal;
}
JS:
var state = []
$(function() {
state[1] = true;
$( "#effect1_1" ).click(function() {
if ( state[1] ) {
$( "#effect1_1" ).animate({
height: 600
}, 1000 );
$( "#effect2_1" ).animate({
height: 560
}, 1000 );
$( "#effect1_1" ).removeClass( "mbox_closed" ).addClass( "mbox_active" );
} else {
$( "#effect1_1" ).animate({
height: 200
}, 1000 );
$( "#effect2_1" ).animate({
height: 160
}, 1000 );
$( "#effect1_1" ).removeClass( "mbox_active" ).addClass( "mbox_closed" );
}
state[1] = !state[1];
});
});
答案 0 :(得分:3)
制作动画时,请使用scrollHeight
属性:
元素滚动视图的高度;它包括元素 填充但不是它的边缘。
所以你的JS代码就像这样:
var state = []
$(function() {
state[1] = true;
$( "#effect1_1" ).click(function() {
if ( state[1] ) {
$( "#effect1_1" ).animate({
height: $( "#effect2_1" ).prop( "scrollHeight" )
}, 1000 );
$( "#effect2_1" ).animate({ // since scrollHeight brings the padding too, we need to remove it so the div not 'pops out' the container
height: $( "#effect2_1" ).prop( "scrollHeight" ) - parseInt( $( "#effect2_1" ).css( "padding-top" ), 10 ) - parseInt( $( "#effect2_1" ).css( "padding-bottom" ), 10 )
}, 1000 );
$( "#effect1_1" ).removeClass( "mbox_closed" ).addClass( "mbox_active" );
} else {
$( "#effect1_1" ).animate({
height: 200
}, 1000 );
$( "#effect2_1" ).animate({
height: 160
}, 1000 );
$( "#effect1_1" ).removeClass( "mbox_active" ).addClass( "mbox_closed" );
}
state[1] = !state[1];
});
});
更新了fiddle。