【问题标题】:How to code next/previous buttons for a custom "lightbox"如何为自定义“灯箱”编写下一个/上一个按钮
【发布时间】:2014-09-23 18:14:46
【问题描述】:

我正在从头开始创建类似灯箱的功能。到目前为止,我已经让接管元素正常工作并正确显示图像,但现在我想编写下一个/上一个按钮。我还希望按钮循环浏览图库(如果在第一张图片上单击“上一张”,则会加载最后一张图片,反之亦然)。

这是显示我的图像缩略图的 HTML:

<div id="gallery-wrapper">
    <ul>
        <li><a href="/path/to/large/image" class="image"><img src="/path/to/thumbnail" alt="Image" /></a></li>
        <li><a href="/path/to/large/image" class="image"><img src="/path/to/thumbnail" alt="Image" /></a></li>
        <li><a href="/path/to/large/image" class="image"><img src="/path/to/thumbnail" alt="Image" /></a></li>
        <li><a href="/path/to/large/image" class="image"><img src="/path/to/thumbnail" alt="Image" /></a></li>
    </ul>
</div>

点击图片缩略图时,会生成灯箱并通过 Javascript 显示:

$(document).ready(function() {
    $('.image').click(function(e) {
        e.preventDefault();
        $('body').width($('body').width());
        $('body').css('overflow', 'hidden');
        var image_href = $(this).attr("href");
        if ($('#lightbox').length > 0) { 
            $('#content').html('<img src="' + image_href + '" />');
            $('#lightbox').show();
        }
        else { 
            var lightbox =
            '<div id="lightbox">' +
                '<div id="content">' +
                    '<div class="close-gallery">Close<span>X</span></div>'+
                    '<div class="gallery-previous"></div>'+
                    '<img src="'+ image_href +'" />' +
                    '<div class="gallery-next"></div>'+
                '</div>' +
            '</div>';
            $('body').append(lightbox);
        }
    });
    $('body').on('click', '.close-gallery', function () {
        $('body').removeAttr('style');
        $('#lightbox').remove();
    });
});

这是我迄今为止尝试过的。它仅适用于当前所选图像之前或之后的兄弟:

$(document).ready(function($) {
    $('.image').click(function(e) {
        $(this).addClass('active');
        e.preventDefault();
        $('body').width($('body').width());
        $('body').css('overflow', 'hidden');
        var image_href = $(this).attr("href");
        var prevImage = $('.active').parent().prev().find('a').attr("href");
        var nextImage = $('.active').parent().next().find('a').attr("href");
        console.log(prevImage, nextImage);
        if ($('#lightbox').length > 0) { 
            $('#content').html('<img src="' + image_href + '" />');
            $('#lightbox').show();
        }
        else { 
            var lightbox =
            '<div id="lightbox">' +
                '<div id="content">' +
                    '<div class="close-gallery">Close<span>X</span></div>'+
                    '<div class="gallery-previous" data-nav="'+ prevImage +'"></div>'+
                    '<img src="'+ image_href +'" />' +
                    '<div class="gallery-next" data-nav="'+ nextImage +'"></div>'+
                '</div>' +
            '</div>';
            $('body').append(lightbox);
        }
    });
    $('body').on('click', '.gallery-previous', function () {
        var prev = $(this).data('nav');
        console.log(prev);
        $('#content').find('img').attr('src',prev);
    });
    $('body').on('click', '.gallery-next', function () {
        var next = $(this).data('nav');
        console.log(next);
        $('#content').find('img').attr('src',next);
    });
    $('body').on('click', '.close-gallery', function () {
        $('body').removeAttr('style');
        $('#lightbox').remove();
    });
});

我的想法是用下一个/上一个图像链接替换当前的灯箱图像src,但我无法弄清楚我将如何实际实现这一点。任何帮助都非常感谢!

【问题讨论】:

  • 不清楚你在哪里绊倒了。您是否尝试使用.attr()
  • 刚刚用我迄今为止尝试过的内容更新了我的问题。我确实尝试使用.attr()/.data(),但它仅适用于当前所选图像之前和之后的兄弟姐妹,因为nextprev 变量是在初始单击图像时设置的。

标签: javascript jquery image


【解决方案1】:
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js' id='jquery-core-js'></script>

<div id="gallery-wrapper">
    <ul>
        <li><a href="https://mb.cision.com/Public/12091/3216809/95a99f5c35185d28_org.png" class="cision_gallery_image"><img src="https://mb.cision.com/Public/12091/3216809/95a99f5c35185d28_org.png" alt="Image" /></a></li>
        <li><a href="https://mb.cision.com/Public/12091/3216809/887b4860968192bd_org.png" class="cision_gallery_image"><img src="https://mb.cision.com/Public/12091/3216809/887b4860968192bd_org.png" alt="Image" /></a></li>
        <li><a href="http://165.22.182.59/cision/wp-content/uploads/2021/11/3D-Visualisering.jpg" class="cision_gallery_image"><img src="http://165.22.182.59/cision/wp-content/uploads/2021/11/3D-Visualisering.jpg" alt="Image" /></a></li>
        <li><a href="http://165.22.182.59/cision/wp-content/uploads/2021/11/S-Granstorp-1.jpg" class="cision_gallery_image"><img src="http://165.22.182.59/cision/wp-content/uploads/2021/11/S-Granstorp-1.jpg" alt="Image" /></a></li>
    </ul>
</div>

<script type="text/javascript">
jQuery(document).ready(function($) {
    window.onkeydown = function( event ) {
      if ( event.keyCode == 27 ) {
        jQuery('body').removeAttr('style');
        jQuery('#cision_lightbox_cs').remove();
      }
    };
    jQuery('.cision_gallery_image').click(function(e) {
        jQuery('.cision_gallery_image').removeClass("cision_active")
        jQuery(this).addClass('cision_active');
        e.preventDefault();
        jQuery('body').width(jQuery('body').width());
        jQuery('body').css('overflow', 'hidden');
        var image_href = jQuery(this).attr("href");
        var prevImage = jQuery('.cision_gallery_image.cision_active').parent().prev().find('a').attr("href");
        var nextImage = jQuery('.cision_gallery_image.cision_active').parent().next().find('a').attr("href");
        console.log(prevImage, nextImage);
        if (jQuery('#cision_lightbox_cs').length > 0) { 
            jQuery('#cision_content').html('<img src="' + image_href + '" />');
            jQuery('#cision_lightbox_cs').show();
        }
        else { 
            var cision_lightbox_cs =
            '<div id="cision_lightbox_cs" style="display: block;">' +
                '<div id="cision_content">' +
                    '<div class="close-gallery">Close<span>X</span></div>'+
                    '<div class="gallery-previous""><</div>'+
                    '<img src="'+ image_href +'" />' +
                    '<div class="gallery-next">></div>'+
                '</div>' +
            '</div>';
            jQuery('body').append(cision_lightbox_cs);
        }
    });

    jQuery('body').on('click', '.gallery-previous', function () {
        var prev = jQuery('.cision_gallery_image.cision_active').parent().prev().find('a').attr("href");
        if (prev) {

        jQuery('.cision_gallery_image.cision_active').parent().prev().find('a').addClass("cision_active");
        jQuery('.cision_gallery_image.cision_active').parent().next().find('a').removeClass("cision_active");
        console.log(prev);
        jQuery('#cision_content').find('img').attr('src',prev);    

        }
    });
    
    jQuery('body').on('click', '.gallery-next', function () {
        var next = jQuery('.cision_gallery_image.cision_active').parent().next().find('a').attr("href");

        if (next) {
        jQuery('.cision_gallery_image.cision_active').parent().next().find('a').addClass("cision_active");
        jQuery('.cision_gallery_image.cision_active').parent().prev().find('a').removeClass("cision_active");

        console.log(next);
        jQuery('#cision_content').find('img').attr('src',next);

        }
    });

    jQuery('body').on('click', '.close-gallery', function () {
        jQuery('body').removeAttr('style');
        jQuery('#cision_lightbox_cs').remove();
    });
});
</script>

<style type="text/css">
div#gallery-wrapper {}

div#gallery-wrapper ul li {
    float: left;
    width: 25%;
    box-sizing: border-box;
    padding: 20px;
}

div#gallery-wrapper ul li a.cision_gallery_image {
    width: 100%;
}

div#gallery-wrapper ul li a.cision_gallery_image img {
    width: 100%;
}

div#cision_lightbox_cs {
    display: none;
    position: fixed;
    width: 100%;
    height: 100vh;
    z-index: 9999;
    overflow-x: scroll;
    background-color: rgb(124 121 121 / 50%);
}

div#cision_lightbox_cs div#cision_content {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 60%;
    height: 90vh;
    transform: translateX(-50%) translateY(-50%);
    padding-bottom: 50px;
    padding-top: 50px;
}

div#cision_lightbox_cs div#cision_content img {
    width: 100%;
    padding-bottom: 30px;
}
</style>enter code here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    相关资源
    最近更新 更多