在 CSS3 或 jQuery 中,将元素的宽度从 0 调整到 100%,使其及其包装器的宽度达到所需的宽度,无需预设宽度
http://jsfiddle.net/nicktheandroid/tVHYg/
悬停时 .wrapper
,它的子元素 .contents
应该从 0px
动画到它的自然宽度。然后,当鼠标从 .wrapper
中移除时,它应该以动画方式返回到 0px
。 .wrapper
元素的宽度应仅达到所需的宽度(允许 .contents
增长),因此 .wrapper
的宽度应增长并像 .contents
那样缩小宽度。 .contents
不应设置宽度。我正在使用 CSS3,但它可以在 jQuery 中完成,那就没问题了。
问题: 看到 JSfiddle
.wrapper
不仅有它需要的宽度。- 当
.contents
增长时,当它几乎达到全宽时,它会跳到下一行当 - 悬停在
.wrapper
、.contents
上时当它应该动画到0px
时消失
.wrapper {
display: inline-block;
height: 20px;
width: auto;
padding:10px;
background:#DDD;
}
.contents {
display:inline-block;
width:0%;
white-space:nowrap;
overflow:hidden;
background:#c3c;
}
.wrapper:hover .contents {
-webkit-transition: width 1s ease-in-out;
-moz-transition: width 1s ease-in-out;
-o-transition: width 1s ease-in-out;
transition: width 1s ease-in-out;
width:100%;
}
<div class="wrapper">
<span>+</span>
<div class="contents">These are the contents of this div</div>
</div>
http://jsfiddle.net/nicktheandroid/tVHYg/
When hovering .wrapper
, it's child element .contents
should animate from 0px
to it's natural width. Then when the mouse is removed from .wrapper
, it should animate back down to 0px
. The .wrapper
element should only be as wide as it needs to be (allowing .contents
to grow), so .wrapper
should grow in width and shrink in width as .contents
does. There should be no set width for .contents
. I'm using CSS3, but it could be accomplished in jQuery, that would be fine.
The problem:
See the JSfiddle
.wrapper
is not only as wide as it needs to be.- when
.contents
grows, when it's almost at it's full width, it jumps down to the next line - When hovering off of
.wrapper
,.contents
vanishes, when it should animate down to0px
.wrapper {
display: inline-block;
height: 20px;
width: auto;
padding:10px;
background:#DDD;
}
.contents {
display:inline-block;
width:0%;
white-space:nowrap;
overflow:hidden;
background:#c3c;
}
.wrapper:hover .contents {
-webkit-transition: width 1s ease-in-out;
-moz-transition: width 1s ease-in-out;
-o-transition: width 1s ease-in-out;
transition: width 1s ease-in-out;
width:100%;
}
<div class="wrapper">
<span>+</span>
<div class="contents">These are the contents of this div</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我想我已经明白了。
动画设置为
100%
会导致其换行,因为该框大于可用宽度(100% 减去+
及其后面的空格)。相反,您可以为内部元素设置动画,其
100%
是.contents
的总宽度。I think I've got it.
Animating to
100%
causes it to wrap because the box is bigger than the available width (100% minus the+
and the whitespace following it).Instead, you can animate an inner element, whose
100%
is the total width of.contents
.http://jsfiddle.net/tVHYg/5/
http://jsfiddle.net/tVHYg/5/
一个较晚的答案,但我认为这个按照问题中的要求工作:)
这个使用 z-index 和绝对位置,并避免容器元素宽度在过渡中不增长的问题。
您可以调整文本的边距和填充以满足您的需要,并且如果需要,“+”可以更改为字体很棒的图标。
a late answer, but I think this one works as required in the question :)
this one uses z-index and position absolute, and avoid the issue that the container element width doesn't grow in transition.
You can tweak the text's margin and padding to suit your needs, and "+" can be changed to font awesome icons if needed.
通过转换填充和宽度让它工作。
JSFiddle: http://jsfiddle.net/tuybk748/1/
Got it to work by transitioning the padding as well as the width.
JSFiddle: http://jsfiddle.net/tuybk748/1/
请检查以下代码片段
Please check following snippet
这还不是一个得到广泛支持的解决方案,但希望将其留在这里供未来的读者使用。
目前(截至 2024 年 11 月 11 日)calc-size 尚未真正得到支持。但是,一旦浏览器开始实现它并且不需要旧版浏览器支持,它就可以用于设置宽度和高度的动画。引用链接文档:
在 MDN 文档中,还有一个 示例 使用
calc-size
函数进行基于高度的过渡。例如,您可以使用最新的 Chrome 对其进行测试。This is not a widely supported solution yet, but wanted to leave it here for future readers.
Currently (as of 2024-11-11) the calc-size is not really supported yet. However, as soon as the browsers start to implement it and no legacy browser support is needed, it can be used to animate both width and height. Quote from the linked docs:
In the MDN docs, there is also an example for height-based transition using the
calc-size
function. You can test it with a recent Chrome, for example.我无法在不指定宽度的情况下使其工作,但以下 css 工作了
我'我不确定您是否能够在不设置宽度的情况下使其正常工作。
I haven't been able to get it to work without specifying a width but the following css worked
I'm not sure you will be able to get it working without setting a width on it.