H5通用结构,初探Swiper.js & Animate.css

参考文档:Swiper中文网-轮播图幻灯片js插件,H5页面前端开发
Animate.css | A cross-browser library of CSS animations.

概述及演示

本文实现的是一个自适应全屏H5动画通用结构
囊括了 4.1.1 版本 Animate.css 的全部 97 种动画效果

废话少说,先看结果
字体、背景颜色都是随机的呢😋
在这里插入图片描述


全屏垂直滑动布局

基于 Swiper.js 库的基础上,自定义部分 css 即可

﹂ DOM 结构

<!-- Swiper 区域 -->
<div class="swiper-container">
    <!-- ... -->
    <!-- Slide 显示区域 -->
    <div class="swiper-wrapper">
		<div class="swiper-slide">
			Slide 1
		</div>
		<div class="swiper-slide">
			Slide 2
		</div>
	</div>
    
    <!-- Pagination 分页器区域 -->
    <div class="swiper-pagination"></div>
    <!-- ... -->
</div>

﹂ Swiper 初始化

const swiper = new Swiper('.swiper-container', {
    direction: 'vertical',
    pagination: {
        el: '.swiper-pagination',
        type: 'fraction',
        clickable: true,
    },            
});

﹂ Css 调整

html, body {
    position: relative;
    height: 100%;
}

body {
    background: #eee;
    font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
    font-size: 14px;
    color: #000;
    margin: 0;
    padding: 0;
}

.swiper-container {
    width: 100%;
    height: 100%;
}

.swiper-slide {
    text-align: center;
    font-size: 18px;
    background: #fff;
    /* Center slide text vertically */
    display: -webkit-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    -webkit-justify-content: center;
    justify-content: center;
    -webkit-box-align: center;
    -ms-flex-align: center;
    -webkit-align-items: center;
    align-items: center;
}

在这里插入图片描述


生成全部 Slide 区域

动态生成 div.swiper-slide 节点,再配上点色彩

﹂ Animate 的 97 种动画

官网说明中没有明确数量说明,想要得到所有动画的名称还是有很多方法的

比如利用 开发者工具 F12 遍历页面
在这里插入图片描述
再比如直接通过 vscode正则化搜索 查询 .css 内容
在这里插入图片描述

﹂ 增加 DOM 节点

<!-- Swiper 区域 -->
<div class="swiper-container">
    <!-- 标题区域 -->
    <div class="swiper-title">
        SWIPER ANIMATE SHOW PAGE
    </div>

    <!-- Slide 显示区域 -->
    <div class="swiper-wrapper"></div>
    
    <!-- Pagination 分页器区域 -->
    <div class="swiper-pagination"></div>

    <!-- 选项区域 -->
    <div class="swiper-choice">
        <select class="swiper-selector"></select>
    </div>
</div>

增加 selectortitle 两部分

﹂ 动态生成 Slide 节点

// 构建 Dom
const effects = ['bounce', 'flash',...,'slideOutUp']

const randRGB = () => { return Math.round(Math.random() * 200) }
const randRGBbg = () => { return Math.round(Math.random() * 55) + 200 }

const swiperWrapper = document.querySelector('.swiper-wrapper')
const swiperSelector = document.querySelector('.swiper-selector')

effects.forEach(val => {
    let swEl = document.createElement('div')
    swEl.className = 'swiper-slide'
    swEl.style.backgroundColor = `rgb(${randRGBbg()},${randRGBbg()},${randRGBbg()})`
    swEl.innerHTML = `<p style="color:rgb(${randRGB()},${randRGB()},${randRGB()});">${val}</p>`

    let opEl = document.createElement('option')
    opEl.value = effects.indexOf(val)
    opEl.innerText = val


    swiperWrapper.append(swEl)
    swiperSelector.append(opEl)
})

其中 randRGBrandRGBbg 分别是随机生成字体和背景颜色数值的方法
生成 swiper-slide 的同时,也动态生成 <select> 标签的 <option> 选项

在这里插入图片描述
至此,结构部分就都完成了(个人觉得,还挺好看的🌼)


加入动效

<script src="assets/swiper.animate.min.js"></script>

引入 swiper.animate.js

swEl.innerHTML = `<p class="ani" swiper-animate-effect="${val}" swiper-animate-duration="1s"
	swiper-animate-delay="0.3s" style="color:rgb(${randRGB()},${randRGB()},${randRGB()});">${val}</p>`

修改 effects.forEach 回调中 swEl 节点中 p元素的具体内容,增加了 swiper-animate-effectswiper-animate-duration 属性

const swiper = new Swiper('.swiper-container', {
    direction: 'vertical',
    pagination: {
        el: '.swiper-pagination',
        type: 'fraction',
        clickable: true,
    },
    on: {
        init: function () {
            swiperAnimateCache(this); //隐藏动画元素 
            this.emit('slideChangeTransitionEnd');//在初始化时触发一次slideChangeTransitionEnd事件
        },
        slideChangeTransitionEnd: function () {
            swiperAnimate(this); //每个slide切换结束时运行当前slide动画
            swiperSelector.querySelectorAll('option')[this.activeIndex].selected = true // 当动画完成时更新selector                    
        }
    }
});

增加 swiperinitslideChangeTransitionEnd 事件方法,来触发动画,同时调整 selected

swiperSelector.addEventListener('change', () => {
    swiper.slideTo(swiperSelector.selectedIndex)
})

增加 selectchange 事件,增加除滑屏以外第二种切换方式

至此全部完成🤭
在这里插入图片描述


完整代码

Github - h5-demos / SwiperAnimate

﹂ HTML代码

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

<head>
    <meta charset="utf-8">
    <title>Swiper animate demo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">

    <link href="https://cdn.bootcdn.net/ajax/libs/Swiper/6.4.14/swiper-bundle.min.css" rel="stylesheet">

    <!-- animate.min.css 使用前需要带有prefix -->
    <!-- <link href="https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.1/animate.min.css" rel="stylesheet"> -->
    <link href="https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.1/animate.compat.min.css" rel="stylesheet">
    <link rel="stylesheet" href="assets/style.css">

</head>

<body>
    <!-- Swiper 区域 -->
    <div class="swiper-container">
        <!-- 标题区域 -->
        <div class="swiper-title">
            SWIPER ANIMATE SHOW PAGE
        </div>

        <!-- Slide 显示区域 -->
        <div class="swiper-wrapper"></div>
        
        <!-- Pagination 分页器区域 -->
        <div class="swiper-pagination"></div>

        <!-- 选项区域 -->
        <div class="swiper-choice">
            <select class="swiper-selector"></select>
        </div>
    </div>

    <!-- 引入 JS -->
    <script src="https://cdn.bootcdn.net/ajax/libs/Swiper/6.4.14/swiper-bundle.min.js"></script>
    <script src="assets/swiper.animate.min.js"></script>

    <script>
        // 构建 Dom
        const effects = ['bounce','flash','pulse','rubberBand','shakeX','shakeY','headShake','swing','tada','wobble','jello','heartBeat','backInDown','backInLeft','backInRight','backInUp','backOutDown','backOutLeft','backOutRight','backOutUp','bounceIn','bounceInDown','bounceInLeft','bounceInRight','bounceInUp','bounceOut','bounceOutDown','bounceOutLeft','bounceOutRight','bounceOutUp','fadeIn','fadeInDown','fadeInDownBig','fadeInLeft','fadeInLeftBig','fadeInRight','fadeInRightBig','fadeInUp','fadeInUpBig','fadeInTopLeft','fadeInTopRight','fadeInBottomLeft','fadeInBottomRight','fadeOut','fadeOutDown','fadeOutDownBig','fadeOutLeft','fadeOutLeftBig','fadeOutRight','fadeOutRightBig','fadeOutUp','fadeOutUpBig','fadeOutTopLeft','fadeOutTopRight','fadeOutBottomRight','fadeOutBottomLeft','flip','flipInX','flipInY','flipOutX','flipOutY','lightSpeedInRight','lightSpeedInLeft','lightSpeedOutRight','lightSpeedOutLeft','rotateIn','rotateInDownLeft','rotateInDownRight','rotateInUpLeft','rotateInUpRight','rotateOut','rotateOutDownLeft','rotateOutDownRight','rotateOutUpLeft','rotateOutUpRight','hinge','jackInTheBox','rollIn','rollOut','zoomIn','zoomInDown','zoomInLeft','zoomInRight','zoomInUp','zoomOut','zoomOutDown','zoomOutLeft','zoomOutRight','zoomOutUp','slideInDown','slideInLeft','slideInRight','slideInUp','slideOutDown','slideOutLeft','slideOutRight','slideOutUp']

        const randRGB = () => { return Math.round(Math.random() * 200) }
        const randRGBbg = () => { return Math.round(Math.random() * 55) + 200 }

        const swiperWrapper = document.querySelector('.swiper-wrapper')
        const swiperSelector = document.querySelector('.swiper-selector')

        effects.forEach(val => {
            let swEl = document.createElement('div')
            swEl.className = 'swiper-slide'
            swEl.style.backgroundColor = `rgb(${randRGBbg()},${randRGBbg()},${randRGBbg()})`
            swEl.innerHTML = `<p class="ani" swiper-animate-effect="${val}" swiper-animate-duration="1s"
                    swiper-animate-delay="0.3s" style="color:rgb(${randRGB()},${randRGB()},${randRGB()});">${val}</p>`

            let opEl = document.createElement('option')
            opEl.value = effects.indexOf(val)
            opEl.innerText = val


            swiperWrapper.append(swEl)
            swiperSelector.append(opEl)
        })

        //初始化swiper
        const swiper = new Swiper('.swiper-container', {
            direction: 'vertical',
            pagination: {
                el: '.swiper-pagination',
                type: 'fraction',
                clickable: true,
            },
            on: {
                init: function () {
                    swiperAnimateCache(this); //隐藏动画元素 
                    this.emit('slideChangeTransitionEnd');//在初始化时触发一次slideChangeTransitionEnd事件
                },
                slideChangeTransitionEnd: function () {
                    swiperAnimate(this); //每个slide切换结束时运行当前slide动画
                    swiperSelector.querySelectorAll('option')[this.activeIndex].selected = true // 当动画完成时更新selector                    
                }
            }
        });

        swiperSelector.addEventListener('change', () => {
            swiper.slideTo(swiperSelector.selectedIndex)
        })
    </script>
</body>

</html>

﹂ CSS 代码

/**
* 全屏通用 RESET 部分
*/

html, body {
    position: relative;
    height: 100%;
}

body {
    background: #eee;
    font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
    font-size: 14px;
    color: #000;
    margin: 0;
    padding: 0;
}

.swiper-container {
    width: 100%;
    height: 100%;
}

.swiper-slide {
    text-align: center;
    font-size: 18px;
    background: #fff;
    /* Center slide text vertically */
    display: -webkit-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    -webkit-justify-content: center;
    justify-content: center;
    -webkit-box-align: center;
    -ms-flex-align: center;
    -webkit-align-items: center;
    align-items: center;
}

/**
* 本例独用
*/

.swiper-title {
    top: 20px;
    left: 0;
    width: 100%;
    position: absolute;
    text-align: center;
    -webkit-transition: .3s opacity;
    -o-transition: .3s opacity;
    transition: .3s opacity;
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    z-index: 10;
}

.swiper-wrapper p {
    font-size: 40px;
}

.swiper-choice {
    bottom: 40px;
    left: 0;
    width: 100%;

    position: absolute;
    text-align: center;
    -webkit-transition: .3s opacity;
    -o-transition: .3s opacity;
    transition: .3s opacity;
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    z-index: 10;
}

码字不易,如果喜欢,不用三连,点个赞👍便是最大的鼓励
欢迎关注微信公众号 "书咖里的曼基康"
书咖里的曼基康

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
animateControl v1.0.3 的功能特点: 1、完全兼容swiper的loop模式,完美支持swiper的嵌套。 2、完美支持animate.css,能够为页面中的任意元素添加动画。 3、完美支持为单个元素同时添加多个animate.css动画效果(无需进行HTML标签的嵌套),并且,可以随意控制这些动画效果的播放方式。 4、能够设置每一个动画效果的播放方式:同步播放、依次播放、循环播放。 5、能够设置每一个动画效果的动画类型:进入动画、表演动画、退出动画(在animate.css的源码中,opacity值从0至100的是进入动画,没有opacity属性的是表演动画。opacity值从100到0的是退出动画)。 6、实现了排版效果、动画效果、动画控制的完美分离,极大的提高了开发效率,能够在很短的时间内实现复杂的动画效果控制。 7、animateControl 不与swiper结合时,可对网页中的其它元素添加动画效果,并进行控制。 8、能够让不懂JS代码的人,经过简单的学习,就能快速的制作HTML5+CSS3动态微网页。 9、相较于上一个版本,在 v1.0.3 中对整个核心代码进行了优化,大大提升了运行速率,使动画播放更流畅。 10、相较于上一个版本,在 v1.0.3 中简化了所有参数的名称,让使用变得更加简单(用更少的代码,就能实现更复杂的效果)。 11、本次发布的动画控制器文件为min版,大大减少了文件的体积(下载解压后,文件位置在:js/swiper.animate.min.js)。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值