简体   繁体   English

单击图库中的缩略图时显示 Swiper slider

[英]Display a Swiper slider when clicking a thumbnail in a gallery

I'm building a thumbnail gallery with a slider feature using Swiper.我正在使用 Swiper 构建具有 slider 功能的缩略图库。 By default, the slider is hidden, and when the user clicks one of the images, the slider must be displayed showing the clicked image.默认情况下,slider 是隐藏的,当用户单击其中一张图像时,必须显示 slider 以显示单击的图像。 Once the slider is open, the user can click a Close button to hide it and return to the thumbnail gallery.打开 slider 后,用户可以单击关闭按钮将其隐藏并返回缩略图库。

This is the code I'm using:这是我正在使用的代码:

JS: JS:

var swiper;

swiper = new Swiper( '.gallery-slider .swiper-container', {
    loop: true,
    autoplay: {
        delay: 3000,
        disableOnInteraction: false,
    },
    navigation: {
        nextEl: '.swiper-next',
        prevEl: '.swiper-prev',
    },
});

$( '.gallery-thumbnails a[data-slide]' ).on( 'click', function( e ) {
    e.preventDefault();
    $( '.gallery-thumbnails' ).hide();
    $( '.gallery-slider' ).show();
    var slideno = $( this ).data( 'slide' );
    swiper.slideTo( slideno + 1, false, false );
});

$( '.gallery-slider .close' ).on( 'click', function( e ) {
    e.preventDefault();
    $( '.gallery-slider' ).hide();
    $( '.gallery-thumbnails' ).show();
});

CSS: CSS:

.gallery-slider {
    display: none;
}

HTML: HTML:

<div class="gallery-thumbnails">
    <div class="gallery-image"><a href="" data-slide="0"><img src="thumb0.jpg" /></a></div>
    <div class="gallery-image"><a href="" data-slide="1"><img src="thumb1.jpg" /></a></div>
    <div class="gallery-image"><a href="" data-slide="2"><img src="thumb2.jpg" /></a></div>
    <div class="gallery-image"><a href="" data-slide="3"><img src="thumb3.jpg" /></a></div>
    ....
</div>

<div class="gallery-slider">
    <div class="swiper-container">
        <div class="swiper-prev">previous</div>
        <div class="swiper-next">next</div>
        <div class="close">close</div>
        <div class="swiper-wrapper">
            <div class="swiper-slide"><img src="slide0.jpg" /></div>
            <div class="swiper-slide"><img src="slide1.jpg" /></div>
            <div class="swiper-slide"><img src="slide2.jpg" /></div>
            <div class="swiper-slide"><img src="slide3.jpg" /></div>
            ....
        </div>
    </div>
</div>

Using this code, the slider is shown when a user clicks a thumbnail, but the slider itself doesn't work.使用此代码,当用户单击缩略图时会显示 slider,但 slider 本身不起作用。 The next and prev buttons don't do anything.下一个和上一个按钮不做任何事情。 Is the slider not initialized when hidden? slider 隐藏时是否未初始化?

What am I doing wrong?我究竟做错了什么? Any help would be appreciated.任何帮助,将不胜感激。

Thanks谢谢

Apparently, you need to add the observer and observeParents parameters when initializing Swiper so the slider is updated (reinitialized) each time you change its style (like hide/show) or modify its child elements (like adding/removing slides) or its parent element.显然,您需要在初始化observeParents时添加observer和观察父母参数,以便 slider 每次更改其样式(如隐藏/显示)或修改其子元素(如添加/删除幻灯片)或其父元素时都会更新(重新初始化) .

var swiper;

swiper = new Swiper( '.gallery-slider .swiper-container', {
    loop: true,
    observer: true,
    observeParents: true,
    autoplay: {
        delay: 3000,
        disableOnInteraction: false,
    },
    navigation: {
        nextEl: '.swiper-next',
        prevEl: '.swiper-prev',
    },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM