【问题标题】:popover hover not triggering on dynamic images弹出悬停未触发动态图像
【发布时间】:2022-01-12 09:24:52
【问题描述】:

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

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

function appendImg() {
  $('.infoBar').append('<div class="imgWrap"><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>

【问题讨论】:

    标签: javascript jquery bootstrap-4


    【解决方案1】:

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

    先将事件监听器包裹在函数内部,然后在append之后调用函数

    function appendImg() {
      $('.infoBar').append('<div class="imgWrap"><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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      相关资源
      最近更新 更多