一个项目想用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;
}
}
复制代码 代码如下:
read more
slide!
slide!
slide!
slide!
slide!
read more
slide!
slide!
slide!
slide!
slide!
viewbutton的层级可自行修改,但要注意动画的目标是什么。我写的viewbutton会对它上一级的那个div做动画,所以就不用把选择器写得过于复杂了。