原生javascript编写手机端H5滑动效果

主要用到touchstart 、touchmove、touchend三个函数的编写。
效果如下:

这里写图片描述


**代码:**


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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta content="telephone=yes" name="format-detection" />
    <meta name="apple-mobile-web-app-status-bar-style" content="white">
    <meta name="x5-fullscreen" content="true">
    <meta name="apple-touch-fullscreen" content="yes">
    <title>H5滑动banner效果</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            height: 200px;
            width: 100%;
            overflow: hidden;
        }

        .movebox {
            height: 200px;
            width: 9000px;
            padding: 0;
            position: relative;
            left: 0;
        }

        .movebox li {
            height: 200px;
            float: left;
            list-style: none;
            font-size: 30px;
            color: #fff;
        }
    </style>
    <script>
        window.onload = function () {

            var moveX, //手指滑动距离
                endX, //手指停止滑动时X轴坐标
                cout = 0, //滑动计数器
                moveDir; //滑动方向
            var movebox = document.querySelector(".movebox"); //滑动对象
            var Li = movebox.querySelectorAll("li"); //滑动对象item
            var width = parseInt(window.getComputedStyle(movebox.parentNode).width); //滑动对象item的宽度

            movebox.style.width = (width * 4) + "px"; //设置滑动盒子width
            for (var i = 0; i < Li.length; i++) {
                Li[i].style.width = width + "px"; //设置滑动item的width,适应屏幕宽度
            }

            //触摸开始
            function boxTouchStart(e) {
                var touch = e.touches[0]; //获取触摸对象
                startX = touch.pageX; //获取触摸坐标
                endX = parseInt(movebox.style.webkitTransform.replace("translateX(", "")); //获取每次触摸时滑动对象X轴的偏移值
            }

            function boxTouchMove(e) {
                var touch = e.touches[0];
                moveX = touch.pageX - startX; //手指水平方向移动的距离

                if (cout == 0 && moveX > 0) { //刚开始第一次向左滑动时
                    return false;
                }

                if (cout == 3 && moveX < 0) { //滑动到最后继续向右滑动时
                    return false;
                }

                movebox.style.webkitTransform = "translateX(" + (endX + moveX) + "px)"; //手指滑动时滑动对象随之滑动
            }

            function boxTouchEnd(e) {
                moveDir = moveX < 0 ? true : false; //滑动方向大于0表示向左滑动,小于0表示向右滑动
                //手指向左滑动
                if (moveDir) {

                    if (cout < 3) {
                        movebox.style.webkitTransform = "translateX(" + (endX - width) + "px)";
                        cout++;
                    }
                    //手指向右滑动
                } else {
                    //滑动到初始状态时返回false
                    if (cout == 0) {
                        return false;
                    } else {
                        movebox.style.webkitTransform = "translateX(" + (endX + width) + "px)";
                        cout--;
                    }
                }
            }

            //滑动对象事件绑定
            movebox.addEventListener("touchstart", boxTouchStart, false);
            movebox.addEventListener("touchmove", boxTouchMove, false);
            movebox.addEventListener("touchend", boxTouchEnd, false);
        }
    </script>
</head>

<body style="position:absolute;width:100%;overflow:hidden;">
    <div class="box">
        <ul class="movebox" style="transition-duration:0.2s;transform: translateX(-0px);">
            <li style="background:red;">1</li>
            <li style="background:yellow">2</li>
            <li style="background:blue">3</li>
            <li style="background:green">4</li>
        </ul>
    </div>
</body>

</html>
http://blog.youkuaiyun.com/xiaoxiao108/article/details/8913616 html5 挺火,写个html5游戏玩玩吧,想起cocos2d 貌似各个平台都有,网上找了找,下载了个Cocos2d-html5引擎包。 貌似另一个开源引擎lufylegend.js也很好,下次用用lufylegend.js试试。 开发环境 chrome Cocos2d-html5 游戏地址:http://hehe108.sinaapp.com/cocos2d/ (adsw回车) 实现方法如下 1.创建好 LayerGradient的子类 (里面放坦克子弹) 2.重写 onEnter 方法添加一些基本按钮 跟一些初始坦克,子弹 3.通过schedule方法 控制 坦克 子弹的重画 4.根据键盘按键(ASWD)确定出坦克的方向,根据坦克的方向修改坦克的X,Y轴坐标,来实现坦克的移动 5.通过cc.rectIntersectsRect函数来进行碰撞检测,实现子弹打击坦克 具体代码 1.在项目里面添加方向 var Direction = { L:0, U:1, D:2, R:3, STOP:4 }; 2.添加子弹类的相关属性 SPEED:10, WIDTH:15, HEIGHT:15, x:null, y:null, dir:null, live:true, tankClient:null, //LayerGradient子类TankClient 的引用 good:null, 3.子弹初始化,重画 ctor:function (x,y,good,dir,tankClient) { cc.associateWithNative( this, cc.Sprite ); this.x=x; this.y=y; this.dir=dir; this.tankClient=tankClient; this.good=good; this.initWithFile(s_missile); this.setPosition( cc.p(this.x, this.y) ); this.tankClient.addChild(this); }, Draw:function(){ if(!this.live){ this.tankClient.removeChild(this, true); return; } this.setPosition( cc.p(this.x, this.y) ); this.Move(); }, 4.添加子弹打击坦克的方法 HitTank:function(t){ if (cc.rectIntersectsRect(this.GetRectangle(), t.GetRectangle()) && t.live && this.live && this.good != t.good){ t.live = false; this.live = false; return true; } return false; }, 5.添加坦克类相关属性 SPEED:5, WIDTH:58, HEIGHT:58, x:0, y:0, l:false, u:false, r:false, d:false, dir:Direction["STOP"], ptDir:Direction["D"], tankClient:null, good:null, step:0, live:true, 6.在tank类中 坦克初始化,重画 ctor:function (x,y,good,tankClient) { cc.associateWithNative( this, cc.Sprite ); this.x=x; this.y=y; this.tankClient=tankClient; this.good=good; if(good){ this.initWithFile(s_tank); }else{ this.initWithFile(s_enemy); } this.setPosition( cc.p(this.x, this.y) ); this.tankClient.addChild(this); }, Draw:function(){ if (!this.live){ if (!this.good){ this.tankClient.removeChild(this, true); } this.tankClient.removeChild(this, true); return; } this.setPosition( cc.p(this.x, this.y) ); switch (this.ptDir) { case Direction["D"]: this.setRotation(0); //旋转精灵控制 炮筒方向 break; case Direction["U"]: this.setRotation(180); break; case Direction["L"]: this.setRotation(270); break; case Direction["R"]: this.setRotation(90); break; } this.Move(); }, 7.tank发子弹的方法 Fire:function() { if(!this.live) return null; for(var i=0;i<this.tankClient.missiles.length;i++){ var m = this.tankClient.missiles[i]; if (m.live == false){ m.x=this.x; m.y=this.y; m.live=true; m.dir=this.ptDir; m.good=this.good; this.tankClient.addChild(m); return m; } } var missile=new Missile(this.x,this.y,this.good,this.ptDir,this.tankClient); this.tankClient.missiles.push(missile); return missile; }, 8.LayerGradient加入坦克 this.tanks = []; this.myTank = new Tank(60,20, true, this); for (var i = 0; i < 10; i++){ this.tanks.push(new Tank(50 + 70 * (i + 1), 420, false, this)); } 9.LayerGradient中调用子弹打击坦克的方法 for(var i=0;i<this.missiles.length;i++){ var m = this.missiles[i]; m.HitTank(this.myTank); m.HitTanks(this.tanks); m.Draw(); } 10.控制坦克移动射击的部分代码 onKeyUp:function(key) { this.myTank.KeyReleased(key); }, onKeyDown:function(key) { this.myTank.KeyPressed(key); } 11.用Ant和compiler合并压缩js后发布到sae 如果你发现有什么不合理的,需要改进的地方,请留言。或者可以通过 328452421@qq.com 联系我,非常感谢。 http://blog.youkuaiyun.com/xiaoxiao108/article/details/8913616
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值