JavaScript Swiper插件

本文档详细介绍了JavaScript的Swiper插件,包括如何初始化Swiper实例、设置环路和自动切换、调整切换时间与方向,以及配置各种选项如分页器、前进后退按钮和滚动条等。此外,还涵盖了分页器控制切换、触摸事件响应、网格布局和动画效果的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Swiper开发文档

一款免费以及轻量级的移动设备触控滑块的js框架


初始化Swiper实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="./swiper.min.css">
    <script src="./js/jquery-3.4.1.min.js"></script>
    <script src="./js/swiper.min.js"></script>
    <style type="text/css">
        #box1{
    
            width: 600px; height: 300px;
            margin: 50px auto;
        }
        #box1 .sd1, #box1 .sd2, #box1 .sd3{
    
            line-height: 300px; text-align: center;
            font-size: 50px; color: #fff;
        }
        #box1 .sd1{
    background-color: #ff0000;}
        #box1 .sd2{
    background-color: #00ff00;}
        #box1 .sd3{
    background-color: #0000ff;}
    </style>
</head>
<body>
    <!-- swiper页面结构 -->
    <div class="swiper-container" id="box1">
        <div class="swiper-wrapper">
            <div class="swiper-slide sd1">Slide 1</div>
            <div class="swiper-slide sd2">Slide 2</div>
            <div class="swiper-slide sd3">Slide 3</div>
        </div>
    </div>

    <script>  
        // 初始化Swiper实例:new Swiper(swiper容器, 个性化配置)
        var mySwiper = new Swiper ('.swiper-container', {
       });       
    </script>
</body>
</html>

环路及自动切换

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="./swiper.min.css">
    <script src="./js/jquery-3.4.1.min.js"></script>
    <script src="./js/swiper.min.js"></script>
    <style type="text/css">
        #box1{
    
            width: 600px; height: 300px;
            margin: 50px auto;
        }
        #box1 .sd1, #box1 .sd2, #box1 .sd3{
    
            line-height: 300px; text-align: center;
            font-size: 50px; color: #fff;
        }
        #box1 .sd1{
    background-color: #ff0000;}
        #box1 .sd2{
    background-color: #00ff00;}
        #box1 .sd3{
    background-color: #0000ff;}
    </style>
</head>
<body>
    <!-- swiper页面结构 -->
    <div class="swiper-container" id="box1">
        <div class="swiper-wrapper">
            <div class="swiper-slide sd1">Slide 1</div>
            <div class="swiper-slide sd2">Slide 2</div>
            <div class="swiper-slide sd3">Slide 3</div>
        </div>
    </div>

    <script>  
        // 初始化Swiper实例:new Swiper(swiper容器, 个性化配置)
        var mySwiper = new Swiper ('.swiper-container', {
    
        //   direction: 'vertical', // 垂直切换选项; 默认为水平切换
          loop: true, // 循环模式选项; 默认为false
          autoplay: true,  //开启启动切换; 默认为false
        })        
    </script>
</body>
</html>

切换时间和切换方向

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="./swiper.min.css">
    <script src="./js/jquery-3.4.1.min.js"></script>
    <script src="./js/swiper.min.js"></script>
    <style type="text/css">
        #box1{
    
            width: 600px; height: 300px;
            margin: 50px auto;
        }
        #box1 .sd1, #box1 .sd2, #box1 .sd3{
    
            line-height: 300px; text-align: center;
            font-size: 50px; color: #fff;
        }
        #box1 .sd1{
    background-color: #ff0000;}
        #box1 .sd2{
    background-color: #00ff00;}
        #box1 .sd3{
    background-color: #0000ff;}
    </style>
</head>
<body>
    <!-- swiper页面结构 -->
    <div class="swiper-container" id="box1">
        <div class="swiper-wrapper">
            <div class="swiper-slide sd1">Slide 1</div>
            <div class="swiper-slide sd2">Slide 2</div>
            <div class="swiper-slide sd3">Slide 3</div>
        </div>
    </div>

    <script>  
        // 初始化Swiper实例:new Swiper(swiper容器, 个性化配置)
        var mySwiper = new Swiper ('.swiper-container', {
    

        loop: true, // 循环模式选项; 默认为false
        
        //   autoplay: true,  //开启启动切换; 默认为false; 值可以是布尔值,也可以是对象
        autoplay:{
          //当值为对象时,表示自动切换
            delay:1000, //自动切换的时间间隔(单位:ms)
            reverseDirection:true,      //开启反向自动切换
        },
		
		//direction: "vertical",      //Slide的切换方向,默认为水平方向;水平:horizontal、垂直:vertical
		
        speed:2000,     //自动切换过程时间(单位:ms)
        })        
    </script>
</body>
</html>

常用配置项

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="./swiper.min.css">
    <script src="./js/jquery-3.4.1.min.js"></script>
    <script src="./js/swiper.min.js"></script>
    <style type="text/css">
        #box1{
    
            width: 600px; height: 300px;
            margin: 50px auto;
        }
        #box1 .sd1, #box1 .sd2, #box1 .sd3{
    
            line-height: 300px; text-align: center;
            font-size: 50px; color: #fff;
        }
        #box1 .sd1{
    background-color: #ff0000;}
        #box1 .sd2{
    background-color: #00ff00;}
        #box1 .sd3{
    background-color: #0000ff;}
    </style>
</head>
<body>
    <!-- swiper页面结构 -->
    <div class="swiper-container" id="box1">
        <div class="swiper-wrapper">
            <div class="swiper-slide sd1">Slide 1</div>
            <div class="swiper-slide sd2">Slide 2</div>
            <div class="swiper-slide sd3">Slide 3</div>
        </div>
    </div>

    <script>  
        // 初始化Swiper实例:new Swiper(swiper容器, 个性化配置)
        var mySwiper = new Swiper ('.swiper-container',
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值