节点门面和自定义事件研究

借点击和轻触来研究节点门面和自定义事件
当支持轻触时用轻触,否则使用点击

// 初始版本
// 使用自定义事件增加 tap 和tapend  事件
// 当支持轻触是用轻触 ,否则用点击 
function addTapListener(node, callback) {

    //start by supporting mousevents
    var startEvent = 'mousedown', endEvent = 'mouseup';

    //if touch events are available use them instead
    if (typeof(window.ontouchstart) != 'undefined') {
        //touch events work
        startEvent = 'touchstart';
        endEvent   = 'touchend';
    }
    //添加自定义事件 tap, 后续可以像绑定click事件一样绑定
    node.addEventListener(startEvent, function(e) {
        var tap = document.createEvent('CustomEvent');
        tap.initCustomEvent('tap', true, true, null);
        node.dispatchEvent(tap);
    }); 
    //添加自定义事件 tapend, 后续可以像绑定click事件一样绑定
    node.addEventListener(endEvent, function(e) {
        var tapend = document.createEvent('CustomEvent');
        tapend.initCustomEvent('tapend', true, true, null);
        node.dispatchEvent(tapend);
    })
    //绑定 tap 事件
    node.addEventListener('tap', callback);
}
//调用addTapListener 方法 添加tap 和 tapend事件
addTapListener(document.getElementById('toggle'), function(e){
    e.preventDefault();
    e.target.className = 'active button';
    togglePicture();
});
//给元素添加tapend事件
node.addEventListener('tapend', function(e){
    e.preventDefault();
    e.target.className = "button";
});
//
//优化版本 使用节点门面
(function(){

    var TOUCHSTART, TOUCHEND;

    //normal touch events
    if (typeof(window.ontouchstart) != 'undefined') {

        TOUCHSTART = 'touchstart';
        TOUCHEND   = 'touchend';

    //microsoft touch events
    } else if (typeof(window.onmspointerdown) != 'undefined') {
        TOUCHSTART = 'MSPointerDown';
        TOUCHEND   = 'MSPointerUp';
    } else {
        TOUCHSTART = 'mousedown';
        TOUCHEND   = 'mouseup';
    }


    function NodeFacade(node){

        this._node = node;

    }

    NodeFacade.prototype.getDomNode = function() {
        return this._node;
    }
    //使用原型 绑定on方法
    NodeFacade.prototype.on = function(evt, callback) {

        if (evt === 'tap') {
            this._node.addEventListener(TOUCHSTART, callback);
        } else if (evt === 'tapend') {
            this._node.addEventListener(TOUCHEND, callback);
        } else {
            this._node.addEventListener(evt, callback);
        }

        return this;

    }
    //使用原型 绑定off方法
    NodeFacade.prototype.off = function(evt, callback) {
        if (evt === 'tap') {
            this._node.removeEventListener(TOUCHSTART, callback);
        } else if (evt === 'tapend') {
            this._node.removeEventListener(TOUCHEND, callback);
        } else {
            this._node.removeEventListener(evt, callback);
        }

        return this;
    }
    //使用门面模式,模拟jquery的写法
    window.$ = function(selector) {

        var node = document.querySelector(selector);

        if(node) {
            return new NodeFacade(node);
        } else {
            return null;
        }
    }


})();
//链式绑定
$('.button').on('tap', function(e) {
    e.preventDefault();
    togglePicture();
    e.target.className = "active button";
}).on('tapend', function(e) {
    e.target.className = "button";
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值