如何使用CSS扩展<li>(如Apple.com)</li>

时间:2013-10-29 02:44:09

标签: html css html-lists expand

enter image description here

活小提琴:

http://jsfiddle.net/jMAzr/1/

Apple.com上的工作示例: http://www.apple.com/

<html>
<head>
<style>
body {
 background:#000000;   
}
li {
    list-style:none;
    display: list-item;
}
ul.menu li {
    float:left;
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
    text-decoration: none;
    font-weight: 400;
}
ul.menu li a {
    text-decoration:none;
}
.header-tab-title {
    display: block;
    position: relative;
    height: 46px;
    line-height: 46px;
    cursor: pointer;
    font-size: 16px;
    color: #fff;
    text-align: center;
}
.search-field {
    width: 70px;
    border-radius:10px;
    transition: width 0.5s ease-in-out,margin-left 0.5s ease-in-out;
    -webkit-transition: width 0.5s ease-in-out,margin-left 0.5s ease-in-out;
    -moz-transition: width 0.5s ease-in-out,margin-left 0.5s ease-in-out;
    -o-transition: width 0.5s ease-in-out,margin-left 0.5s ease-in-out;
    animation-direction:reverse;
    -webkit-animation-direction:reverse; /* Safari and Chrome */
    border: 1px solid #353535;
    height:18px;
    padding: 0px 23px 4px;
    color:#FFF;
}
.search-field:focus {
    color:#000;
    width: 140px;
    margin-left:-70px; 
    height:20px;
}
</style>
</head>
<body>
<ul class="menu">
<li><span class="header-tab-title" style="width:124px;">Produtos</span></li>
<li><span class="header-tab-title" style="width:151px;">Sites Prontos</span></li>
<li><span class="header-tab-title"style="width:192px;" >Anuncie no Google</span></li>
<li><span class="header-tab-title" style="width:230px;">Facebook Para Negócios</span></li>
<li><span class="header-tab-title" style="width:152px;">Hospedagem</span></li>
<li><span class="header-tab-title" style="width:110px;"><input class="search-field" type="search"></span></li>
</ul>
</body>
</html>

2 个答案:

答案 0 :(得分:3)

您正在寻找的行为需要一个表格布局来随着一个人的增长而改变其他“单元格”的宽度。我用这种布局创建了一个简化的小提琴:

http://jsfiddle.net/Hp3JU/

我删除了几个元素来简化示例,但解决方案的本质是外部display: table,然后是display:table-row,然后每个“单元格”为display:table-cell 。给“表”一个固定的宽度并删除:hover伪类上的负边距,以防止最后一个单元格与其邻居重叠。您还需要移除硬编码的固定宽度,以便您的单元格可以根据需要折叠以腾出空间。

<div class="table">
    <ul class="menu">
      <li>Produtos</li>
      <li>Sites Prontos</li>
      <li>Anuncie no Google</li>
      <li>Facebook Para Negócios</li>
      <li>Hospedagem</li>
      <li><input class="search-field" type="search" /></li>
    </ul>
</div>

CSS(仅限相关部分):

.table {
    display: table;
    width: 100%;
}
ul.menu {
    display: table-row;
}
ul.menu > li {
    display: table-cell;
}
.search-field {
    width: 70px;
    border-radius:10px;
    transition: width 0.5s ease-in-out,margin-left 0.5s ease-in-out;
    -webkit-transition: width 0.5s ease-in-out,margin-left 0.5s ease-in-out;
    -moz-transition: width 0.5s ease-in-out,margin-left 0.5s ease-in-out;
    -o-transition: width 0.5s ease-in-out,margin-left 0.5s ease-in-out;
    animation-direction:reverse;
    -webkit-animation-direction:reverse; /* Safari and Chrome */
    border: 1px solid #353535;
    height:18px;
    padding: 0px 23px 4px;
    color:#FFF;
}
.search-field:focus {
    color:#000;
    width: 140px;
    height:20px;
}

答案 1 :(得分:1)

当用户nav成为搜索字段时,Apple会通过向focus元素添加类来实现此目的。

Default State: default state

Active State:

active state

因此,为了模仿相同的功能,您需要使用JavaScript将类添加到ul.menu元素。该类将更改菜单的宽度。使用CSS动画来控制滑动效果。

相关问题