简体   繁体   English

jQuery何时动画 <div> 失去重点

[英]jQuery animate when <div> loses focus

I've got the following code on my site: 我的网站上有以下代码:

<!-- Boat weather widget -->
<div id="boat_weather_tab_container">
    <div id="boat_weather_tab">
        <script type="text/javascript" src="widget.js"></script>
    </div>
    <div id="boat_weather_tab_button">
        <img src="images/blank150.gif">
    </div>
</div>

<script type="text/javascript">
$(document).ready(function(){
    $("#boat_weather_tab_button").click(function(){
        // boat_weather_tab_hidden = 1 on page load
        if (boat_weather_tab_hidden) {
            $("#boat_weather_tab").animate({
                marginTop: "-1px"
            }, 500);
            boat_weather_tab_hidden = 0;
        } else {
            $("#boat_weather_tab").animate({
                marginTop: "-254px"
            }, 500);
            boat_weather_tab_hidden = 1;
        }
    });
});
</script>

Now the client wants #boat_weather_tab to slide back up not just when #boat_weather_tab_button is clicked, but when the user clicks anywhere else on the page, which I understand to be the equivalent of when the parent container div #boat_weather_tab_container loses focus. 现在客户想#boat_weather_tab滑回涨不只是当#boat_weather_tab_button被点击,但是当用户点击其他地方的页面,我的理解是,当父容器DIV相当于上#boat_weather_tab_container失去焦点。

What is the jQuery I would need to accomplish this? 我需要完成什么的jQuery?

Maybe 也许

$(document).click(function(event)
{
 if(boat_weather_tab_hidden==0 && $("#boat_weather_tab").queue()==0 )
 {
   $("#boat_weather_tab").animate({
                marginTop: "-254px"
            }, 500);
            boat_weather_tab_hidden = 1;

 }
});

Not tested, but the idea is the document click will now only hide the boat when it is not currently animating 尚未测试,但想法是文档单击现在将仅在当前未设置动画时隐藏船

You might be able to use the blur event . 您也许可以使用blur事件 It is essentially the act of losing focus. 从本质上讲,这是失去焦点的行为。

$("#boat_weather_tab_container").blur(function() {
    //code to run on blur
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM