【问题标题】:CSS/Javascript Mouseover Popup boxCSS/Javascript 鼠标悬停弹出框
【发布时间】:2013-03-05 16:18:35
【问题描述】:

我有一个带有 javascript/css 内容框的表格单元格,鼠标悬停时会弹出。

页面上有 20 个单元格。一切正常,当您将鼠标悬停在产品链接上时,您会看到内容框。但是,我想在内容框中放置一个链接,用户可以选择点击该链接。因此,弹出框必须保持足够长的时间让用户鼠标悬停点击链接。

真的,我希望 OnMouseOver 保持打开状态,直到一两秒过去和/或用户 OnMouseOver 的另一个单元格。

我遇到的问题是弹出框没有保持打开状态(由于 OnMouseOut)来单击链接。如果我关闭 OnMouseOut(我尝试过),那么所有弹出框都会保持打开状态,所以这也不起作用。

我的 CSS 如下所示:

<style type="text/css" title="">
    .NameHighlights         {position:relative; }
    .NameHighlights div     {display: none;}
    .NameHighlightsHover    {position:relative;}
    .NameHighlightsHover div {display:block;position:absolute;width: 15em;top:1.3em;*top:20px;left:70px;z-index:1000;}
</style>

还有html:

<td>
    <span class="NameHighlights" onMouseOver="javascript:this.className='NameHighlightsHover'" onMouseOut="javascript:this.className='NameHighlights'">
    <a href="product link is here">Product 1</a>
           <div>
            # of Votes:  123<br>
            % Liked<br>
            <a href="product review link>See User reviews</a>
            </div>
    </span>
</td>

那么,我怎样才能让弹出框保持打开足够长的时间来点击链接,同时在另一个内容框被激活时让它消失?

提前致谢。

【问题讨论】:

    标签: javascript html css mouseover


    【解决方案1】:

    如果有人正在寻找已接受答案的 jQuery 版本:

    var t;
    $(function(){
                $('span.NameHighlights').mouseover(
    
                        function(e){
                            hideAll();
                            clearTimeout(t);
                            $(this).attr('class', 'NameHighlightsHover');
    
                        }
                ).mouseout(
    
                        function(e){
    
                            t = setTimeout(function() {
                            //$(this).attr('class', 'NameHighlights');
                            hideAll();
                            }, 300);
    
                        }
                );
            });
    
    function hideAll() {
        $('span.NameHighlightsHover').each(function(index) {
                console.log('insde hideAll');
                    $(this).attr('class', 'NameHighlights');
                })
    };
    

    jsFiddle

    【讨论】:

      【解决方案2】:

      您必须为此任务改进 HTML 标记,需要摆脱内联事件处理程序:

      <span class="NameHighlights">
          <a href="product link is here">Product 1</a>
          <div>
              # of Votes:  123<br>
              % Liked<br>
              <a href="product review link">See User reviews</a>
          </div>
      </span>
      

      然后你必须将你的事件绑定到所有.NameHighlights spans:

      var span = document.querySelectorAll('.NameHighlights');
      for (var i = span.length; i--;) {
          (function () {
              var t;
              span[i].onmouseover = function () {
                  hideAll();
                  clearTimeout(t);
                  this.className = 'NameHighlightsHover';
              };
              span[i].onmouseout = function () {
                  var self = this;
                  t = setTimeout(function () {
                      self.className = 'NameHighlights';
                  }, 300);
              };
          })();
      }
      

      http://jsfiddle.net/3wyHJ/

      所以想法是使用setTimeout方法。

      注意:我使用了querySelectorAll,IE7 不支持,如果需要支持,可以使用getElementsByClassName 方法的任何实现。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-11
        • 1970-01-01
        • 1970-01-01
        • 2014-11-04
        • 1970-01-01
        • 1970-01-01
        • 2017-10-14
        • 1970-01-01
        相关资源
        最近更新 更多