当前位置:Gxlcms > JavaScript > jQuery+CSS半开折叠效果原理及代码(自写)_jquery

jQuery+CSS半开折叠效果原理及代码(自写)_jquery

时间:2021-07-01 10:21:17 帮助过:23人阅读

一个项目想用jQuery做一个可以半折叠的DIV元素,苦于jQueryUI中accordion没有提供相关的方法,就自己写了个。以前使用jQueryUI的时候发现能够用的accordion全部折叠起来了,没办法设置折叠的最小高度。
代码质量很低,希望老鸟能够指点指点。

下图是效果展示,能够借由jQuery的函数展开收缩

代码如下:

//author: hlhr
//require: Jquery1.4 and above
function animate_toggle_height(maxh,minh,maxo,mino,element,speed) {//这个是纵向的,参数解释:最大高度,最小高度,最大透明度,最小透明度,元素,动画速度
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";
}
}

代码如下: