,所以我正在研究一个项目,并对搜索文本框进行了动画,如下所示:
input[type=text] {
width: 130px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
height: 30px;
}
input[type=text]:focus {
width: 700px;
}
<input type="text" name="search" placeholder="Search..">
一切正常工作,但是现在我有一个问题,我在同一页面上有更多的文本框,我不希望它们像这一内容一样被动画,因为输入[type = text]正在更改它们。任何帮助都将受到很多赞赏。
有几种方法:
- 在
input
中使用类或ID
input[type="text"] {
width: 130px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
height: 30px;
}
input[type="text"]:focus {
width: 700px;
}
#animated {
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
}
<input id="animated" type="text" name="search" placeholder="Search..">
<hr />
<input type="text" name="search" placeholder="Search..">
- 使用另一个属性,在这种情况下,
[name="search"]
input[type="text"] {
width: 130px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
height: 30px;
}
input[type="text"]:focus {
width: 700px;
}
input[name="search"] {
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
}
<input type="text" name="search" placeholder="Search..">
<hr />
<input type="text" name="whatever" placeholder="Search..">
您需要更改CSS选择器更具体而不是input[type=text]
,也许是input[name=search]
假设您不想添加ID或类,然后将选择器更改为...
input[name="search"]
您可以使用CSS选择器,例如:first-child
选择网站上的第一个输入。
input[type=text] {
width: 130px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
height: 30px;
}
input[type=text]:first-child:focus {
width: 700px;
}
<input type="text" name="search" placeholder="Search..">
<hr>
<input type="text" name="search" placeholder="Search..">
另一种方法是将id
设置为特定的输入ANG更改CSS选择器至#givenID:focus
。
input[type=text] {
width: 130px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding: 12px 20px 12px 40px;
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
height: 30px;
}
#inputID:focus {
width: 700px;
}
<input id="inputID" type="text" name="search" placeholder="Search..">
<hr>
<input type="text" name="search" placeholder="Search..">
在输入文本中添加类,例如 animated
<input type="text" class="animated" name="search" placeholder="Search..">
然后在您的CSS中进行修改:
input.animated:focus {
width: 700px;
}