当前位置: 首页 > article >正文

轮播库-swiper使用案例

Swiper使用案例

记得先请求ajax数据再去渲染界面,否则loop会出现问题。虽然也可以使用observer:true来解决

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="lib/swiper-bundle.min.css">
    <script src="lib/swiper-bundle.min.js"></script>

    <script src="lib/jquery.js"></script>

    <style>
        .kunkun img {
            width: 100%;
        }
    </style>
</head>

<body>

    <div class="swiper kunkun">
        <div class="swiper-wrapper">
            <!-- <div class="swiper-slide">
                <img src=""/>
            </div> -->
        </div>
        <!-- 如果需要分页器 -->
        <div class="swiper-pagination"></div>
    </div>
    <script>
        $.get("http://localhost:3000/banner").then(res => {
            console.log(res)
            render(res)
            initSwiper()
        })

        function render(list) {
            var oslides = list.map(item => `
            <div class="swiper-slide">
                <img src="${item.imgUrl}"/>
            </div>
            `)

            // console.log(oslides.join(""))

            $(".swiper-wrapper").html(oslides.join(""))
        }


        $.extend({
            swiper: function (ele, obj) {
                new Swiper(ele,obj)
            }
        })

        function initSwiper() {
            $.swiper(".kerwin", {
                // direction: 'vertical', // 垂直切换选项
                loop: true, // 循环模式选项
                // 如果需要分页器
                pagination: {
                    el: '.swiper-pagination',
                    clickable: true
                },
                // 保证loop:true生效。如果不设置这个,且先渲染然后发送ajax请求,会loop失效,建议先请求后渲染
                observer: true 
            })
        }
    </script>
</body>

</html>

http://www.kler.cn/a/531450.html

相关文章:

  • 【DeepSeek背后的技术】系列一:混合专家模型(MoE)
  • w191教师工作量管理系统的设计与实现
  • c++ stl 遍历算法和查找算法
  • Linux网络 HTTPS 协议原理
  • 数据结构与算法之栈: LeetCode 641. 设计循环双端队列 (Ts版)
  • 何谓共赢?
  • CommonJS 和 ES6module 的区别
  • Linux系统下安装配置 Nginx 超详细图文教程
  • 梯度、梯度下降、最小二乘法
  • 快速上手mybatis教程
  • XCCL、NCCL、HCCL通信库
  • 算法基础——一致性
  • 强化学习笔记(5)——PPO
  • c++ 定点 new 及其汇编解释
  • 数据结构之栈和队列(超详解)
  • 使用 Kotlin 将 Vertx 和 Springboot 整合
  • C++类的初始化列表是怎么一回事?哪些东西必须放在初始化列表中进行初始化,原因是什么?
  • MySQL(高级特性篇) 13 章——事务基础知识
  • Docker 部署 ClickHouse 教程
  • 搜索插入位置(35)
  • SpringBoot整合Mybatis|入门级增删改查|2025
  • Chromium132 编译指南 - Android 篇(五):获取源码
  • 八. Spring Boot2 整合连接 Redis(超详细剖析)
  • 自动化软件测试的基本流程
  • Ubuntu20安装docker
  • 【DeepSeek论文精读】6. DeepSeek R1:通过强化学习激发大语言模型的推理能力