【问题标题】:Adding Captions to Bootstrap Responsive Modal Image Gallery向 Bootstrap 响应式模态图像库添加标题
【发布时间】:2018-08-12 11:01:43
【问题描述】:

我通过引导程序有一个响应式模态图像库,而且我是编码初学者。在我尝试添加字幕之前,我的图像可以成功地前后滑动。

我已经在我的 javascript 中为每张图片添加了悬停字幕:

  $(".popup").wrap('<div class="alt-wrap"/>')

  $('.modal-body>caption').remove();

   $(".popup").each(function() {
   $(this).after('<p class="alt">' + $(this).attr('alt') + '</p>');

})

现在,每当我点击我的模态图像时,悬停标题都可以正常工作,但一旦我点击下一个/上一个,旧图像仍然存在。

当我通过模态框单击下一个/上一个时,如何删除旧的图像/标题?

我的 js 的其余部分如下所示:

 var imagesList = $("div#imagesList");
    var imagesListLength = imagesList.children().length;

    var showImageGallery = function(imageNumber){

        $('.modal-body>img').remove();

        $('#imageModal').modal('show');


        var image = imagesList.children()[imageNumber];
        $(image).clone().appendTo('.modal-body');

        $("#currentImageNumber").text(imageNumber);

    }

    var nextImage = function(){

        $('.modal-body>img').remove();

        var nextNum = parseInt($("#currentImageNumber").text());
        if(nextNum == imagesListLength-1){ nextNum = -1;}
        $("#currentImageNumber").text(nextNum+1);

        var image = imagesList.children()[nextNum+1];
        $(image).clone().appendTo('.modal-body');

    }


    var previousImage = function(){

        $('.modal-body>img').remove();

        var previousNum = parseInt($("#currentImageNumber").text());
        if(previousNum == 0){ previousNum = imagesListLength;}
        $("#currentImageNumber").text(previousNum-1);

        var image = imagesList.children()[previousNum-1];
        $(image).clone().appendTo('.modal-body');


    }

$(window).keydown(function(event) {
  if (event.key === 'ArrowLeft') {
    previousImage();
  } 
  if (event.key === 'ArrowRight') {
    nextImage();
  }
  if (event.key === 'Escape') {
    $('#close-modal').trigger('click')
  }
})

【问题讨论】:

  • 在你的 nextImage 函数中添加 console.log(nextNum);,就在 nextNume 的下方,并在此处通过控制台的输出。
  • 当我单击新图像时,我仍然得到一连串旧图像。我点击的新图片会放在我之前点击过的其他图片的下方。在我添加字幕脚本之前,我的图像手动滑过很好,这让我觉得我的字幕脚本不完整。

标签: javascript jquery bootstrap-modal captions responsive-slides


【解决方案1】:

我只需要补充:

 $('.modal-body>.alt-wrap').remove();

var showImageGallery = function(imageNumber){

  $('.modal-body>.alt-wrap').remove();

到我的 .popup、nextImage 和 previousImage 函数

【讨论】:

    猜你喜欢
    • 2016-10-09
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 2014-07-21
    • 2021-10-07
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    相关资源
    最近更新 更多