如何在悬停右侧显示弹出窗口?

时间:2018-10-03 08:15:44

标签: javascript html css ruby-on-rails bootstrap-4

当前,当用户将鼠标悬停在链接或文本上时,我会在顶部显示一个弹出窗口。但是,问题在于弹出窗口无法在屏幕上正确显示。弹出窗口的顶部在浏览器中不可见。我在应用程序中使用twitter-bootstrap。如何使弹出窗口显示在元素的右侧而不是顶部?

function showPopup(element) {
  elements = $(".popuptext")
  $.each(elements, function(i, e) {
    e.classList.remove("show");
  })
  popup_id = $(element).attr("data-popup-id")
  var popup = document.getElementById(popup_id);
  popup.classList.add("show");
}

function hidePopup(element) {
  popup_id = $(element).attr("data-popup-id")
  var popup = document.getElementById(popup_id);
  popup.classList.remove("show");
  popup.classList.add("hide");
}
/* Popup container - can be anything you want */
.popup {
  position: relative;
  display: inline-block;
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* The actual popup */
.popup .popuptext {
  visibility: hidden;
  width: 350px;
  background: #ffffff;
  color: #585858;
  text-align: center;
  border-radius: 6px;
  border: solid 1px black;
  padding: 8px 0;
  position: absolute;
  z-index: 1;
  bottom: 125%;
  /*left: 100px;*/
  margin-left: -80px;
}

/* Popup arrow */
.popup .popuptext::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: #555 transparent transparent transparent;
}

/* Toggle this class - hide and show the popup */
.popup .show {
  visibility: visible;
  -webkit-animation: fadeIn 1s;
  animation: fadeIn 1s;
}

/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="popup">
  <a href="/my-link" data-popup-id="popup-c5e1e2c5" onmouseenter="showPopup(this)">Hover On this text</a>
  <span class="popuptext show" data-popup-id="popup-c5e1e2c5" id="popup-c5e1e2c5" onmouseleave="hidePopup(this)">
        My Whole content
      </span>
</div>

感谢您的帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

如果您想通过HTML解决此问题,则:

<button type="button" class="btn btn-secondary" data-container="body" data-toggle="popover" data-placement="right" data-content="Popup text.">
  Popover on right
</button>

如果您想通过JS解决此问题:

$('#showPopup').popover('right')

您可以了解有关弹出式窗口in the bootstrap docs

的更多信息