【问题标题】:My css transition searchbox isn't working我的 CSS 转换搜索框不起作用
【发布时间】:2015-12-14 14:05:31
【问题描述】:

我希望我的搜索输入在我单击按钮后显示,在转换中,但它似乎对我不起作用......有什么帮助吗? 这是我的代码

这是我的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>

【问题讨论】:

    标签: jquery html css css-transitions


    【解决方案1】:

    这是你需要的:)

    注意:我已经添加了classie.js 外部库。 这会在点击时为输入添加一个焦点类。并编写这个 css

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

    显示文本框。

    希望这对您有所帮助。

    (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));
    #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;
    }
    <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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多