当前位置:首页 > JavaScript

js实现图片滑动

2026-03-01 01:11:44JavaScript

实现图片滑动效果

使用JavaScript实现图片滑动效果可以通过多种方式完成,以下是几种常见的方法:

使用CSS和JavaScript结合实现滑动

通过CSS设置容器和图片样式,JavaScript处理滑动逻辑:

const slider = document.querySelector('.slider');
const images = document.querySelectorAll('.slider img');
let currentIndex = 0;

function slideTo(index) {
    slider.style.transform = `translateX(-${index * 100}%)`;
}

document.querySelector('.next').addEventListener('click', () => {
    currentIndex = (currentIndex + 1) % images.length;
    slideTo(currentIndex);
});

document.querySelector('.prev').addEventListener('click', () => {
    currentIndex = (currentIndex - 1 + images.length) % images.length;
    slideTo(currentIndex);
});

对应的CSS样式:

.slider {
    display: flex;
    transition: transform 0.5s ease;
    width: 100%;
}

.slider img {
    width: 100%;
    flex-shrink: 0;
}

使用Swiper.js库

Swiper是一个流行的滑动库,可以轻松实现各种滑动效果:

import Swiper from 'swiper';
import 'swiper/css';

const swiper = new Swiper('.swiper', {
    loop: true,
    navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
    },
    pagination: {
        el: '.swiper-pagination',
    },
});

HTML结构:

<div class="swiper">
    <div class="swiper-wrapper">
        <div class="swiper-slide"><img src="image1.jpg"></div>
        <div class="swiper-slide"><img src="image2.jpg"></div>
        <div class="swiper-slide"><img src="image3.jpg"></div>
    </div>
    <div class="swiper-pagination"></div>
    <div class="swiper-button-next"></div>
    <div class="swiper-button-prev"></div>
</div>

实现自动轮播

在基础滑动上添加自动轮播功能:

let intervalId;
const delay = 3000;

function startAutoSlide() {
    intervalId = setInterval(() => {
        currentIndex = (currentIndex + 1) % images.length;
        slideTo(currentIndex);
    }, delay);
}

function stopAutoSlide() {
    clearInterval(intervalId);
}

slider.addEventListener('mouseenter', stopAutoSlide);
slider.addEventListener('mouseleave', startAutoSlide);

startAutoSlide();

添加触摸支持

为移动设备添加触摸事件支持:

let touchStartX = 0;
let touchEndX = 0;

slider.addEventListener('touchstart', (e) => {
    touchStartX = e.changedTouches[0].screenX;
});

slider.addEventListener('touchend', (e) => {
    touchEndX = e.changedTouches[0].screenX;
    handleSwipe();
});

function handleSwipe() {
    if (touchEndX < touchStartX) {
        // 向左滑动,显示下一张
        currentIndex = (currentIndex + 1) % images.length;
        slideTo(currentIndex);
    }
    if (touchEndX > touchStartX) {
        // 向右滑动,显示上一张
        currentIndex = (currentIndex - 1 + images.length) % images.length;
        slideTo(currentIndex);
    }
}

添加过渡效果

使用CSS添加平滑过渡效果:

js实现图片滑动

.slider {
    transition: transform 0.5s cubic-bezier(0.25, 0.1, 0.25, 1);
}

这些方法可以根据具体需求进行组合和调整,实现不同风格的图片滑动效果。

标签: 图片js
分享给朋友:

相关文章

vue图片实现多选

vue图片实现多选

Vue图片多选实现方法 基础实现方案 使用v-model绑定数组配合input[type="checkbox"]实现多选功能。创建图片列表数据时,每个图片对象应包含唯一标识符: data() {…

js 进度条的实现

js 进度条的实现

使用 HTML 和 CSS 创建基础进度条 HTML 结构可以简单使用一个 div 元素作为容器,内部嵌套另一个 div 表示进度: <div class="progress-containe…

js实现选题

js实现选题

实现选题功能的JavaScript方法 基础实现方案 使用数组存储选项,通过随机索引选取: const options = ['选项A', '选项B', '选项C', '选项D']; const r…

js实现求导

js实现求导

实现数值求导的方法 在JavaScript中实现求导通常采用数值方法,因为JavaScript不是符号计算语言。以下是常见的数值微分方法: 中心差分法 中心差分法提供较高精度的导数近似: func…

js实现的游戏

js实现的游戏

JavaScript 游戏开发基础 JavaScript 是开发网页游戏的流行选择,因其无需插件即可在浏览器中运行。以下是一些关键技术和资源: HTML5 Canvas Canvas 提供了绘制图形…

js 实现拖拽

js 实现拖拽

实现拖拽的基本步骤 在JavaScript中实现拖拽功能需要监听几个关键事件:mousedown、mousemove和mouseup。以下是实现的基本逻辑。 监听目标元素的mousedown事件,记…