代码之家  ›  专栏  ›  技术社区  ›  XMen

我需要阻止一个DIV消失,当我在DIV的链接鼠标

  •  2
  • XMen  · 技术社区  · 16 年前

    这是我的完整代码。当我将鼠标移到popupcontact div上时,它会在上面显示divtoshow div,并且它有一个名为rahul的链接,当我将鼠标移到链接上时,它会隐藏div name divtoshow。

    我的div应该在我鼠标离开时隐藏,而不是在我鼠标越过链接时隐藏。

    <!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" dir="ltr">
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js" type="text/javascript"></script>
    </head>
    <body>
        <div id="popupContact"  style="position:absolute;left:100px;top:100px;width:100px;height:50px;background-color:orange;border:1px solid red ;">
        </div>
        <div id="divtoshow" style="display:none;background-color:green; border:1px solid black;width:200px;height:100px;position:absolute;">
        dsfdssd <div><a href="#">rahul</a></div>
        </div>
    </body>
    </html>
    <script  type='text/javascript'>
    $(document).ready(function(){
        var popup_pos=$('#popupContact').offset();
        var timer;
         $("#popupContact").mouseover(function() {
            if(timer) {
                clearTimeout(timer);
                timer = null
               }
            timer = setTimeout(function() {
                console.log($("#VersionSelectField").is(':hidden'));
                if(!$("#VersionSelectField").is(':hidden')){
                    $("#divtoshow").css('position',"absolute"); 
                    $("#divtoshow").css('top',popup_pos.top-20);    
                    $("#divtoshow").css('left',popup_pos.left-20);  
                    $("#divtoshow").fadeIn(300);
                     $("#popupContact").hide();
                }
    
            }, 100);
    
            });
    
         $("#divtoshow").mouseout(function() {
                if(timer) {
                    clearTimeout(timer);
                    timer = null
                   }
                timer = setTimeout(function() {
                    $("#divtoshow").fadeOut("slow");
                     $("#popupContact").show();
    
                }, 1000);
        });
    });
    </script>
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Nick Craver    16 年前

    .mouseout() 这样地:

    $("#divtoshow").mouseout(function() {
    

    .mouseleave() ,如下所示:

    $("#divtoshow").mouseleave(function() {
    

    不会 当进入子元素时激发,如 mouseout 威尔,你不想让它藏起来的时候它就藏起来了。


    另一个代码提示,您至少可以在 #divtoshow 选择器,或者更好的链接并将对象传递给 .css() ,如下所示:

    $("#divtoshow").css({ position: "absolute", 
                          top: popup_pos.top-20, 
                          left: popup_pos.left-20 })
                   .fadeIn(300);
    

    而且,这不是标记的问题,但是如果 #popupContact mouseover 不向孩子开枪就等于 mouseenter .

        2
  •  0
  •   foomip    15 年前

    我发现如果你不这样做的话,有时它在IE的某些版本中就不能正常工作。