jquery - 在悬停效果上保持 div 打开

标签 jquery html css

我正在考虑使用以下内容:

http://jsfiddle.net/webdevem/4RgTS/

但我需要一种方法来确保当您将鼠标悬停在显示的 div 上时显示的 div 保持打开状态。

代码如下:

$(document).ready(function() {
    $("#toggleSwitch_j").hover(

    function() {
        $("#theBox_3").slideDown(500);
    }, function() {
        $("#theBox_3").slideUp(500);
    });


    $("#StayOpen").hover(
        function() {
            $("#theBox_2").slideDown(500);
        }, function() {
            $("#theBox_2").slideUp(500);
        });
    });


<a href="#" id="toggleSwitch_j">jQuery Hover</a><div id="theBox_3">Peek-a-boo!</div>

#theBox_3, #theBox_2{
    display: none; 
    border: 1px solid #000;
    width: 200px;
    height: 100px;
    background-color: #ddf;
    position: absolute;
}
#toggleSwitch_j, #StayOpen {
 background-color: #cacaca;            
}

最佳答案

这是 demo on Fiddle用文本输入尝试编写和CSS动画(过渡)的CSS修改。

jQuery

// Function fires when hover on the link
$(document).ready(function ($) {
    $(document).on('hover', '.hover-me', function (e) {
        e.preventDefault();
        $(this).closest("div").find(".pop-on-hover").removeClass("closed").addClass("opened");

    });

    // Function fires when click anywhere outside the slided div
    $(document).click(function (event) {
        var clickover = $(event.target);
        var _opened = $(".pop-on-hover").hasClass("pop-on-hover opened");
        if (_opened == true && !clickover.hasClass("hover-me") && !clickover.hasClass("pop-on-hover") && clickover.parents('.pop-on-hover').length == 0) {
            event.preventDefault();
            CloseHovered();
        }
    });
    // Close hovered panel function
    function CloseHovered() {
        $(".pop-on-hover").removeClass("opened").addClass("closed");
    }
});

CSS

  body {
     background-color: #eef;     
  }
  .pop-on-hover {
      border: 1px solid #000;
      width: 200px;
      background-color: #ddf;
      height:0px; 
      overflow:hidden;
      -webkit-transition: all .25s ease-out;
      -moz-transition: all .25s ease-out;
      -ms-transition: all .25s ease-out;
      transition: all .25s ease-out;
  }
  #toggleSwitch_j, #StayOpen {
   background-color: #cacaca;

  }
  .pop-on-hover.closed {
     height:0px; 

  }
  .pop-on-hover.opened {
     height:80px; 
      -webkit-transition: all .25s ease-out;
      -moz-transition: all .25s ease-out;
      -ms-transition: all .25s ease-out;
      transition: all .25s ease-out;
  }

更新

如果您需要打开它,您可能不会使用此部分或以其他方式使用它。

   if (_opened == true && !clickover.hasClass("hover-me") && !clickover.hasClass("pop-on-hover") && clickover.parents('.pop-on-hover').length == 0) {
     event.preventDefault();
     CloseHovered();
   }

关于jquery - 在悬停效果上保持 div 打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36425154/

相关文章:

jQuery 双下拉导航问题

javascript - 我正在将 Bootstrap Timepicker 与 twitter bootstrap 一起使用,但它不会在单击时间图标时显示时间选择器弹出窗口

html - 为什么这些内联 block div 元素之间存在无法解释的间隙?

javascript - Angular 显示 ng-repeat 仅在单击按钮后

java - 将 Freemarker 与 ckEditor(或任何 html 编辑器)一起使用时如何解决引号问题?

php - 重命名 html、php 和 css 文件中的文本

css - WordPress 5.9 editor-style.css 不再工作并添加了覆盖 block 样式的 css 属性

php - 代码未在服务器目录 php 中创建文件

javascript - 按比例增加网站上的每个字体大小

jquery - 更改页面滚动上的事件菜单项?