Zepto实现滑动核销效果

本文介绍了一个基于Zepto.js的滑动手势实现方案,通过监听touchstart、touchmove和touchend事件来实现页面元素的旋转与滑动效果。该方案利用CSS3的transform属性完成旋转动画,并在滑动超过预设阈值时使页面元素滑落。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要事件:touchmove touchstart touchend

0、定义常量值(滑动 横向记录差 distance,手指离开屏幕位置与开始滑动位置;刮开角度 :angle 当距离差到达时的翻转角度,过程中为0-angle)

1、记录touchstart 的 pageX :originX

2、绑定touchmove事情,对 实时pageX :X ,

3、比较X-originX,同时根据常量值计算 偏转角度(角度为-),进行transform rotate的 css3 旋转。

当差值达到常量distance时,滑动页滑落(可设置top值并赋予动画效果 掉落至屏幕底部)

4、若差值未到,则在touchend事件后,重置角度为rotate 0deg

伪代码如下:

var $ = require('zepto'),

    Rive = function(option){

        this.container = $(option.container);
        this.callback = option.callback;
        this.ajax = true;

        var X, reduce, distance = 150, angle= 30,_this = this;
        var body = $('body')
        body.on('touchstart',function(event){
            X = event.touches[0].pageX;
        });
        this.container.on('touchmove',function(event){

            event.preventDefault();
            var x = event.touches[0].pageX;
            reduce = x - X;
            var animateTo = - angle /distance* reduce;

            if(reduce > 0){
                _this.container.css({
                    'transform': 'rotate('+ animateTo +'deg)',
                    '-ms-transform': 'rotate('+ animateTo +'deg)',
                    '-moz-transform': 'rotate('+ animateTo +'deg)',
                    '-webkit-transform': 'rotate('+ animateTo +'deg)',
                    '-o-transform': 'rotate('+ animateTo +'deg)',
                    'transition': 'all 0.5s ease-out'
                });
                if(reduce > distance ){
                    if(_this.ajax) {
                        _this.ajax = false;
                        _this.container.css({
                            'top': '200px',
                            'opacity': '0',
                            'transition': 'all 0.7s ease-out'
                        })
                        _this.callback && _this.callback();
                    };
                }
            }
        });
        body.on('touchend', function(){
            _this.ajax = true;
            if (reduce > distance) {
                _this.container.css({
                    'transition': 'all 0 ease 0'
                });
            } else {
                _this.container.css({
                    'transform': 'rotate(0deg)',
                    '-ms-transform': 'rotate(0deg)',
                    '-moz-transform': 'rotate(0deg)',
                    '-webkit-transform': 'rotate(0deg)',
                    '-o-transform': 'rotate(0deg)',
                    'transition': 'all 0 ease 0',
                    'top': '0'
                });
            };
        });

    }, p = Rive.prototype;

    p.reset = function () {
        this.container.css({
            'transform': 'rotate(0deg)',
            '-ms-transform': 'rotate(0deg)',
            '-moz-transform': 'rotate(0deg)',
            '-webkit-transform': 'rotate(0deg)',
            '-o-transform': 'rotate(0deg)',
            'transition': 'all 0 ease 0',
            'top': '0',
            'opacity': '1'
        });
        this.ajax = true;
    };
    
module.exports = Rive;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值