您可以利用这个max-width
技巧,它并不完美,但它解决了将数值转换为字符串状态的问题:
http://jsfiddle.net/Cqmuf/1/
(以上内容已更新float:left
)
这种方法的缺点是你必须为你的菜单设置一个最大宽度,但我通常发现这是一件好事。
标记:
<div class="menu">
<a href="#">[i] Hello</a>
<a href="#">[i] There</a>
</div>
CSS:
div a {
display: block;
overflow: hidden;
white-space: nowrap;
}
.menu {
transition: all 2s;
max-width: 17px;
/*
here you can set float:left or position:absolute
which will force the menu to collapse to it's minimum
width which, when expanded, will be the actual menu
item widths and not max-width.
*/
float: left;
}
.menu:hover {
/*
If you have the possibility of varied widths, i.e. multilingual
then make sure you use a max-width that works for them all. Either
that or do what a number of multilingual sites do and set a body
class that states the current language, from there you can then
tailor your max-width differently e.g. wider for German.
*/
max-width: 300px;
}
多语言的单独维度示例:
.lang-de .menu:hover { max-width: 400px; }
.lang-gb .menu:hover { max-width: 300px; }
因此,您实际上是在修改max-width
属性,而不是转换宽度,您可以更轻松地将其设置为固定值,这一切都是因为它只会在达到此限制时才开始使用,并且在此之前保持不可见。