过渡输入宽度

Transition input width

我有输入搜索字段。当我点击它时,我需要它变得更宽,执行从右到左的平滑过渡,即输入位于页面的右侧,当我点击它时,它应该向左拉伸一些像素并变成更宽的。

这是我的 css:

#header #search input {
background: #FFF;
padding: 1px 1px 1px 5px;
width: 178px;
height: 21px;
border: 1px solid #CCCCCC;
border-radius: 6px;
-webkit-transition: all .5s;
-moz-transition: all .5s;
transition: all .5s;

}
#header #search input:focus {
background: #FFF;
padding: 1px 1px 1px 5px;
width: 300px;
height: 21px;
border: 1px solid #CCCCCC;

你能帮我实现吗?

input[type="search"] {
  background: #FFF;
  padding: .5rem 1rem;
  position: relative;
  top: 0;
  left: 0;
  width: 178px;
  height: 40px;
  outline: none;
  border: 1px solid #CCCCCC;
  border-radius: 6px;
  transition: all .5s;
}

input[type="search"]:focus {
  width: 300px;
  top: 0;
  right: 100%;
}
<div style="text-align:right;">
  <input type="search" id="search" />
</div>

绝对定位输入并将右边属性设置为零:

position:absolute;
right:0;

聚焦时强制向左扩展。

jsFiddle example

使用 position: relative 让你的容器偏移父级。

#header, #search {
    width: 100%;
    position: relative;
}

然后使用 position: absolute 定位您的输入并使用 right 放置它。

#header #search input {
    position: absolute;
    right: 0px;
    background: #FFF;
    padding: 1px 1px 1px 5px;
    width: 178px;
    height: 21px;
    border: 1px solid #CCCCCC;
    border-radius: 6px;
    -webkit-transition: all .5s;
    -moz-transition: all .5s;
    transition: all .5s;
}

#header #search input:focus {
background: #FFF;
padding: 1px 1px 1px 5px;
width: 300px;
height: 21px;
border: 1px solid #CCCCCC;

您的动画现在将向左拉伸。