Halloween Sale 15% off Ends in See the plans
HTML

Swipeable Carousel for Any Block

Turns a Group, Row, Gallery or Query Loop post list with the class "wpide-carousel" into a swipeable horizontal carousel with previous/next buttons, using native scroll snapping.

Don’t have WPIDE yet? Install it on your site

Code

<style>
.wpide-carousel-wrap{position:relative;}
.wpide-carousel{display:flex!important;flex-wrap:nowrap!important;gap:16px;overflow-x:auto;scroll-snap-type:x mandatory;scroll-behavior:smooth;scrollbar-width:none;padding-bottom:4px;}
.wpide-carousel::-webkit-scrollbar{display:none;}
.wpide-carousel>*{flex:0 0 var(--wpide-slide,80%)!important;max-width:none!important;scroll-snap-align:start;margin:0!important;}
@media (min-width:782px){.wpide-carousel>*{flex-basis:var(--wpide-slide-desktop,calc(33.333% - 11px))!important;}}
.wpide-carousel-btn{position:absolute;top:50%;transform:translateY(-50%);width:44px;height:44px;border:0;border-radius:50%;background:#fff;color:#111;box-shadow:0 2px 10px rgba(0,0,0,.2);font-size:22px;line-height:1;cursor:pointer;z-index:2;}
.wpide-carousel-btn[disabled]{opacity:0;pointer-events:none;}
.wpide-carousel-btn--prev{left:-10px;}.wpide-carousel-btn--next{right:-10px;}
@media (prefers-reduced-motion:reduce){.wpide-carousel{scroll-behavior:auto;}}
</style>
<script>
document.querySelectorAll('.wpide-carousel').forEach(function (track) {
    var wrap = document.createElement('div');
    wrap.className = 'wpide-carousel-wrap';
    track.parentNode.insertBefore(wrap, track);
    wrap.appendChild(track);
    track.setAttribute('tabindex', '0');
    track.setAttribute('role', 'region');
    track.setAttribute('aria-label', track.getAttribute('aria-label') || 'Carousel');
    function make(dir, label, text) {
        var b = document.createElement('button');
        b.type = 'button';
        b.className = 'wpide-carousel-btn wpide-carousel-btn--' + dir;
        b.setAttribute('aria-label', label);
        b.textContent = text;
        b.addEventListener('click', function () {
            var step = track.firstElementChild ? track.firstElementChild.getBoundingClientRect().width + 16 : track.clientWidth;
            track.scrollBy({ left: dir === 'next' ? step : -step });
        });
        wrap.appendChild(b);
        return b;
    }
    var prev = make('prev', 'Previous', '\u2039');
    var next = make('next', 'Next', '\u203A');
    function update() {
        prev.disabled = track.scrollLeft < 5;
        next.disabled = track.scrollLeft + track.clientWidth >= track.scrollWidth - 5;
    }
    track.addEventListener('scroll', update, { passive: true });
    window.addEventListener('resize', update);
    update();
});
</script>

Notes

Add wpide-carousel to the block's Additional CSS class(es). Set the slide width with the --wpide-slide variable (default 80% on phones, 33% on desktop).

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.