javascript - 如何禁用重复图像的显示?

标签 javascript jquery html css

这是一个脚本,用于收集从 wordpress 管理面板上传到页面的所有图像并将它们放入 slider 中。

<div id="slider-box">
  <div id="current">

  </div>
  <div id="all-photos">

  </div>
</div>

Javascript

function setCurrent(first) {
    $('#current').html(first);
}

$(document).ready(function () {
    // set images to preview and remove big images
    $('#content').find('img').each(function () {

        var photoSrc = $(this).attr('src'),
            photoImg = '<img src="' + photoSrc + '">';

        $('#all-photos').append('<div class="photo-template">' + photoImg + '</div>');
        $(this).remove();
    }); // end each


    // set first image preview to current box 
    var firstPhoto = $('#all-photos').find('div.photo-template:first').html()
    setCurrent(firstPhoto);

    // click handler
    $('div.photo-template').on('click', function () {
        var selected = $(this).html();
        setCurrent(selected);
    });
});

效果很好,当我向页面添加几张照片时(例如 http://beardhouse.com.ua/knizhka-divan/millenium ),但是当我只添加一张照片时 - 它会复制到带有小图像的框中(例如 http://beardhouse.com.ua/beskarkasnue-mjagkie-divany/lado ) 如何在页面上只有一张图片时禁止显示重复图片?

最佳答案

尝试将此添加到您的 Javascript 中,它应该会在底部找到图像的数量,然后如果发现多于 1 个,则会移除容器。

if($('#all-photos .photo-template img').length < 2) {
    $('#all-photos').hide();
}

希望这对您有所帮助。

关于javascript - 如何禁用重复图像的显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22750093/

相关文章:

javascript - 更正菜单悬停显示

javascript - Angular 服务和 HttpClient 类型不存在

javascript - 分析JS语法

javascript - Mvc Kendo 网格 - 不显示从 Controller 返回的任何数据

javascript - 需要对显示内容的 ajax 数据结果加载效果

javascript - 无法使用 Javascript 更改 box-ordinal-group CSS 属性

javascript - @reach/router 嵌套 protected 路由

jquery - IE 的问题(jQuery 代码)

html - 使用 Ruby on Rails 捕获音频标签的加载源

javascript - 慢慢停止随机化