2013-08-12 129 views
0

目前我有一个搜索图标,当它悬停在它上面时打开搜索框,当鼠标离开它时关闭搜索栏。但是我不明白如何在运行一次之前运行一次。如何阻止Jquery运行多次直到脚本完成

HTML

<div id="search-container"> 
    <input type="submit" name="ctl00$btnSearch" value="Go" id="ctl00_btnSearch" class="trigger searchbutton search-bg"> 
    <div class="search-bar"> 
     <input name="ctl00$txtSearch" type="text" id="ctl00_txtSearch" class="searchbox" value="Search" onfocus="this.value=''"> 
    </div> 
</div> 

的JavaScript

$(document).ready(function() { 
    $('#search-container .trigger').mouseover(function() { 
     $('section .search-bar input').animate({width:"150px"}, 750, function(){ 
      $(this).find('input').first().focus(); 
     });  
     $('.search-bar').animate({width:"185px"}, 750, function(){ 
      $(this).find('input').first().focus(); 
     }); 
     $("#search-container .trigger").mouseleave(function(){ 
      $("section .search-bar input, .search-bar").animate({width:"0px"}, 750, function(){ 
       $(this).find('input').first().focus(); 
      }); 
     });  
    }); 
}); 

演示:http://jsfiddle.net/fmrfL/

+3

您的演示不会因为工作你有e未指定JQuery库。你还应该在你的问题 – musefan

+1

中包含你的javascript,而你并没有导入Jquery,并且当你想让它出现在搜索框中,并且当你想让它消失时,并不清楚 –

+0

“在再次运行它之前如何让它运行一次”。你的意思是,当页面加载?要么 ?? – TCHdvlp

回答

0

您可以使用下面的代码的函数中:

$(document).ready(function() { 
    $('#search-container .trigger').mouseover(function(e) { 
     $('section .search-bar input').stop(true,true).animate({width:"150px"}, 750, function(){ 
      $(this).find('input').first().focus(); 
     });  
     $('.search-bar').stop(true,true).animate({width:"185px"}, 750, function(){ 
      $(this).find('input').first().focus(); 
     }); 
     }); 
     $("#search-container .trigger").mouseleave(function(e){ 
      $("section .search-bar input, .search-bar").stop(true,true).animate({width:"0px"}, 750,       
       function(){ 
       $(this).find('input').first().focus(); 
      }); 
     }); 
}); 
+0

基本上,jquery stop方法可以做到这一点。检查[Jquery API文档](http://api.jquery.com/stop/) –

相关问题