是否可以通过 Easing 滑动切换 div 并设置最小高度?
我有一个div,它使用slideToggle 和缓动来折叠和展开。
$('button').click(function () {
$('div').slideToggle('2000', "easeOutBounce");
});
我希望它在向上滑动时具有最小高度,以便始终有一个小的可见内容。
因此,当它向下滑动时,具有 height:(div height)
,而在 slideUp 上,height:10px;
请注意,div 可以具有任意数量的高度,这就是我不使用动画的原因。
I have a div of which collapse and expands using slideToggle and easing.
$('button').click(function () {
$('div').slideToggle('2000', "easeOutBounce");
});
I want it when it slide's Up to have a minimum height so that there always a small visible content of it.
So when it's slide down has height:(div height)
and on slideUp, height:10px;
Note that the div could have any amount of height so that's why I am not using animation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能想到的最好的方法如下:
JS Fiddle 演示。
然而,这个演示有一些问题:
if
/else if
语句。each()
来分配 div 元素的“默认”/“自然”高度。div
不是position:absolute
,它们会缩小到内联元素的基线(因为它们是display: inline-block< /code>),如果它们是
float: left
(或者显然是float: right
),这可能不是问题。希望它有用,但就目前的状态来说肯定不理想。
编辑以发布上述插件化版本(它保留了与以前相同的所有问题):
JS Fiddle 演示。
30
,代表您要达到的高度想要元素滑动到。如果没有提供单位,则高度默认以像素为单位。带引号的字符串,定义动画中使用的缓动类型;如果没有 jQuery UI,这要么是“线性”,要么是“摆动”。 jQuery UI 可能还有其他选项,但这尚未经过测试。如果未指定,动画默认为“线性”。因为在带引号的字符串中使用单位会导致最终的在发布本文之前,我没有意识到一个更大的问题,即插件版本仅允许动画一次迭代。我对此感到困惑,尽管也许对其他人来说这是显而易见的?
好吧,看来是else if :
if
语句中对高度的评估。使用3em
导致最终的curH ==toggleMinHeight
返回 false。大概是由于该变量中存在单位 (em
)。上述指南已被编辑,以反映不应使用这些单位。参考文献:
animate()
。:animated
选择器。attr()
。click()
。each()
。height()
。is()
。The best I could come up with is the following:
JS Fiddle demo.
This demo has some issues, however:
if
/else if
statement.each()
to assign the 'default'/'natural' height of the div elements.div
s aren'tposition: absolute
they shrink down to the baseline of the in-line elements (as they'redisplay: inline-block
), this may not be a problem if they'refloat: left
(orfloat: right
, obviously).Hopefully it's of use, but it's certainly not ideal in its current state.
Edited to post the plugin-ised version of the above (it retains all the same issues as before):
JS Fiddle demo.
30
, and represents the height to which you want the element to slide to. If there are no units supplied then the height is, by default, considered to be in pixels.a quoted string defining the type of easing to be used in the animation; without jQuery UI this is either 'linear' or 'swing'. With jQuery UI other options may be possible, but this is untested. If unspecified the animation defaults to 'linear.'Because using units in a quoted string causes the finalelse if
to return false (obviously, I suppose...sigh), please use only an unquoted numerical argument for this variable (or edit the plug in to properly deal with quoted strings with units...).A larger issue that I hadn't realised until I posted this, is that the plugin version only allows one iteration of the animation. Which I'm confused about, though perhaps it's obvious to someone else?
Okay, it appears to be the evaluation of the height in the
if
statement. Using3em
caused the finalelse if
:curH == toggleMinHeight
to return false. Presumably due to the presence of the unit (em
) in that variable. The above guidance has been edited to reflect that the units should not be used.References:
animate()
.:animated
selector.attr()
.click()
.each()
.height()
.is()
.并不真地。当调用 Slide 方法时,它会获取 style 属性
但是,您可以通过使用回调函数动态修复它
动画到底有什么问题?
Not really. When the slide method is called, it gets the style property
However, you may be able to dynamically fix it by using the callback function
What exactly is the problem with animation?
我建议你玩点诡计;
为什么不在滑动 div 上方有一个具有相同宽度和颜色的 div,以便它始终显示并作为滑动部分的一部分出现
I suggest that you play it tricky;
why dont you have a div above the sliding div that has the same width and color so that it is always displayed and appears as part of the sliding part