jQuery的mouseover显示隐藏的div和显示div,如果只有鼠标仍然在div上 [英] jQuery mouseover to show hidden div and to show div if only mouse still over the div

查看:147
本文介绍了jQuery的mouseover显示隐藏的div和显示div,如果只有鼠标仍然在div上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的mouseover和mouseout功能有问题。当我将鼠标悬停在一个链接上时,它会显示一个隐藏的< div> ,当鼠标悬停在div上时,它会隐藏div。问题是,如果我将鼠标悬停在一个链接上,那么我将鼠标移动到其他不在div上的其他位置,div不会消失。



如果我使用链接的mouseout事件设置div的可见性,那么我将无法将鼠标悬停在div上。



这是我的HTML:

 <!DOCTYPE html PUBLIC -  // W3C / / DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
< html xmlns =http://www.w3.org/1999/xhtml>

< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title>
无标题文件
< / title>
< script type =text / javascriptsrc =http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js>
< / script>
< script type =text / javascript>
$(document).ready(function(){
$(#show_div)。mouseover(function(){
$(#hello).css('visibility' ,'visible');
});

$(#hello)。 ('#hello)。cout('visibility','visible');
});
$(#hello ,'hidden');
});
});
< / script>
< / head>

< body>
< br />
< br />
< br />
< br />
< a id =show_divhref =#>连结文字< / a>
< div id =hellostyle =visibility:hidden;>
< ul>
< li>
咖啡
< / li>
< li>
Tea
< / li>
< li>
牛奶
< / li>
< / ul>
< / div>
< br />
< br />
< / body>
< / html>


解决方案

我使用setTimeout函数来更改css属性。将setTimeout的时间间隔设置为〜333-500毫秒,并为Div设置鼠标悬停以清除超时。然后,在div本身的mouseout上,再次设置计时器:)

示例/答案:

  //用于隐藏div的计时器
var hideTimer;

//将鼠标上的DIV显示在
$(#show_div)。mouseover(function(){
//忘记定时器中的任何隐藏事件
clearTimeout(hideTimer);
$(#hello).css('visibility','visible');
});
$ b $(#hello)。mouseover(function(){
clearTimeout(hideTimer);
$(#hello).css('visibility','可见');
});

//设置一个计时器来隐藏DIV
$(#show_div)。mouseout(function(){
hideTimer = setTimeout(hideHello,333);
});
$ b $(#hello)。mouseout(function(){
hideTimer = setTimeout(hideHello,333);
});

//隐藏DIV
函数hideHello(){
$(#hello)。css('visibility','hidden');
}


I have a problem with my mouseover and mouseout functions. When I mouseover a link, it shows a hidden <div>, and when I mouseout of the div it hides the div. The problem is that if I mouseover a link, then I move mouse somewhere else which is not over the div, the div won't go away.

If I use the mouseout event of the link to set the visibility of the div, then I won't be able to hover on the div.

Here's my HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>
            Untitled Document
        </title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js">
        </script>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#show_div").mouseover(function() {
                    $("#hello").css('visibility', 'visible');
                });

                $("#hello").mouseover(function() {
                    $("#hello").css('visibility', 'visible');
                });
                $("#hello").mouseout(function() {
                    $("#hello").css('visibility', 'hidden');
                });
            });
        </script>
    </head>

    <body>
        <br/>
        <br/>
        <br/>
        <br/>
        <a id="show_div" href="#">Link text</a>
        <div id="hello" style="visibility:hidden;">
            <ul>
                <li>
                    Coffee
                </li>
                <li>
                    Tea
                </li>
                <li>
                    Milk
                </li>
            </ul>
        </div>
        <br/>
        <br/>
    </body>    
</html>

解决方案

I use a setTimeout function to change the css property. Set the interval of the setTimeout to ~333-500 milliseconds, and set the mouseover for the Div to clear the timeout. Then, on the mouseout of the div itself, set the timer again :)

Example/Answer:

// timer for hiding the div
var hideTimer;

// show the DIV on mouse over
$("#show_div").mouseover(function() {
    // forget any hiding events in timer
    clearTimeout( hideTimer );
    $("#hello").css('visibility', 'visible');
});

$("#hello").mouseover(function() {
    clearTimeout( hideTimer );
    $("#hello").css('visibility', 'visible');
});

// set a timer to hide the DIV
$("#show_div").mouseout(function() {
    hideTimer = setTimeout( hideHello, 333 );
});

$("#hello").mouseout(function() {
    hideTimer = setTimeout( hideHello, 333 );
});

// hides the DIV
function hideHello() {
    $("#hello").css('visibility', 'hidden');
}

这篇关于jQuery的mouseover显示隐藏的div和显示div,如果只有鼠标仍然在div上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆