使用jquery为按钮设置动画

时间:2013-04-26 19:02:24

标签: jquery button popup jquery-animate

有人可以帮助我在jquery或javascript的帮助下为我的搜索按钮设置动画。当用户将我的搜索按钮悬停时,我希望搜索框出现(并在用户搜索某些内容时保持可见),但每当鼠标远离搜索按钮或搜索框,这个小盒子应该消失。

<div class = "searchBox">
    <p id = "paragraph"><input type = "text" name = "serachBar"/>
    <input type = "button" value = "szukaj" name = "search"/>
    </p>
    <div id = "searchButton"><a href = "#">Search</a></div>
</div>

CSS

body{
background-color:rgb(138,187,129);

#searchButton{
position:fixed;
border:1px solid rgb(255,255,255);
border-top:none;
width:80px;
height:30px;
top:0px;
right:60px;
border-radius:0px 0px 3px 3px;
background:rgb(30,114,41);
}
#searchButton :hover{
background:rgba(0,0,0,0.3);
}
#searchButton a{
text-decoration:none;
font-size:20px;
font-family:"Palatino Linotype", "Book Antiqua", Palatino, serif;
text-align:center;
color:white;
padding:0;
margin:0;
display:block;
height:100%;
width:100%;
}

.searchBox p{
position:fixed;
border-bottom:1px solid rgba(255,255,255,0.3);
border-radius:2px;
box-shadow:0 1px 2px 2px #1F0000;
-moz-box-shadow:0 1px 2px 2px #1F0000;
-webkit-box-shadow:0 1px 2px 2px #1F0000;
border-top:none;
background:rgba(0,0,0,0.2);
width:200px;
height:50px;
top:0;
right:0;
margin:0;
visibility:hidden;
font-size:15px;
font-family:"Palatino Linotype", "Book Antiqua", Palatino, serif;
color:white;
}

2 个答案:

答案 0 :(得分:1)

真的很快回答。你可能需要编辑这个以满足你的需要,但我相信你所追求的那种jquery如下:

$(document).ready(function(){
    $('#submit, #searchField').on('mouseenter', function(){
        $('#searchField').fadeIn(400);    
    }); 
    $('#submit, #searchField').on('mouseleave', function(){
        $('#searchField').fadeOut(400);    
    }); 
});

HTML:

<form>
    <input type="text" name="searchField" id="searchField"/>
    <input type="submit" id="submit" value="submit"/>
</form>

这里也是fiddle

答案 1 :(得分:0)

所以这是一种方法。只需在搜索框和搜索按钮外设置一个div。然后设置mouseenter和mouseleave事件。您还需要检查是否输入了任何文本。这是一个小提琴:http://jsfiddle.net/bgs7D/

 var $divContainer = $('#divContainer'),
 $txtSearch = $('#txtSearch'),
 $btnSearch = $('#btnSearch');

$txtSearch.hide();

$divContainer.on('mouseenter',function(){
    $txtSearch.show();
});

$divContainer.on('mouseleave',function(){
    if($txtSearch.val()==''){
        $txtSearch.hide();
    }
});

和html:

<div id= "divContainer"> 
<input id="txtSearch" type ="text" ></input>
<input id="btnSearch" type = "button" value="Search" ></input> 
</div>