A project wants to use jQuery to create a DIV element that can be semi-folded, so it writes it by itself because accordion does not provide relevant methods in jQueryUI. In the past, when using jQueryUI, we found that all the accordion that can be used were folded up, and there was no way to set the minimum collapse height.
The code quality is very low. I hope laruence can give some advice.
Is the effect display, which can be expanded and reduced by jQuery functions.
Copy codeThe Code is as follows: // author: hlhr
// Require: Jquery1.4 and above
Function animate_toggle_height (maxh, minh, maxo, mino, element, speed) {// This is vertical. parameter description: maximum height, minimum height, maximum transparency, minimum transparency, element, animation speed
If (element.css ("height") = minh. toString (). concat ("px") {// expand if it is the minimum height
Element. animate ({
Height: maxh,
Opacity: maxo
}, {Queue: false}, speed );
Return "Fold"
}
If (element.css ("height") = maxh. toString (). concat ("px") {// fold if it is the maximum height
Certificate (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 ";
}
}
Copy codeThe Code is as follows: <Head>
<Script src = "jquery-1.9.1.min.js"> </script> <! -- Jquery library required -->
<Script src = "jqslider. js"> </script> <! -- Link to the above js library -->
<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" mirror.css ({opacity: min_o}); // sets the initial transparency.
$ (". Slide" ).css ({height: min_h}); // you can specify the initial height.
$ (". Viewbutton"). click (function () {// only vertical expansion is called here. You can also call horizontal expansion
Dimensions (this).html (animate_toggle_height (max_h, min_h, max_o, min_o, $ (this). parent (), 500); // automatically identifies it as the upper-level animation of viewbutton
});
</Script>
</Body>
</Html>
You can modify the viewbutton hierarchy, but pay attention to the animation target. The viewbutton I wrote will be animated to the div at the upper level, so there is no need to write the selector too complicated.