我的css过渡搜索框不起作用

时间:2015-12-14 01:59:57

标签: jquery html css3 css-transitions

  

我希望我的搜索输入显示在我点击按钮后,在转换中,但它似乎不适合我...任何帮助?   这是我的代码

     

这是我的css部分

<style>
#search {
position: absolute;
top: 0;
left: 0;
width: 60px;
height: 60px;
}
#label {
width: 60px;
height: 60px;
position: relative;
z-index: 20;
background:red;
}
#label label {
display: block;
width: 60px;
height: 60px;
background: url("search.png") 0 0;
font-size: 0;
color: rgba(0, 0, 0, 0);
text-indent: -9999px;
cursor: pointer;
}
#label label:hover {
background: url("search.png") -60px 0
}
#label.active label {
background: url("search.png") -60px 0
}
#input {
position: absolute;
top: 0;
left: 50px;
width: 450px;
height: 60px;
z-index: 5;
overflow: hidden;
background-color:green;
}
#input input {
display: block;
position: absolute;
top: 0;
left: -450px;
width: 450px;
height: 100%;
margin: 0;
padding: 0 10px;
border: none;
background-color: #23688b;
color: #fff;
font-size: 18px;
backface-visibility: none;
border-radius: 0;
transition: position 0.3s ease ;
}
#input input:focus {
outline: none
}
#input.focus {
z-index: 20;
}
#input.focus input {

position: left;
}
</style>
  

这是正文部分

<form id="search" action="#" method="post">
<div id="label"><label for="search-terms" id="search-label">search</label></div>
<div id="input"><input type="text" name="search-terms" id="search-terms" placeholder="Enter search terms..."></div>
</form>
  

最后这是脚本部分

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">
</script>
<script>
(function(window){

// get vars
var searchEl = document.querySelector("#input");
var labelEl = document.querySelector("#label");

// register clicks and toggle classes
labelEl.addEventListener("click",function(){
if (classie.has(searchEl,"focus")) {
classie.remove(searchEl,"focus");
classie.remove(labelEl,"active");
} else {
classie.add(searchEl,"focus");
classie.add(labelEl,"active");
}
});

// register clicks outisde search box, and toggle correct classes
document.addEventListener("click",function(e){
var clickedID = e.target.id;
if (clickedID != "search-terms" && clickedID != "search-label") {
if (classie.has(searchEl,"focus")) {
classie.remove(searchEl,"focus");
classie.remove(labelEl,"active");
}
}
});
}(window));
</script>

1 个答案:

答案 0 :(得分:2)

这是你需要的东西:)

注意:我添加了classie.js外部库。 这会在点击时添加焦点类。并写这个css

#input input:focus {
outline: none;
left: 0px;
transition: left 0.3s ease ;
}

显示文本框。

希望这对你有所帮助。

&#13;
&#13;
(function(window){

// get vars
var searchEl = document.querySelector("#input");
var labelEl = document.querySelector("#label");

// register clicks and toggle classes
labelEl.addEventListener("click",function(){
if (classie.has(searchEl,"focus")) {
classie.remove(searchEl,"focus");
classie.remove(labelEl,"active");
} else {
classie.add(searchEl,"focus");
classie.add(labelEl,"active");
}
});

// register clicks outisde search box, and toggle correct classes
document.addEventListener("click",function(e){
var clickedID = e.target.id;
if (clickedID != "search-terms" && clickedID != "search-label") {
if (classie.has(searchEl,"focus")) {
classie.remove(searchEl,"focus");
classie.remove(labelEl,"active");
}
}
});
}(window));
&#13;
#search {
position: absolute;
top: 0;
left: 0;
width: 60px;
height: 60px;
}
#label {
width: 60px;
height: 60px;
position: relative;
z-index: 20;
background:red;
}
#label label {
display: block;
width: 60px;
height: 60px;
background: url("http://png-4.findicons.com/files/icons/1262/amora/256/find.png") 0 0;
background-size: contain;
font-size: 0;
color: rgba(0, 0, 0, 0);
text-indent: -9999px;
cursor: pointer;
}
#label label:hover {
background: url("http://png-4.findicons.com/files/icons/1262/amora/256/find.png") -60px 0;
background-size: contain;
}
#label.active label {
background: url("http://png-4.findicons.com/files/icons/1262/amora/256/find.png") -60px 0;
background-size: contain;
}
#input {
position: absolute;
top: 0;
left: 50px;
width: 450px;
height: 60px;
z-index: 5;
overflow: hidden;
background-color:green;
}
#input input {
display: block;
position: absolute;
top: 0;
left: -450px;
width: 450px;
height: 100%;
margin: 0;
padding: 0 10px;
border: none;
background-color: #23688b;
color: #fff;
font-size: 18px;
backface-visibility: none;
border-radius: 0;
transition: position 0.3s ease ;
}
#input input:focus {
outline: none;
left: 0px;
transition: left 0.3s ease ;
}
#input.focus {
z-index: 20;
}
#input.focus input {

position: left;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/classie/1.0.1/classie.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<form id="search" action="#" method="post">
<div id="label"><label for="search-terms" id="search-label">search</label></div>
<div id="input"><input type="text" name="search-terms" id="search-terms" placeholder="Enter search terms..."></div>
</form>
&#13;
&#13;
&#13;