目錄
需要解決的問題" >需要解決的問題
程式碼實作" >程式碼實作
微信小程序代码" >微信小程序代码
首頁 web前端 js教程 小程式swiper輪播CSS3動畫及跳到指定swiper-item的使用

小程式swiper輪播CSS3動畫及跳到指定swiper-item的使用

Jun 22, 2020 am 11:32 AM
html5 uni-app 微信小程式

本人微信公眾號:前端修練之路,歡迎關注。

需要解決的問題

最近幾日一直在看怎麼製作微信小程式的swiper輪播圖。因為我既需要產生小程式的程式碼,也需要產生H5版程式碼,如果寫兩套效率會比較低下,所以選擇了uni-app。

uni-app已經在基礎元件swiper中已經直接支援了輪播動畫。

我主要需要解決的是以下幾個問題:

  • ①在swiper中怎樣加入css3流行的<span style="font-size: 14px;">animate.css </span>動畫。
  • ②添加好後如果滑動了輪播圖,怎麼能保證下一螢幕的動畫不會自動播放。
  • ③怎麼能實現輪播圖的無限循環播放。
  • ④怎麼能實現,當使用者點擊一個按鈕之後,可以跳到指定的<span style="font-size: 14px;">swiper-item</span>中。也就是跳到指定的螢幕。
  • ⑤小程式和H5版的程式碼會產生一個頭部,在H5版中需要隱藏掉導覽列。

以下就是我整個製作的思維過程,僅供參考。另外,程式碼是uni-app開發,所以在小程式中和H5中測試都沒有問題。另外為了方便小程式開發同學了解,會提供小程式版程式碼和uni-app程式碼供參考。

程式碼實作

在H5開發中常用的就是animate.css。在微信中自然是支援的,因為微信會對上傳的小程式有大小限制,所以這裡我使用了一個極簡化的animate.css,其中刪掉了很多-webkit- animation開頭的css3。因為我們只需要在小程式和H5中運行,這樣做影響也不大。如果需要的話,可以從下面的程式碼中取得。

我們先來看下程式碼:

<template>
    <view class="content">
        <button type="primary" @tap="goChange">跳转到第二屏</button>
        <swiper class="content-swiper" :vertical="true" :indicator-dots="true" :autoplay="false" :interval="3000" :duration="1000" @change="changeSwiper" @animationfinish="changeFinish" :current-item-id="item_id" circular="true">
            <swiper-item item-id="slide0">
                <view class="swiper-item">
                    <image src="../../static/uni.png" :class="animate_0"></image>
                </view>
            </swiper-item>
            <swiper-item item-id="slide1">
                <view class="swiper-item">
                    <image src="../../static/uni.png" :class="animate_1"></image>
                </view>
            </swiper-item>
            <swiper-item item-id="slide2">
                <view class="swiper-item">
                    <image src="../../static/uni.png" :class="animate_2"></image>
                </view>
            </swiper-item>
            <swiper-item item-id="slide3">
                <view class="swiper-item">
                    <image src="../../static/uni.png" :class="animate_3"></image>
                </view>
            </swiper-item>
        </swiper>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                item_id: 'slide2',
                animate_0: 'animated swing',
                animate_1: '',
                animate_2: '',
                animate_3: ''
            }
        },
        onLoad() {

        },
        methods: {
            changeSwiper(event){    // 清空除了当前swiper以外的所有动画
                let current = event.detail.current;    // 当前页下标
                this.item_id = 'slide'+current;     // 这里必须记录,否则只能跳转一次
                switch (current){
                    case 0:
                        this['animate_1'] = this['animate_2'] = this['animate_3'] = '';
                    break;
                    case 1: 
                        this['animate_0'] = this['animate_2'] = this['animate_3'] = ''; 
                    break;
                    case 2:
                        this['animate_0'] = this['animate_1'] = this['animate_3'] = '';
                    break;
                    case 3:
                        this['animate_0'] = this['animate_1'] = this['animate_2'] = '';
                    break;
                }

            },
            changeFinish(event){ // swiper动画完成之后,给当前swiper添加动画效果
                let current = event.detail.current;
                switch(current){
                    case 0: 
                        this['animate_0'] = 'animated swing';
                    break;
                    case 1:
                        this['animate_1'] = 'animated shake';
                    break;
                    case 2:
                        this['animate_2'] = 'animated tada';
                    break;
                    case 3:
                        this['animate_3'] = 'animated heartBeat';
                    break;
                }
            },
            goChange(){
                this.item_id = 'slide1';
            }
        }
    }
</script>

<style lang="scss">
    @import '../../common/animate.css';
    
    .content {
        text-align: center;
        .content-swiper{
            height: 100vh;
            
            image{
                height: 200upx;
                width: 200upx;
                margin-top: 200upx;
            }
        }
    }
</style>
登入後複製
  • 首先<span style="font-size: 14px;">uni-app</span>支持sass。在css中直接引入了简洁版<span style="font-size: 14px;">animate.css</span>问题①
  • 之后通过查看文档,发现<span style="font-size: 14px;">circular</span>这个参数可以实现类似H5页面使用swiper.js<span style="font-size: 14px;">loop</span>参数的功能。这里我掉到了<span style="font-size: 14px;">uni-app</span><span style="font-size: 14px;">微信小程序</span>文档描述的坑中。因为一直在找<span style="font-size: 14px;">loop</span>(循环)这个参数,我甚至都以为实现不了这个无限循环的功能了呢。原来<span style="font-size: 14px;">小程序</span>中这个参数叫做<span style="font-size: 14px;">circular</span>(圆形)。o(╯□╰)o 问题③
  • 因为我这里要实现一个竖屏的滑动效果,所以将参数<span style="font-size: 14px;">vertical</span>设置为<span style="font-size: 14px;">true</span>
  • <span style="font-size: 14px;">uni-app</span>中,通过<span style="font-size: 14px;">change</span>事件,可以监听每一个轮播屏的改变。在这个事件中,我记录的当前屏的下标<span style="font-size: 14px;">current</span>。然后将非当前屏的全部css3动画取消掉。最后在<span style="font-size: 14px;">animationfinish</span>事件中,当<span style="font-size: 14px;">swiper</span>滑动动画结束后,给当前屏的元素添加css3动画。问题②
  • <span style="font-size: 14px;">uni-app</span>中有个<span style="font-size: 14px;">current-item-id</span>参数,代表当前所在滑块的 <span style="font-size: 14px;">item-id</span>。这个文档我看了好久,才明白。原来是需要在<span style="font-size: 14px;">swiper-item</span>中指定上<span style="font-size: 14px;">item-id</span>。然后当用户点击事件触发时,修改绑定到<span style="font-size: 14px;">current-item-id</span>上的值即可。我的代码初始化时指定到了<span style="font-size: 14px;">item-id</span><span style="font-size: 14px;">slide2</span>这一屏上。问题④
  • 最后一个问题时<span style="font-size: 14px;">uni-app</span>中隐藏掉H5导航栏。只需要在<span style="font-size: 14px;">pages.json</span>中设置<span style="font-size: 14px;">titleNView</span><span style="font-size: 14px;">false</span>即可。

微信小程序代码

<!--index.wxml-->
<view class="container">
    <button bindtap=&#39;goChange&#39;>跳转到</button>
    <swiper vertical="true" circular="true" current="{{currentId}}" indicator-dots="true" bindchange="changeSwiper" bindanimationfinish="changeFinish">
        <swiper-item>
            <image src=&#39;../../static/uni.png&#39; class=&#39;animated {{animate_0}}&#39;></image>
        </swiper-item>
        <swiper-item>
            <image src=&#39;../../static/uni.png&#39; class=&#39;animated {{animate_1}}&#39;></image>
        </swiper-item>
        <swiper-item>
            <image src=&#39;../../static/uni.png&#39; class=&#39;animated {{animate_2}}&#39;></image>
        </swiper-item>
    </swiper>
</view>
//index.js
const app = getApp()

Page({
    data: {
        currentId: 0,
        animate_0: 'swing',
        animate_1: '',
        animate_2: ''
    },
    onLoad: function() {

    },
    goChange: function() {
        this.setData({
            currentId: 2
        });
    },
    changeSwiper: function(event) {
        let current = event.detail.current;
        switch (current) {
            case 0:
                this.setData({
                    animate_1: '',
                    animate_2: ''
                });
                break;
            case 1:
                this.setData({
                    animate_0: '',
                    animate_2: ''
                });
                break;
            case 2:
                this.setData({
                    animate_0: '',
                    animate_1: ''
                });
                break;
        }
    },
    changeFinish: function(event) {
        let current = event.detail.current;
        switch (current) {
            case 0:
                this.setData({
                    animate_0: 'swing',
                });
                break;
            case 1:
                this.setData({
                    animate_1: 'shake',
                });
                break;
            case 2:
                this.setData({
                    animate_2: 'tada',
                });
                break;
        }
    }
})
登入後複製

我将代码托管到了腾讯云开发者平台,需要的话可以参考。在代码目录unpackage/dist/build/h5中,就是生成好的H5版页面。需要注意的是,要部署到web服务器使用,不支持本地file协议打开。
其中生成了两个版本的代码,方便大家参考。

推荐教程:《微信小程序

以上是小程式swiper輪播CSS3動畫及跳到指定swiper-item的使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1662
14
CakePHP 教程
1419
52
Laravel 教程
1312
25
PHP教程
1262
29
C# 教程
1235
24
HTML 中的表格邊框 HTML 中的表格邊框 Sep 04, 2024 pm 04:49 PM

HTML 表格邊框指南。在這裡,我們以 HTML 中的表格邊框為例,討論定義表格邊框的多種方法。

HTML 中的巢狀表 HTML 中的巢狀表 Sep 04, 2024 pm 04:49 PM

這是 HTML 中巢狀表的指南。這裡我們討論如何在表中建立表格以及對應的範例。

HTML 左邊距 HTML 左邊距 Sep 04, 2024 pm 04:48 PM

HTML 左邊距指南。在這裡,我們討論 HTML margin-left 的簡要概述及其範例及其程式碼實作。

HTML 表格佈局 HTML 表格佈局 Sep 04, 2024 pm 04:54 PM

HTML 表格佈局指南。在這裡,我們詳細討論 HTML 表格佈局的值以及範例和輸出。

HTML 輸入佔位符 HTML 輸入佔位符 Sep 04, 2024 pm 04:54 PM

HTML 輸入佔位符指南。在這裡,我們討論 HTML 輸入佔位符的範例以及程式碼和輸出。

HTML 有序列表 HTML 有序列表 Sep 04, 2024 pm 04:43 PM

HTML 有序列表指南。在這裡我們也分別討論了 HTML 有序列表和類型的介紹以及它們的範例

HTML onclick 按鈕 HTML onclick 按鈕 Sep 04, 2024 pm 04:49 PM

HTML onclick 按鈕指南。這裡我們分別討論它們的介紹、工作原理、範例以及各個事件中的onclick事件。

在 HTML 中移動文字 在 HTML 中移動文字 Sep 04, 2024 pm 04:45 PM

HTML 中的文字移動指南。在這裡我們討論一下marquee標籤如何使用語法和實作範例。

See all articles