【问题标题】:How to show image on another image on click while there are multiple images with same class name using javascript or jquery?当使用javascript或jquery有多个具有相同类名的图像时,如何在单击时在另一个图像上显示图像?
【发布时间】:2019-01-22 16:56:28
【问题描述】:

我有一个带有缩略图的视频列表,这些视频正在使用循环生成并制作一个播放列表。

这里是jquery的代码

 var div = $('#div_thumbnails');
 div.html('');
 $('.cjtp-hp-video').each(function(i, video){ 
     var src = $(video).find('img').attr('src');
     var index = $(video).attr('data-index');
     var title = $(video).find('.cjtp-hp-title').text();
     var thumbnail = '<img class="thumbnail" src="'+src+'" data-index="'+index+'" data-title="'+title+'">';
     div.append(thumbnail);
 });

这个循环正在制作这个 HTML:

<div class="div_thumbnails">
   <img class="thumbnail" src="https://i.ytimg.com/vi/YHLZitYYyIg/mqdefault.jpg" data-index="0" data-title="Vsauce enters The Create Unknown – Vsauce2 po..." style="opacity: 1;outline: white solid 3px;outline-offset: -3px;">
   <img class="thumbnail" src="https://i.ytimg.com/vi/YHLZitYYyIg/mqdefault.jpg" data-index="1" data-title="Vsauce enters The Create Unknown – Vsauce2 po..." style="opacity: 1;outline: white solid 3px;outline-offset: -3px;">
   <img class="thumbnail" src="https://i.ytimg.com/vi/YHLZitYYyIg/mqdefault.jpg" data-index="2" data-title="Vsauce enters The Create Unknown – Vsauce2 po..." style="opacity: 1;outline: white solid 3px;outline-offset: -3px;">
   <img class="thumbnail" src="https://i.ytimg.com/vi/YHLZitYYyIg/mqdefault.jpg" data-index="3" data-title="Vsauce enters The Create Unknown – Vsauce2 po..." style="opacity: 1;outline: white solid 3px;outline-offset: -3px;">
</div>

这是 Css

#div_thumbnails {
    height: 110px;
    border-width: 2px 2px 0px 2px;
    white-space: nowrap;
    position: relative;
    overflow-x: scroll;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
}

我正在使用 jquery 使用此代码在第一次单击图像时更改图像选择样式:

$(document).find('#div_thumbnails').on('click', 'img', function() {
    var css = { 'outline': '', 'outline-offset': '' };

    $('#div_thumbnails').find('img').not(this).css(css).animate({opacity: "0.3"}, 500);

    if (index != $(this).attr('data-index'))
        clicked=0;

    index = $(this).attr('data-index');
    clicked++;
    if (clicked === 1) {
        var title = $(this).attr('data-title');
        $('#span_thumbnails_title').html(title);
    } else if (clicked === 2) {
        core.playVideo(index);
        clicked = 0;
    }
});

现在请告诉我如何使用 jquery 或 javascript 在所选图像上显示播放视频图标图像?

【问题讨论】:

  • 您想准确显示哪个图标?它来自哪里?
  • 我想在存储在我的图像文件夹中的图像上显示 traingle 播放图标。
  • @Armel 你有什么解决办法吗?
  • @Armel 你能帮我解决这个问题吗? stackoverflow.com/questions/54297252/…

标签: javascript jquery html


【解决方案1】:

有很多解决方案。您可以尝试通过添加 div 来更改生成 HTML 的函数,该函数将包装您的缩略图,然后在鼠标进入包装器时添加一个播放图标。

我为您的案例实现了一个 CSS 解决方案:

var $wrappers = $('.thumbnail_wrapper');

$wrappers.on('click', function () {
  $wrappers.removeClass('active');
  $(this).addClass('active');
});
.thumbnail_wrapper {
  position: relative;
}

.thumbnail_wrapper.active::after {
  border-left: 10px solid white;
  border-top: 10px solid transparent;
  border-bottom: 10px solid transparent;
  content: "";
  position: absolute;
  top: 85px;
  left: 155px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="div_thumbnails">
  <div class="thumbnail_wrapper">
    <img class="thumbnail" src="https://i.ytimg.com/vi/YHLZitYYyIg/mqdefault.jpg" data-index="0" data-title="Vsauce enters The Create Unknown – Vsauce2 po..." style="opacity: 1;outline: white solid 3px;outline-offset: -3px;">
  </div>
  <div class="thumbnail_wrapper">
    <img class="thumbnail" src="https://i.ytimg.com/vi/YHLZitYYyIg/mqdefault.jpg" data-index="1" data-title="Vsauce enters The Create Unknown – Vsauce2 po..." style="opacity: 1;outline: white solid 3px;outline-offset: -3px;">
  </div>
  <div class="thumbnail_wrapper">
    <img class="thumbnail" src="https://i.ytimg.com/vi/YHLZitYYyIg/mqdefault.jpg" data-index="2" data-title="Vsauce enters The Create Unknown – Vsauce2 po..." style="opacity: 1;outline: white solid 3px;outline-offset: -3px;">
  </div>
  <div class="thumbnail_wrapper">
    <img class="thumbnail" src="https://i.ytimg.com/vi/YHLZitYYyIg/mqdefault.jpg" data-index="3" data-title="Vsauce enters The Create Unknown – Vsauce2 po..." style="opacity: 1;outline: white solid 3px;outline-offset: -3px;">
  </div>
</div>

【讨论】:

  • 但我希望在第一次点击时播放这个按钮。我该怎么做?
  • @FahadHassanSubzwari 我添加了一小段 JS,这样图标就会在点击时显示出来。
  • 但是我可以从哪里更改播放按钮图像的大小?
  • 在 CSS 中,检查 border 的大小。
  • 你能帮我解决这个[问题] (stackoverflow.com/questions/54297252/…) 吗?
猜你喜欢
  • 1970-01-01
  • 2013-03-15
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-13
  • 2010-11-05
  • 2016-04-11
相关资源
最近更新 更多