如何向下滑动 div 然后 .fadeIn() 内容,反之亦然?
目标
当用户单击该按钮时,相关 div 将:
- 向下滑动
- 停止
- 淡入内容
当用户再次单击该按钮时,该 div 将:
- 淡出
- 停止
- 向上滑动
当前位置
这是一个示例,其中 fadeIn 和淡出发生在正确的时间,但在淡入和淡出前后分别没有幻灯片效果
http://jsfiddle.net/tkRGU/1/
另外还有一个选项,它具有 slideToggle 功能,但没有分别在幻灯片之后和之前发生的 fadeIn 和 fadeOut。
http://jsfiddle.net/MY8DD/7/
Goal
When a user clicks the button, the div in question will:
- slide down
- stop
- fade in the content
When the user clicks the button again, the div will:
- fade out
- stop
- slide up
Current position
Here is an example where the fadeIn and fadeOut is happening at the right time but there is no slide effect before and after the fadeIn and fadeOut respectively
http://jsfiddle.net/tkRGU/1/
Also there is this option which has the slideToggle function but does not have the fadeIn and fadeOut occuring after and before the slide respectively.
http://jsfiddle.net/MY8DD/7/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这将起作用:
HTML:
Javascript:
工作示例关于 JS Fiddle。
对于 IE,只需确保 ClearType 内容后面有背景色即可。
This will work:
HTML:
Javascript:
Working example on JS Fiddle.
For IE just make sure there is a background color behind the content for cleartype.
听起来因为您希望这两个操作同时发生,所以您应该使用 animate 函数。否则行动就会接二连三地发生。
如果您在运行元素之前知道元素的高度,那么您可以相当轻松地进行设置。这是一个非常粗略的示例: http://jsfiddle.net/tArQu/
It sounds like since you want the two operations to occur simultaneously that you should use the animate function. Otherwise the actions will come one after another.
If you know the height of the element before running it, then you can set things fairly easily. Here's an extremely rough example: http://jsfiddle.net/tArQu/
这就是你所追求的?
That what you're after?
如果你事先不知道元素的高度,那就稍微复杂一些。您必须直接设置不透明度动画以使其淡出,并且必须在“滑动”时使用 CSS 可见性隐藏内容。
CSS 可见性“隐藏”允许内容正常占据文档中的空间,但从视图中隐藏; CSS 显示“none”不仅隐藏元素,还将其从文档流中删除。通过使用可见性隐藏元素,我们可以将其向下滑动直到达到其完整高度,而元素的内容保持不可见。
类似地,使用 jQuery 的 fadeIn 函数淡入淡出内容假定元素最初是隐藏的且显示为“none”,因此如果我们使用可见性,它将不起作用。相反,我们使元素最初完全透明(不透明度 0.0);滑动动画完成后,我们将可见性设置为“可见”,然后将不透明度从完全透明设置为完全不透明(0.0 到 1.0)。
假设该元素最初是隐藏的(CSS 显示“none”或 jQuery 隐藏功能):
注意:输入“visibility”和“visible”时要格外小心,因为它们很容易拼写错误——这是许多令人沮丧的错误的根源。
您不必使用可见性,因为您可以通过使内容最初透明来完成同样的事情,但使用它可以使正在发生的事情更加明确。也就是说,这也有效:
If you don't know the height of the element in advance, it is slightly more complicated. You have to animate the opacity directly to fade, and you must hide the content with CSS visibility while it is "sliding".
CSS visibility "hidden" allows content to occupy the space in the document it normally would, but to be hidden from view; CSS display "none" doesn't just hide the element, it removes it from the document flow. By hiding the element using visibility, we can slide it down until it is its full height, while the content of the element remains invisible.
Similarly, fading content in using jQuery's fadeIn function assumes an element is initially hidden with display "none", so it won't work if we use visibility. Instead, we make the element initially fully transparent (opacity 0.0); once the sliding animation is complete, we set visibility to "visible" and then animate the opacity from fully transparent to fully opaque (0.0 to 1.0).
Assuming the element is initially hidden (CSS display "none" or jQuery hide function):
N.B.: Be extra careful typing "visibility" and "visible" as they are easily misspelled -- the source of many frustrating bugs.
You don't HAVE to use visibility, as you can accomplish the same thing by making the content initially transparent, but using it makes it more explicit what is going on. That is, this also works:
尝试一下:http://jsfiddle.net/solidsource/67XZX/
Give this a shot: http://jsfiddle.net/solidsource/67XZX/