【问题标题】:Keep pop up hover content open while hovering over content/body悬停在内容/正文上时保持弹出悬停内容打开
【发布时间】:2020-08-28 22:03:19
【问题描述】:

我正在尝试使用 javascript 和 css 创建自定义悬停。

一切都很好,我可以切换类以显示和隐藏弹出/悬停气泡。 (弹出正文)

但是,当光标悬停在气泡上时,我希望气泡也保持打开状态。 (弹出体)

离开弹出链接后立即关闭,如果我在弹出主体内呈现任何链接,这会使悬停动作无用。请帮忙!

HTML/ERB

<div class="popup-container">
  <span class="popup-link">Partner disclosure</span>
  <span class="popup-body popup-bottom">
    <h3>Partner relationships</h3>

    <p>stuffs in popup body</p>
  </span>
</div>

Javascript

$(document).on('mouseleave click', '.popup-body', function() {
   $(".popup-body").removeClass('show-popup');
})

### if mobile device
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {

  $(document).on('click', '.popup-link', function() {
     var popupContent = $(this).parent('.popup-container').find('.popup-body');
     if (!popupContent.hasClass('show-popup')) {
       popupContent.addClass('show-popup');
     } else {
       popupContent.removeClass('show-popup');
     }
  })

  $(document).on('touchstart', '.page-content', function(e) {
    if ( !e.target.classList.contains(".popup-body") ) {
      $(".popup-body").removeClass('show-popup');
    }
  });

} else { ###desktop

  $(document).on('mouseenter', '.popup-link', function() {
      var popupContent = $(this).parent('.popup-container').find('.popup-body');
      popupContent.addClass('show-popup');
  })

  $(document).on('click', '.page-content', function(e) {
    if ( !e.target.classList.contains(".popup-body") ) {
      $(".popup-body").removeClass('show-popup');
    }
  });
}

CSS

.popup-container {
  position: relative;
  display: inline-block;
  background: none;
  cursor: pointer;
}

.popup-container .popup-body {
  visibility: hidden;
  position: absolute;
  width: 676px;
  background-color: $color-white;
  color: #000;
  text-align: left;
  padding: 30px;
  z-index: 1;
  opacity: 0;
  transition: opacity 0.3s;
  -webkit-box-shadow: 0px 2px 4px 0 rgba(0, 0, 0, 0.5);
  -moz-box-shadow: 0px 2px 4px 0 rgba(0, 0, 0, 0.5);
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.5);
}

.popup-container .show-popup {
  visibility: visible;
  opacity: 1;
  @include susy-breakpoint(0 $breakpoint-tablet-landscape, 8) {
    width: 350px;
  }
}

.popup-bottom {
  top: 135%;
  left: 50%;
  margin-left: -60px;
}

.popup-bottom::after {
  content: "";
  position: absolute;
  display: block;
  bottom: 100%;
  left: 5%;
  margin-left: -5px;
  border-width: 0 10px 8px;
  border-style: solid;
  border-color: #fff transparent;
}

.popup-link {
  color: $color-brand-1;
  text-decoration: none;
  font-weight: bold;
}

.popup-link:hover {
  color: $color-link-hover;
  text-decoration: none;
  font-weight: bold;
}

【问题讨论】:

    标签: javascript html css ruby ruby-on-rails-5


    【解决方案1】:

    我可能会误解你,但如果你只是想让身体离开它的位置时离开,为什么不将 mouseexit 函数添加到 那个 元素呢?这就是我在 sn-p 中所指的内容。

    $(document).on('mouseenter click', '.popup-link', function() {
       var popupContent = $(this).parent('.popup-container').find('.popup-body');
       popupContent.addClass('show-popup');
    })
    
    $(document).on('mouseleave', '.popup-body', function() {
       $(".popup-body").removeClass('show-popup');
    })
    
    $(document).on('click touch', function(e) {
      if ( !e.target.classList.contains(".popup-body") ) {
        $(".popup-body").removeClass('show-popup');
      }
    });
    .popup-container {
      position: relative;
      display: inline-block;
      background: none;
      cursor: pointer;
    }
    
    .popup-container .popup-body {
      visibility: hidden;
      position: absolute;
      width: 676px;
      background-color: $color-white;
      color: #000;
      text-align: left;
      padding: 30px;
      z-index: 1;
      opacity: 0;
      transition: opacity 0.3s;
      -webkit-box-shadow: 0px 2px 4px 0 rgba(0, 0, 0, 0.5);
      -moz-box-shadow: 0px 2px 4px 0 rgba(0, 0, 0, 0.5);
      box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.5);
    }
    
    .popup-container .show-popup {
      visibility: visible;
      opacity: 1;
      @include susy-breakpoint(0 $breakpoint-tablet-landscape, 8) {
        width: 350px;
      }
    }
    
    .popup-bottom {
      top: 135%;
      left: 50%;
      margin-left: -60px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="popup-container">
      <span class="popup-link">Partner disclosure</span>
      <span class="popup-body popup-bottom">
        <h3>Partner relationships</h3>
    
        <p>stuffs in popup body</p>
      </span>
    </div>

    【讨论】:

    • 这取决于你的意思。从技术上讲,您会触发一个 mouseexit 事件,该事件越过身体。你总是可以设置一个超时。预期的用例是什么?我原本以为是身体一直停留在直到鼠标离开身体。您是否也希望它在其他情况下离开?
    • 我现在也附加了对链接元素的点击。您还可以感知是否在主体上单击并删除这样的类。
    • 您必须将点击侦听器附加到正文。这就是事情变得有点棘手的地方。我会做一些研究(这是永远的),我会回复你的。 :)
    • 看看我的代码,看看它是否适合你。我不确定这是否是每次看到处理此问题的 最佳 做法,但它似乎确实按照您的意图工作。我可能会让代码更加 D.R.Y. (不要重复自己)。您可以缓存变量,而不是每次都解析 DOM 来查找它们。 var popupBody = $('.popup-body);
    • 让我看看有没有办法测试它。我几乎肯定点击会转化为浏览器上的触摸事件。我会看看我能做什么。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多