如何使用swiper写一个场景式app

本文介绍了如何利用swiper这个JavaScript插件来构建一个场景式app。内容包括从swiper官网下载资源,引入jQuery,配置swiper参数如mode和resistance,以及展示基本的初始化和HTML结构。示例代码展示了垂直滑动的实现,适用于仅在微信环境运行的app。
摘要由CSDN通过智能技术生成

swiper是一个js插件,主要实现幻灯片播放的效果,使用swiper可以轻松搭建一个场景式app.

这里写图片描述

大家可以使用手机打开如下链接查看最终效果:
http://www.tang520.com/wap_download1.html

首先需要前往swiper官网下载js和css文件:
http://www.swiper.com.cn/


引入头文件

<link rel="stylesheet" type="text/css" href="css/idangerous.swiper.css">
<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="js/idangerous.swiper.min.js"></script>

因为场景式app只运行在微信上,我们不考虑ie浏览器,所以直接使用jquery2.1.1版本.


所有的swiper代码都张差不多,如下所示:

<div class="swiper-container">  
    <div class="swiper-wrapper">
            <div class="swiper-slide">
                ....场景1
            </div>
            <div class="swiper-slide">
                ....场景2                 
            </div>
            <div class="swiper-slide">
                ....场景3             
            </div>
    </div>
</div>

在其中….部分写入任意的html代码


使用如下代码初始化swiper:

var mySwiper = new Swiper('.swiper-container', 
                        {
                                mode: 'vertical',
                                resistance:'100%'
                        });

其中mode指定了滑动方向,当前是垂直滑动.resistance指是否边缘拉伸效果,具体大家可以测试下.


有了这两个swiper就可以正常运行了.
下面贴出这个场景式app的所有代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

    <head>
        <meta name="viewport" content="width=240,user-scalable=no,initial-scale=1,target-densitydpi=medium-dpi" />
        <meta charset="utf-8">
        <title>糖库精品商城</title>
        <script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
        <script type="text/javascript" src="js/idangerous.swiper.min.js"></script>
        <script type="text/javascript" src="js/transformjs.1.0.beta.2.min.js"></script>
        <script type="text/javascript" src="js/jquery.imgpreload.min.js"></script>
        <link rel="stylesheet" type="text/css" href="css/idangerous.swiper.css">
        <link rel="stylesheet" type="text/css" href="css/animate.css">
        <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
        <style>
            body {
                width: 100%;
                height: 100%;
                overflow: hidden;
                margin: 0px;
            }
            .swiper-container {
                width: 100%;
                height: 100%;
                color: #fff;
                display: none;
            }
            .swiper-slide {
                height: 100%;
                opacity: 0.6;
                -webkit-transition: 300ms;
                -webkit-transform: scale(0.9);
                overflow: hidden;
            }
            .swiper-slide img {
                width: 100%;
                position: absolute;
                left: 0px;
                top: 0px;
            }
            .swiper-slide-visible {
                opacity: 0.5;
                -webkit-transform: scale(1);
            }
            .swiper-slide-active {
                top: 0;
                opacity: 1;
                -webkit-transform: scale(1);
            }
            .loader {
                width: 100%;
                height: 100%;
            }
            .loader img {
                position: absolute;
                left: 0px;
                width: 100%;
            }
            .loader span {
                text-align: center;
                width: 100%;
                position: absolute;
                bottom: 3px;
                left: 0px;
            }
            .poss {
                width: 0%;
                height: 3px;
                background-color: #0099FF;
                position: absolute;
                bottom: 0px;
                left: 0px;
            }
            .bt1 {
                width: 100%;
                height: auto;
                position: absolute;
                left: 0px;
                top: 0px;
            }
            .bt {
                width: 80%;
                height: 45px;
                line-height: 45px;
                background-color: rgba(255, 255, 255, 0.5);
                color: white;
                text-align: center;
                z-index: 99;
                margin: 4px auto;
                border-radius: 3px;
                font-weight: bold;
            }
            .bottomArrow {
                position: absolute;
                bottom: 15px;
                width: 25px;
                height: 19px;
                background: url(img/bottomarrow.png) no-repeat;
                background-size: 100% 100%;
                z-index: 100;
                -webkit-animation: guideTop 1.5s infinite;
            }
            @-webkit-keyframes guideTop {
                0% {
                    -webkit-transform: translateY(35px);
                    opacity: 0
                }
                60% {
                    -webkit-transform: translateY (12px);
                    opacity: 1
                }
                100% {
                    -webkit-transform: translateY (0px);
                    opacity: 0
                }
            }
        </style>
    </head>

    <body>
        <div class="loader">
            <span>加载中</span>
            <img src="img/loadding.gif" width="100%" id="load" />
            <div class="poss"></div>
        </div>
        <div class="swiper-container">

            <div class="swiper-wrapper">
                <div class="swiper-slide">
                    <img src="img/1-06-back.png" />
                    <img src="img/1-06.png" />
                    <div class="bt1">
                        <div class="bt" onclick="window.location.href='http://mp.weixin.qq.com/s?__biz=MjM5Njg3OTUzNw==&mid=204257251&idx=1&sn=a0300c8f69599571ec0d3fdc2df69c12&scene=1&from=groupmessage&isappinstalled=0#rd'">关注公众号</div>
                        <div class="bt" onclick="window.location.href='http://mp.weixin.qq.com/mp/redirect?url=https://itunes.apple.com/us/app/gotomeeting/id962258223'">下载苹果版</div>
                        <div class="bt" onclick="window.location.href='http://zhushou.360.cn/detail/index/soft_id/2516732'">下载安卓版</div>
                    </div>
                    <div class="bottomArrow"></div>
                </div>
                <div class="swiper-slide">
                    <img src="img/1-02-back.png" />
                    <img src="img/1-02.png" class="animated tada"/>
                    <div class="bottomArrow"></div>
                </div>
                <div class="swiper-slide">
                    <img src="img/1-03-back.png" />
                    <img src="img/1-03.png" />
                    <div class="bottomArrow"></div>
                </div>
                <div class="swiper-slide">
                    <img src="img/1-04-back.png" />
                    <img src="img/1-04.png" />
                    <div class="bottomArrow"></div>
                </div>
                <div class="swiper-slide">
                    <img src="img/1-05-back.png" />
                    <img src="img/1-05.png" />
                    <div class="bottomArrow"></div>
                </div>
                <div class="swiper-slide">
                    <img src="img/1-06-back.png" />
                    <img src="img/1-06.png" />
                    <div class="bt1">
                        <div class="bt" onclick="window.location.href='http://mp.weixin.qq.com/s?__biz=MjM5Njg3OTUzNw==&mid=204257251&idx=1&sn=a0300c8f69599571ec0d3fdc2df69c12&scene=1&from=groupmessage&isappinstalled=0#rd'">关注公众号</div>
                        <div class="bt" onclick="window.location.href='http://mp.weixin.qq.com/mp/redirect?url=https://itunes.apple.com/us/app/gotomeeting/id962258223'">下载苹果版</div>
                        <div class="bt" onclick="window.location.href='http://zhushou.360.cn/detail/index/soft_id/2516732'">下载安卓版</div>
                    </div>
                </div>
            </div>
        </div>
        <script>
            var imgs = ["img/1-02.png", "img/1-02-back.png", "img/1-03.png",
                "img/1-03-back.png", "img/1-04.png", "img/1-04-back.png",
                "img/1-05.png", "img/1-05-back.png", "img/1-06.png", "img/1-06-back.png"
            ];
            $(document).ready(
                function() {
                    $("#load").css("top",
                        window.innerHeight / 2 - window.innerWidth / 2);
                    var sum = 0;
                    jQuery.imgpreload(imgs, {
                        each: function() {
                            sum++;
                            $(".poss").css("width",
                                sum / imgs.length * 100 + "%");
                        },
                        all: function() {
                            $(".swiper-container").fadeIn(0);
                            var mySwiper = new Swiper('.swiper-container', {
                                mode: 'vertical',
                                resistance:'100%',
                                updateOmImagesReady: true,
                                onSlideChangeStart: function(swiper) {
                                    for (var i = 0; i < swiper.slides.length; i++) {
                                          var slide = swiper.slides[i];
                                          console.info(slide.getElementsByTagName("img")[1]);
                                          slide.getElementsByTagName("img")[1].className="";
                                        }
                                    var img = swiper.activeSlide()
                                        .getElementsByTagName("img")[1];
                                    switch(swiper.activeIndex){
                                        case 0:
                                            $(img).addClass("animated tada");
                                            break;
                                        case 1:
                                            $(img).addClass("animated wobble");
                                            break;
                                        case 2:
                                            $(img).addClass("animated bounceInDown");
                                            break;
                                        case 3:
                                            $(img).addClass("animated flipInY");
                                            break;
                                        case 4:
                                            $(img).addClass("animated rollIn");
                                            break;
                                        default:
                                            $(img).addClass("animated rubberBand");
                                            break;
                                    }

                                },
                                onSlideChangeEnd: function(swiper) {

                                    var img = swiper.activeSlide()
                                        .getElementsByTagName("img")[1];

                                }
                            })
                            $(".loader").fadeOut(200);
                        }
                    });
                    $(".bottomArrow").css("left", window.innerWidth / 2 - 25 / 2);
                    $(".bt1").css("top",
                        window.innerHeight / 2 - $(".bt1").height() / 2);
                });
            $.ajax({
                url: "wap/weixin/api",
                async: false,
                dataType: 'json',
                data: {
                    url: encodeURIComponent(window.location.href.split('#')[0])
                },
                success: function(data) {
                    wx.config({
                        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                        appId: data.appid, // 必填,公众号的唯一标识
                        timestamp: data.timestamp, // 必填,生成签名的时间戳
                        nonceStr: data.nonceStr, // 必填,生成签名的随机串
                        signature: data.signature, // 必填,签名,见附录1
                        jsApiList: ["onMenuShareTimeline",
                                "onMenuShareAppMessage",
                                "onMenuShareQQ",
                                "onMenuShareWeibo",
                                "startRecord",
                                "stopRecord",
                                "onVoiceRecordEnd",
                                "playVoice",
                                "pauseVoice",
                                "stopVoice",
                                "onVoicePlayEnd",
                                "uploadVoice",
                                "downloadVoice",
                                "chooseImage",
                                "previewImage",
                                "uploadImage",
                                "downloadImage",
                                "translateVoice",
                                "getNetworkType",
                                "openLocation",
                                "getLocation",
                                "hideOptionMenu",
                                "showOptionMenu",
                                "hideMenuItems",
                                "showMenuItems",
                                "hideAllNonBaseMenuItem",
                                "showAllNonBaseMenuItem",
                                "closeWindow",
                                "scanQRCode",
                                "chooseWXPay",
                                "openProductSpecificView",
                                "addCard",
                                "chooseCard",
                                "openCard"
                            ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
                    });
                }
            });




            wx.ready(function() {
                // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
                var shareData = {
                    title: '糖库精品商城',
                    desc: '糖库精致礼品商城是一个专门搜集并出售最新,最精致,最美观的礼品的手机app,您可以在安卓,苹果,微信上浏览我们',
                    link: 'http://tang520.weiyibao.com/wap_download1.html',
                    imgUrl: 'http://tang520.weiyibao.com/img/logo.png'
                };
                wx.onMenuShareAppMessage(shareData);
                wx.onMenuShareTimeline(shareData);
                wx.onMenuShareQQ(shareData);
                wx.onMenuShareWeibo(shareData);

            });
        </script>

    </body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值