拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 弹出悬停未触发动态影像

弹出悬停未触发动态影像

白鹭 - 2022-02-10 1984 0 0

我有一个包含多个影像的 div,每个影像都有一个带有内容的弹出框,它作业正常。我遇到的问题是我想通过悬停触发影像弹出框,而我现在使用的方法适用于静态影像,但是当我将影像动态添加到主 div 并悬停在该影像上时,弹出框不会扳机。如何调整我的代码以使其也支持任何动态添加的元素?任何帮助表示赞赏。提前致谢。

要复制单击附加按钮并滚动到最右侧并将鼠标悬停在新添加的影像上,该影像不会在悬停时触发弹出框,但其他静态影像会触发。

function appendImg() {
  $('.infoBar').append('<div ><img src="https://source.unsplash.com/user/c_v_r/100x100"></div>')
}

var popOverSettings2 = {
  selector: '.infoBar .imgWrap',
  container: 'body',
  html: true,
  trigger: "manual",
  placement: 'top',
  sanitize: false,
  animation: false,
  content: function() {
    setTimeout(() => {
      $('.popover').css({
        'width': '20%',
        'height': '20%',
        'overflow': 'auto'
      })
    })
    $('.infoBarPopoverContent').append('<p>Popover stuff...</p>')
    return $('.infoBarPopoverContent').html();
  }
}

$(function() {
  $('.infoBar .imgWrap').popover(popOverSettings2)
    .on("mouseenter", function() {
      var _this = this;
      $(this).popover("show");
      $(".popover").on("mouseleave", function() {
        $(_this).popover('hide');
      });
    }).on("mouseleave", function() {
      var _this = this;
      if (!$(".popover:hover").length) {
        $('.popover').popover('hide');
      }
    });
});
button {
  position: absolute;
  top: 0%;
  left: 0%;
}

.infoBar {
  display: flex;
  flex-direction: row;
  position: absolute;
  top: 30%;
  max-width: 95%;
  height: 160px;
  margin: auto;
  column-gap: 25px;
  background-color: green;
  overflow-x: auto;
}

.infoBar .imgWrap {
  height: 100%;
  cursor: pointer;
}

.infoBar .imgWrap img {
  height: 100%;
  cursor: pointer;
}

.infoBarPopoverContent {
  display: none;
}

.popover .popover-body {
  overflow-x: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG 2QOK9T ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg OMhuP IlRH9sENBO0LRn5q 8nbTov4 1p" crossorigin="anonymous"></script>

<button onclick='appendImg()'>Click to append img</button>

<div class="infoBar" id="infoBar">
  <div class="imgWrap"><img src="https://picsum.photos/200/300"></div>
  <div class="imgWrap"><img src="https://picsum.photos/200/300"></div>
  <div class="imgWrap"><img src="https://picsum.photos/200/300"></div>
  <div class="imgWrap"><img src="https://picsum.photos/200/300"></div>
  <div class="imgWrap"><img src="https://picsum.photos/200/300"></div>
</div>

<div class="infoBarPopoverContent"></div>

uj5u.com热心网友回复:

根据我从您的代码中了解到的是,您必须像这样在动态附加之后附加事件侦听器

先将事件监听器包裹在函式内部,然后在append之后呼叫函式

function appendImg() {
  $('.infoBar').append('<div ><img 
    src="https://source.unsplash.com/user/c_v_r/100x100"></div>')
      addEvent();
 }

var popOverSettings2 = {
  selector: '.infoBar .imgWrap',
  container: 'body',
  html: true,
  trigger: "manual",
  placement: 'top',
  sanitize: false,
  animation: false,
   content: function() {
    setTimeout(() => {
  $('.popover').css({
    'width': '20%',
    'height': '20%',
    'overflow': 'auto'
  })
})
$('.infoBarPopoverContent').append('<p>Popover stuff...</p>')
return $('.infoBarPopoverContent').html();
   }
}
function addEvent(){
$(function() {
  $('.infoBar .imgWrap').popover(popOverSettings2)
    .on("mouseenter", function() {
      var _this = this;
      $(this).popover("show");
      $(".popover").on("mouseleave", function() {
        $(_this).popover('hide');
      });
    }).on("mouseleave", function() {
      var _this = this;
      if (!$(".popover:hover").length) {
        $('.popover').popover('hide');
      }
    });
});
}
addEvent()

https://jsbin.com/rumabifiqo/edit?output

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *