【问题标题】:Hide popup when mouse moves off鼠标移开时隐藏弹出窗口
【发布时间】:2020-08-05 14:32:30
【问题描述】:

我正在尝试在鼠标悬停时添加一个弹出窗口。每行都有多个 td,并且弹出窗口应该只对第一个有效。只要 mouseout 在同一列中,此方法就有效。也就是说,如果我上下移动鼠标,弹出窗口会按预期出现和消失。但是,如果我将鼠标水平移动到下一个 td,弹出窗口不会消失。我为此创建了一个jsfiddle,但弹出窗口不起作用。控制台说没有定义 javascript 函数,但它在这里工作正常,所以我在 jsfiddle 设置中一定有问题。这是我正在使用的代码。正在使用 Td,因为这是给我的代码。无论鼠标如何移动,任何人都可以看到隐藏弹出窗口需要什么?

已编辑以解决问题。

    <style>
    #pop-description{
      display              : none;
      position             : absolute;
      z-index              : 99999;
      left                 : 0px;
      padding              : 10px;
      background           : #3AB9AE;
      border               : 1px solid #9a9a9a;
      margin               : 0px;
    }
    </style>

    <script>
    $(document).ready(function(){
    function ShowDescription(id) { 
     var position = $('.class-desc-'+id).position();
     var desc = $('#desc-'+id).val();
     $('#pop-description').css('top', position.top);
     $('#pop-description').text(desc);
     //$('#pop-description').toggle();

     $('.class-desc-'+id).mouseenter(function() {
       $('#pop-description').css('display', 'block');

     }).mouseleave(function() {
       $('#pop-description').css('display', 'none');
     });  
    }
    });
    </script>

    <div style="display:relative;"><div id="pop-description" style="display:none;"></div></div>
    <table>
    <tr>
    <td class="class-desc-0" onmouseOver="ShowDescription('0');">title</td>
    <td>Address</td>
    <td>State</td>
    <input type="hidden" name="desc-0" value="first test" id="desc-0">
    </tr>

    <tr>
    <td class="class-desc-1" onmouseOver="ShowDescription('1');">title</td>
    <td>Address</td>
    <td>State</td>
    <input type="hidden" name="desc-1" value="second test" id="desc-1">
    </tr>

    <tr>
    <td class="class-desc-2" onmouseOver="ShowDescription('2');">title</td>
    <td>Address</td>
    <td>State</td>
    <input type="hidden" name="desc-2" value="third test" id="desc-2">
    </tr>
    </table>  
      

【问题讨论】:

    标签: javascript jquery popup onmouseout


    【解决方案1】:

    我觉得你想多了。这就是我要做的。我会使用 jQuery,如下所示。

    1. mouseenter 上触发您需要的操作
    2. mouseleave 发起相反的操作

    $(function() {
    
      $(".toggle").mouseenter(function() {
        // Your code goes below: initiate first action
        $(this).addClass("showOff");
    
      }).mouseleave(function() {
    
        // Your code goes below: Initiate opposite action
        $(".toggle").removeClass("showOff");
    
      });
    
    });
    div {
      cursor: pointer;
      width: 200px;
      height: 200px;
      line-height: 200px;
      text-align: center;
      transition: all 2s;
    }
    
    .showOff {
      width: 200px;
      height: 200px;
      line-height: 200px;
      text-align: center;
      background: orange;
      transition: all 2s;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="toggle">Hove over me</div>

    注意:在您的情况下,您在mouseenter 上显示弹出窗口并在mouseleave 上隐藏它

    【讨论】:

    • 用您提供的鼠标功能替换切换命令解决了问题。谢谢你。 :)
    • 很高兴能帮上忙!
    【解决方案2】:

    为什么不直接使用 hover 呢?喜欢

    class-desc:hover .popup{
       display: block;
    }
    

    【讨论】:

    • 主要是因为行数未知,我需要一个这样的类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-31
    • 2011-06-09
    • 2012-10-12
    • 1970-01-01
    • 2015-12-16
    相关资源
    最近更新 更多