欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery+CSS 半開折疊效果原理及代碼(自寫)

 更新時間:2013年03月04日 14:34:52   作者:  
jQuery做一個可以半折疊的DIV元素由于jQueryUI中accordion沒有提供相關(guān)的方法因此自己寫了個,代碼質(zhì)量很低希望大家多多指點(diǎn)
一個項目想用jQuery做一個可以半折疊的DIV元素,苦于jQueryUI中accordion沒有提供相關(guān)的方法,就自己寫了個。以前使用jQueryUI的時候發(fā)現(xiàn)能夠用的accordion全部折疊起來了,沒辦法設(shè)置折疊的最小高度。
代碼質(zhì)量很低,希望老鳥能夠指點(diǎn)指點(diǎn)。

下圖是效果展示,能夠借由jQuery的函數(shù)展開收縮
 
復(fù)制代碼 代碼如下:

//author: hlhr
//require: Jquery1.4 and above
function animate_toggle_height(maxh,minh,maxo,mino,element,speed) {//這個是縱向的,參數(shù)解釋:最大高度,最小高度,最大透明度,最小透明度,元素,動畫速度
if (element.css("height")==minh.toString().concat("px")){//如果是最小高度就展開
element.animate({
height:maxh,
opacity:maxo
},{queue: false},speed);
return "Fold"
}
if (element.css("height")==maxh.toString().concat("px")){//如果是最大高度就折疊
$(this).html("");
element.animate({
height:minh,
opacity:mino
},{queue: false},speed);
return "Read more";
}
}
function animate_toggle_width(maxw,minw,maxo,mino,element,speed) {
if (element.css("width")==minw.toString().concat("px")){
element.animate({
width:maxw,
opacity:maxo
},{queue: false},speed);
return "Fold"
}
if (element.css("width")==maxw.toString().concat("px")){
element.animate({
width:minw,
opacity:mino
},{queue: false},speed);
return "Read more";
}
}

復(fù)制代碼 代碼如下:

<html>
<head>
<script src="jquery-1.9.1.min.js"></script><!--需要jquery庫-->
<script src="jqslider.js"></script><!--鏈接上文的js庫-->
<style>
body{margin: 0 auto; text-align:center;}
.slide{font-size:20px; overflow: hidden; width: 500px; }
#content{margin:0 auto; width: 500px;}
.viewbutton{position:relative; text-align: right;}
</style>
</head>
<body>
<fieldset id="content">
<div class="slide">
<a class="viewbutton" href="#">
Read more
</a>
<p>slide!</p>
<p>slide!</p>
<p>slide!</p>
<p>slide!</p>
<p>slide!</p>
</div>
</fieldset>
<p />
<fieldset id="content">
<div class="slide">
<a class="viewbutton" href="#">
Read more
</a>
<p>slide!</p>
<p>slide!</p>
<p>slide!</p>
<p>slide!</p>
<p>slide!</p>
</div>
</fieldset>

<script type="text/javascript">
var max_h=300;
var min_h=100;
//var max_w=500;
//var min_w=10;
var max_o=1;
var min_o=0.3;
$(".slide").css({opacity:min_o});//設(shè)置初始的透明度
$(".slide").css({height:min_h});//設(shè)置初始的高度
$(".viewbutton").click(function(){//這里只是調(diào)用了縱向展開,也可以調(diào)用橫向展開
$(this).html(animate_toggle_height(max_h,min_h,max_o,min_o,$(this).parent(),500));//自動識別為viewbutton的上一級進(jìn)行動畫
});
</script>
</body>
</html>

viewbutton的層級可自行修改,但要注意動畫的目標(biāo)是什么。我寫的viewbutton會對它上一級的那個div做動畫,所以就不用把選擇器寫得過于復(fù)雜了。

相關(guān)文章

最新評論