【问题标题】:How to insert array of images into swipebox gallery?如何将图像数组插入到 swipebox 库中?
【发布时间】:2015-05-29 17:23:08
【问题描述】:

这是我的html代码:

    <div class="profile-gallery">
        <div class="profile-gallery_top js-bigImg">
            <a href="img/bigImg1.jpg" class="swipebox">
                <img class="profile-bigImage" src="img/bigImg.jpg" alt=""/>
            </a>
        </div>
        <ul class="profile-thumbs">
            <li><img src="img/imgThmubs1.jpg" data-bigimage="img/bigImg1.jpg" data-original="img/origImg1.jpg" alt=""/></li>
            <li><img src="img/imgThmubs2.jpg" data-bigimage="img/bigImg2.jpg" data-original="img/origImg2.jpg" alt=""/></li>
            <li><img src="img/imgThmubs3.jpg" data-bigimage="img/bigImg3.jpg" data-original="img/origImg3.jpg" alt=""/></li>
        </ul>
</div>

我有大图,大图下有 3 个缩略图。如果用户单击第二个缩略图,则大图必须更改为第二个数据-大图。并且 swipebox 链接必须更改为第二个数据原始图像。其他图片也一样。

这是切换图片的解决方案:

    jQuery(document).ready(function( $ ) {
    $('.profile-thumbs li').click(function(){
        var imageurl = $(this).children('img').data('bigimage');
        var imageorig = $(this).children('img').data('original');
        $('.profile-bigImage').attr("src", imageurl);
        $('.swipebox').attr("href", imageorig);
    });
});

这是将图像推送到 swipebox 数组中的代码:

$(function () {
new App();

$('.swipebox').click(function(e) {
    $.swipebox(imgs());
    e.preventDefault();
});

function imgs() {
    var th = $('.profile-thumbs li');
    var arr = [];

    for(var i = 0; i < th.length; i++) {
        var obj = {};
        var current = $(th[i]).find('img').attr('data-original');
        obj['href'] = current;
        arr.push(obj);
    }
    return arr;
}

});

问题是 Swipebox 画廊总是从第一张图片开始滑动。如何更改上面的代码,让它从选定的图像中滑动?

【问题讨论】:

    标签: javascript jquery html photo-gallery


    【解决方案1】:

    您应该使用滑动框中的initialIndexOnArray 选项:

    http://brutaldesign.github.io/swipebox/#options

    选项的完整列表:

    <script type="text/javascript">
    ;( function( $ ) {
    
        $( '.swipebox' ).swipebox( {
            useCSS : true, // false will force the use of jQuery for animations
            useSVG : true, // false to force the use of png for buttons
            initialIndexOnArray : 0, // which image index to init when a array is passed
            hideCloseButtonOnMobile : false, // true will hide the close button on mobile devices
            hideBarsDelay : 3000, // delay before hiding bars on desktop
            videoMaxWidth : 1140, // videos max width
            beforeOpen: function() {}, // called before opening
            afterOpen: null, // called after opening
            afterClose: function() {} // called after closing
            loopAtEnd: false // true will return to the first image after the last image is reached
        } );
    
    } )( jQuery );
    </script>
    

    【讨论】:

    • 阿维纳什谢谢!我没有看到那个选项。我是 Javascript 新手,你能展示几行代码如何根据点击的图像将值传递给 initialIndexOnArray 吗?
    猜你喜欢
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 2021-09-13
    • 2020-07-20
    • 2015-02-20
    • 2011-07-22
    • 1970-01-01
    相关资源
    最近更新 更多