jQuery控制元素上下左右滑動。 Although jQuery has a nice set of slide methods — Reverse the Slide DirectionWith the built-in slide methods, elements are shown by sliding them down and into view. But what if we want to slide something from the bottom up and into view? The trick here is to use some judicious CSS. Let's start with a simple HTML structure: HTML:
To get the inner div to slide up, we'll anchor its bottom edge to the bottom of the bottom of the nearest positioned ancestor (in this case, the #slidebottom div): CSS:
Other properties such as width, padding, margin, and background-color have been set for these elements, but only the essential properties for modifying the slide behavior are shown above. Note: I'll be using the term positioned to refer to elements that have the CSS Now, we can write the jQuery the same way we would with a traditional slide effect: JavaScript:
Try it out: Horizontal SlidesAnimate WidthWe can also slide elements to the left and right. The simplest way is to animate the element's width property. JavaScript: In this case it's not necessary for the sliding element to be positioned. Animate this element's width
While animating the width is fine for what it is, I'm not crazy about how the text wraps as the width decreases. One way to avoid the wrapping is to add a CSS declaration such as Animate LeftAnother way to avoid the text-wrap issue is to animate the element's With this animation, we need to calculate how far to move the element. The following code rests on two assumptions: (1) the sliding element has an outerWidth() that is equal to or greater than its parent element's width, and (2) the sliding element is initially set to JavaScript: Lines 5 through 7 use a "ternary" operator that basically says, "If the left css property equals 0, move the element to the left as many pixels as it is wide (including padding and border); if not, move it back to 0." Animate this element's left style property
Also, if we want the element to be hidden when it slides to the left, we need to add Animate Left MarginFinally, we can achieve the same effect as the left animation by animating the JavaScript: For the sake of variety, we're sliding this one to the right. Animate this element's margin-left style property
With a couple little tweaks, these horizontal slides can be used for a horizontal accordion. Further Resources
Scripts included in this post: |
|