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

如何使嵌入控件与jquery切换?

  •  0
  • helle  · 技术社区  · 16 年前

    我有一个图像。 如果鼠标悬停,下一个图像和上一个图像的控件将显示在图像上(通过CSS移动到那里)。如果鼠标悬停控件的图像,则外壳将消失。

    问题是,如果鼠标悬停在某个控件上,则鼠标悬停图像,控件消失:-(

    我该怎么办?我尝试过类似的方法,但它不起作用: var控制=假;

        $('.detail_img').bind('mouseover', function(){
            $('.gal_control').fadeIn(200);
        });
    
        $('.detail_img').bind('mouseout', function(){
    
            if(!control) $('.gal_control').fadeOut(400);
        });
        $('.gal_control').bind('mouseover', function(){
           control = true;
        });
        $('.gal_control').bind('mouseout', function(){
           control = false;
        });
    

    谢谢你帮我

    HTML…对于所有没有幻想的人:—)

    <div class="rollover_left gal_control"></div>
    <div class="rollover_right gal_control"></div>
    <img class="detail_img" id="detail_img" src...>
    

    更新

    var control = false;
    
    
        $('.detail_img').bind('mouseover', function(){
            control = true;
            $('.gal_control').fadeIn(200);
    
        });
    
        $('.detail_img').bind('mouseout', function(){
            control = false;
        });
        $('body').bind('mouseover', function(){
           if(!control)  $('.gal_control').fadeOut(400);
        });
    
        $('.gal_control').bind('mouseover', function(){
           control = true;
        });
        $('.gal_control').bind('mouseout', function(){
           control = false;
        });
    

    我的意思是,现在就这样了,但有更好的方法吗?我能优化这些线条吗?

    1 回复  |  直到 16 年前
        1
  •  0
  •   arnemart    16 年前

    我将尝试以下一种或两种方法:

    1. 将图像和控件包装在 <div> ,并将鼠标事件附加到DIV而不是图像
    2. 尝试使用 mouseenter 和 mouseleave 事件而不是 mouseover / mouseout