使用jQuery仅在元素列表中定位元素

时间:2017-02-11 15:59:21

标签: javascript jquery html css css3

我要做的是只选择,在这种情况下悬停,元素被视频替换。现在,当其中一个元素被悬停时,所有共享相同类的元素都会被替换。

这是codepen

我已经阅读了关于使用$(this)的内容,但我无法弄清楚如何在这里实现它。

知道怎么做到这一点?谢谢。

此处代码:

html:

<div class="main-parent">
  <div class="img-container">
  <div class="video_overlays">
    <a href="http://dev.mintv.rs/sta-je-i-kako-se-pravi-zanatsko-pivo" target="_blank">
      <img src="http://dev.mintv.rs/wp-content/uploads/2017/02/1a-1.jpg" alt="Thumb" class="thumb_image" />
      <video class="thumbnail_player" src="http://dev.mintv.rs/wp-content/uploads/2017/02/Kabinet.mp4" type="video/mp4" autoplay loop></video>
    </a>
  </div>
  <span class="duration">03:13</span>
</div>
<div class="desc"><p>Title</p></div>
</div>

<div class="main-parent">
  <div class="img-container">
  <div class="video_overlays">
    <a href="http://dev.mintv.rs/sta-je-i-kako-se-pravi-zanatsko-pivo" target="_blank">
      <img src="http://dev.mintv.rs/wp-content/uploads/2017/02/1a-1.jpg" alt="Thumb" class="thumb_image" />
      <video class="thumbnail_player" src="http://dev.mintv.rs/wp-content/uploads/2017/02/Kabinet.mp4" type="video/mp4" autoplay loop></video>
    </a>
  </div>
  <span class="duration">03:13</span>
</div>
<div class="desc"><p>Title</p></div>
</div>

<div class="main-parent">
  <div class="img-container">
  <div class="video_overlays">
    <a href="http://dev.mintv.rs/sta-je-i-kako-se-pravi-zanatsko-pivo" target="_blank">
      <img src="http://dev.mintv.rs/wp-content/uploads/2017/02/1a-1.jpg" alt="Thumb" class="thumb_image" />
      <video class="thumbnail_player" src="http://dev.mintv.rs/wp-content/uploads/2017/02/Kabinet.mp4" type="video/mp4" autoplay loop></video>
    </a>
  </div>
  <span class="duration">03:13</span>
</div>
<div class="desc"><p>Title</p></div>
</div>

的CSS:

.main-parent {
  width: 360px;
}

.img-container {
  height: 202px;
  width: 360px;
  position: relative;
}

.video_overlays {
  position: absolute;
  width: 360px;
  min-height: 100%;
  background-color: rgba(15, 15, 15, 0);
  z-index: 300000;
}

.video_overlays video {
  display: none;
}

.duration {
  z-index: 300001;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  position: absolute;
  right: 0;
  bottom: 0;
  padding: 0px 6px;
  color: #fff;
  font-size: 12px;
  background-color: rgba(0, 0, 0, 0.66);
  line-height: 24px;
}

.desc {
  text-align: center;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}

js:

jQuery('.img-container a').on({
  mouseenter: function() {
    jQuery('.thumbnail_player').css("display", "block");
    jQuery('.thumbnail_player').attr("autoplay", "true");
    jQuery('.thumbnail_player').attr("loop", "true");
    jQuery('.thumb_image').css("display", "none");
  },

  mouseleave: function() {
    jQuery('.thumbnail_player').css("display", "none");
    jQuery('.thumbnail_player').attr("autoplay", "false");
    jQuery('.thumbnail_player').attr("loop", "false");
    jQuery('.thumb_image').css("display", "block");
  }
});

2 个答案:

答案 0 :(得分:1)

jQuery('.img-container a').on({
  mouseenter: function() {
    var $element = $(this).find('.thumbnail_player');
    var $image = $(this).find('.thumb_image');
    $element
      .css("display", "block")
      .attr("autoplay", "true")
      .attr("loop", "true");
    $image.css("display", "none");
  },

  mouseleave: function() {
    var $element = $(this).find('.thumbnail_player');
    var $image = $(this).find('.thumb_image');
    $element
      .css("display", "none")
      .attr("autoplay", "false")
      .attr("loop", "false");
    $image.css("display", "block");
  }
});

答案 1 :(得分:0)

试试这个:

jQuery('.img-container a').on({
  mouseenter: function() {
    jQuery('.thumbnail_player').css("display", "block");
    jQuery('.thumbnail_player').attr("autoplay", "true");
    jQuery('.thumbnail_player').attr("loop", "true");
    jQuery('.thumb_image',this).css("display", "none");
  },

  mouseleave: function() {
    jQuery('.thumbnail_player').css("display", "none");
    jQuery('.thumbnail_player').attr("autoplay", "false");
    jQuery('.thumbnail_player').attr("loop", "false");
    jQuery('.thumb_image').css("display", "block");
  }
});