Blog
Moving Slider with CSS and jQuery: Responsive Image Panning
Moving Slider with CSS and jQuery: Responsive Image Panning
Build a moving slider by letting jQuery select the visible slide while CSS slowly pans its decorative image layer. Keep captions still, use native Previous and Next buttons, and provide a way to pause motion. This example changes slides only when the user chooses one.
The responsive demo includes three original illustrated landscapes, numbered slide controls, a pause-motion checkbox and reduced-motion support. The source ZIP works offline with local artwork and jQuery; no carousel plugin or build step is required.
Try the moving slider live demo
Moving landscape playground
Choose a scene with Previous, Next or the numbered buttons. Only its decorative artwork pans; the caption stays still. Slides never change automatically.
Each selected scene pans once over eight seconds. Reduced motion makes it static. If scripting is unavailable, all three scenes remain readable below one another.
Separate slide selection from image movement
There are two different behaviors. Selecting a slide changes which scene is visible. Panning moves the decorative background inside the current scene. Keeping these separate makes the controls and motion preferences easier to manage.
This version has no automatic slide rotation. Each selected scene pans once for eight seconds and then holds its last position. Previous, Next and the numbered buttons select a different scene. The pause checkbox can stop the pan before it finishes.
1. Start with readable slide content
Write every scene into the HTML before enhancement. Without jQuery or JavaScript, all scenes remain visible in document order. The enhanced controls start hidden so visitors are not presented with buttons that do nothing when scripting fails.
<section class="slider" aria-labelledby="slider-title">
<h2 id="slider-title">Moving landscapes</h2>
<div class="controls" hidden>
<button type="button" class="previous">Previous slide</button>
<button type="button" class="next">Next slide</button>
</div>
<div class="slide" data-title="Alpine dawn">
<div class="art" aria-hidden="true"></div>
<h3>Alpine dawn</h3>
<p>Layered peaks and a quiet mountain lake.</p>
</div>
<!-- More scenes follow. -->
</section>
The example art is decorative because the visible caption supplies the scene’s name and description. If your image carries information that is not already in the text, use an image element with suitable alternative text. Do not hide meaningful product details, charts or instructions as decorative backgrounds.
2. Create a clipped, oversized image layer
.art {
position: relative;
aspect-ratio: 16 / 9;
min-height: 180px;
overflow: hidden;
}
.art::before {
content: "";
position: absolute;
inset: -8%;
background: url("landscape.svg") center / cover no-repeat;
transform: translateX(3%);
}
@keyframes landscape-pan {
from { transform: translateX(3%); }
to { transform: translateX(-3%); }
}
The layer extends past the frame, leaving enough image area for a small horizontal pan. The frame clips the overflow, so moving the image does not create a horizontal page scrollbar. Keep movement modest and check every replacement image at narrow and wide sizes; important details near the edges may be cropped.
The source uses original SVG illustrations encoded into CSS data URLs. It also includes the editable SVG files. You can replace the backgrounds with licensed photographs, but choose appropriate file sizes and avoid loading oversized hero images merely for a small component.
3. Switch slides with jQuery
Use one current index and normalize it to the slide count. This makes Next wrap from the final scene back to the first and Previous wrap from the first scene to the last. The following excerpt shows the selection logic; the full source adds controls and accessible state updates.
var current = 0;
var $slides = $root.find('.slide');
var count = $slides.length;
function show(index) {
current = (index + count) % count;
$slides.each(function (i) {
$(this)
.prop('hidden', i !== current)
.toggleClass('is-active', i === current);
});
}
$root.find('.next').on('click', function () { show(current + 1); });
$root.find('.previous').on('click', function () { show(current - 1); });
The jQuery on() documentation explains attaching event handlers. Bind handlers after the component exists, either after its HTML or during document readiness. Scope selections to the current component so multiple sliders do not accidentally operate on one another.
The complete implementation starts its index at minus one, selects scene zero during initialization and ignores repeated selection of the already-active scene. It shows only one slide and updates the numbered control’s current state and visible status. It does not move keyboard focus when a slide changes.
4. Add the moving-image animation and pause control
/* In the full source, animation is enabled only when :has is supported. */
.slider-ready .slide.is-active .art::before {
animation: landscape-pan 8s linear forwards;
}
.slider:has(.pause-motion:checked) .art::before {
animation-play-state: paused;
}
The pause control is a visible native checkbox labeled Pause image motion. Its checked state pauses the decorative layer and remains selected when the user chooses another slide. Unchecking it resumes the current pan; if the animation has already reached its end, choose another scene and return to replay.
The actual stylesheet gates panning behind support for the selector used by the pause control. In older browsers that cannot apply that selector, artwork remains static while slide buttons still work. A pause control must not appear functional while motion continues without it.
5. Use normal keyboard controls and clear slide status
The carousel has native buttons. Tab and Shift+Tab move between controls, and Enter or Space activates a button. Focus stays on that control, making repeated Next or Previous actions predictable. Inactive slides use the HTML hidden state rather than being merely shifted offscreen.
The WAI-ARIA carousel pattern describes named containers, slides and native navigation buttons. The enhanced demo names each slide with its position and title. A polite status reports user-selected changes without interrupting the current announcement.
Numbered selectors are buttons in a named group, not tabs. They keep their normal Tab stops and indicate the current choice with aria-current. This small three-scene example does not implement arrow-key tab behavior or add a custom keyboard shortcut.
6. Respect reduced motion
@media (prefers-reduced-motion: reduce) {
.slider .art::before {
animation: none !important;
transform: none;
}
}
The static version keeps the artwork, caption and slide controls. A user can still switch scenes without an animated pan. There is no automatic rotation timer to pause or restart, and the slider never advances merely because a reader has been looking at a caption for several seconds.
7. Make controls fit small screens
Use a flexible row that can wrap the Previous button, numbered controls and Next button. Keep button targets large enough to activate comfortably, let captions wrap, and avoid a fixed full-screen height. The demo uses a 16:9 frame with a minimum height so small screens retain a usable illustration area.
Check the actual page after embedding it in WordPress. Theme rules may change button typography, hide overflow or add paragraph wrappers. The component’s CSS and events use a unique prefix, and its control layout accounts for common editor-added wrappers.
Download and run the complete project
Extract the ZIP and open index.html. It includes CSS, JavaScript, three editable SVG illustrations, a no-script fallback page, a README, and a local copy of jQuery 3.7.1 with its MIT license. This pinned tutorial dependency is not a claim about the latest jQuery release.
Load local jQuery before script.js in the standalone project. On a WordPress page that already loads jQuery, use the existing instance instead of adding a second copy. For a maintained integration, enqueue the component assets through a theme or plugin and verify its script permissions and content policy.
Common slider problems
- Buttons do nothing: check that jQuery loaded before the component script and that initialization runs after the markup exists.
- Blank space appears during panning: increase the artwork inset or reduce the translation, then test the crop.
- Hidden slides are announced: hide inactive content properly instead of moving it offscreen alone.
- The pause checkbox seems ineffective: the pan may have finished, or reduced motion may already make it static.
- The layout overflows a phone: allow controls to wrap and constrain the frame to its container.
Frequently asked questions
Does this slider change slides automatically?
No. It changes only through Previous, Next or the numbered buttons. The current scene’s decorative image pans once and then stops.
Can I use my own images?
Yes. Replace the decorative backgrounds and check cropping, file size and licensing. Use meaningful image alternatives when a picture communicates information beyond the caption.
Why is only the image moving?
Keeping the caption still helps readers follow the content while the background provides a subtle visual effect. CSS transforms apply only to the decorative layer.
What happens without JavaScript?
All three scenes remain visible as static content, and the enhanced controls remain hidden. The ZIP includes a separate page that omits both scripts for checking this fallback.
Related tutorials
Continue with sliding tabs with CSS and jQuery or a vertical menu that expands on hover and focus.