`

YUI3 event小结

 
阅读更多

其它事件,参考:http://yuilibrary.com/yui/docs/event/

use("______", ...) What's in it?
event-base

The core DOM event subscription system as well as the DOM lifecycle eventsdomreadycontentready, and available. Notably, it does NOT include

  • event delegation
  • event simulation
  • synthetic events

If you've use()d anything, you probably have this already.

event

event定义在event module中,除了

event-simulate

 

node-event-simulate

 

node-event-delegate

这三个模块中外,其它模板都包含event

 

 

event module提供了如下几个类:详情参考http://yuilibrary.com/yui/docs/api/modules/event.html

 

 

event-delegate & 
node-event-delegate
事件委托和节点委托功能
event-simulate & 
node-event-simulate

模拟事件

Note: 伪造事件不应该用在面向用户的代码

event-synthetic

事件合成

event-flick 增加了一个“甩尾”事件或触摸鼠标交互
event-focus blur和focus不支持冒泡事件的,event-focus这个模块是让focus和blur支持冒泡事件?
event-gestures

事件的手势,如gesturemove,touchstart等

在发下模块中:

  • "event-touch"
  • "event-move"
  • "event-flick"

In the future, may contain more gesture abstraction modules.

event-hover 增加了一个“悬停事件,一个用于启动和一个结束
event-key 增加了一个“键盘”事件,当键被按下时
event-mouseenter 增加一个 "mouseenter" and "mouseleave" 事件,相当于 "mouseover" 和"mouseout".
event-mousewheel

鼠标滚轮事件,目前,这一事件只能认购y.on(“mousewheel回调函数)

event-move

增加了“gesturemovestart(手势行动的开始gesturemove手势行动的)和“gesturemoveend手势行动的结束事件,

根据客户设备作为抽象在鼠标和触摸等事件

event-outside 外部事件
  • blur
  • change
  • click
  • dblclick
  • focus
  • keydown
  • keypress
  • keyup
  • mousedown
  • mousemove
  • mouseout
  • mouseover
  • mouseup
  • select
  • submit

也可以定义外部事件,Y.Event.defineOutside(eventType),Y.Event.defineOutside(eventType, "yonderclick").

 

event-resize

增加了一个“windowresize事件,只有fire()后,用户已停止拖动窗口的大小调整手柄这种标准化的window.onresize事件跨越浏览器

这样调用: Y.on("windowresize", callback);

使用Y.on订阅一个自定义事件,通过调用Y.fire方法可以在程序中触发自定义事件。

event-touch 触摸事件
event-valuechange

增加了一个“valuechange”事件触发时,输入元素文本的变化

valuechange事件时引发的价值属性文本输入字段或变化的结果,一个按键鼠标操作输入法编辑器拼音输入事件

 

 

使用Y.on订阅一个自定义事件,通过调用Y.fire方法可以在程序中触发自定义事件。

 

  1. YUI().use('event-custom'function(Y) {  
  2.    Y.on('customapp:started'function() {  
  3.       alert('YUI 3');  
  4.     
  5.   });  
  6.   
  7.     Y.fire('customapp:started');  
  8. });  

 

 

EventFacade相关属性
EventFacade包装原始的event对象,在DOM事件及自定义事件中作为第一个参数传递给回调函数。
关于DOM事件的event属性可以参考w3c相关讲解 
本节主要说明EventFacade的几个常用属性和方法:
Y.on('click',function(e){
this; //注册事件的对象 ;如果是委托事件,this 不会代表委托对象,而只表示被委托的单个对象。
  //如果是NodeList注册,返回NodeList集合对象; 
e.target;          //触发事件的对象(在click事件中即被点击的对象,并不一定是注册的对象)
e.currentTarget ;   // 注册事件的对象;如果是委托事件,也只表示被委托的单个对象。
 // this不同,如果是NodeList注册,返回集合中被点击的对象  
e.container;          // 委托对象 ,非委托事件 返回 undefined
  
e.stopPropagation(); //停止事件冒泡。
e.preventDefault();  //阻止事件默认行为。
e.halt(); //相当于调用stopPropagation() 和 preventDefault()

}

once用法:

当加载dom完成后,只执行一次事件,和on的使用格式一样:
下面的例子,当点击test时,会弹出test,再点击时showMsg就不会被执行,而showMsg1却可执行无限次
<script type="text/javascript">
   YUI().use("node",function(Y){
	   function showMsg(){ alert('test');}
	    function showMsg1(){ alert('test1');}
       Y.one('#demo').once('click',showMsg);
	   Y.one('#demo').on('click',showMsg1);
     
   });
</script>
  <div id="demo" style="cursor:pointer;">test</div>
 
  另外:传递一个数组事件可以调用相同的函数
inputNode.on(['focus', 'mouseover'], activate);

对象可订阅多个事件每个都有自己的回调函数

function validate(e) { ... }
function clearPlaceholder(e) { ... }

var groupSub = inputNode.on({
    blur    : validate,
    keypress: validate,
    focus   : clearPlaceholder
});
groupSub.detach();//删除所有监听事件
事件分类node('myclass|click',callback)
node.on('foo-category|click', callback);

node.detach('foo-category|click');
// OR
node.detach('foo-category|*')
创建dom自定义事件:
Y.Event.define(type, config)

Y.Event.define("tripleclick", {

    // The setup logic executed when node.on('tripleclick', callback) is called
    on: function (node, subscription, notifier) {
        // supporting methods can be referenced from `this`
        this._clear(subscription);

        // To make detaching easy, a common pattern is to add the subscription
        // for the supporting DOM event to the subscription object passed in.
        // This is then referenced in the detach() method below.
        subscription._handle = node.on('click', function (e) {
            if (subscription._timer) {
                subscription._timer.cancel();
            }

            if (++subscription._counter === 3) {
                this._clear(subscription);

                // The notifier triggers the subscriptions to be executed.
                // Pass its fire() method the triggering DOM event facade
                notifier.fire(e);
            } else {
                subscription._timer =
                    Y.later(300, this, this._clear, [subscription]);
            }
        });
    },

    // The logic executed when the 'tripleclick' subscription is `detach()`ed
    detach: function (node, subscription, notifier) {
        // Clean up supporting DOM subscriptions and other external hooks
        // when the synthetic event subscription is detached.
        subscription._handle.detach();

        if (subscription._timer) {
            subscription._timer.cancel();
        }
    },

    // Additional methods can be added to support the lifecycle methods
    _clear: function (subscription) {
        subscription._counter = 0;
        subscription._timer = null;
    },
    
    ...
});
调用:Y.one('#hellokitty').on('tripleclick', omgYayCantClickEnough);
分享到:
评论

相关推荐

    yui3-master.zip

    yui3-master.zip

    YUI3 dialog组件

    基于YUI3的dialog组件该组件是基于YUI3开发的,功能强大,详细见http://www.qiqicartoon.com

    YUI 入门教程YUI 入门教程YUI 入门教程

    YUI教程YUI 入门教程YUI 入门教程YUI 入门教程

    从YUI2到YUI3看前端的演变 pdf

    YUI3 引入了粒度更细的模块管理方式,通过异步 HTTP 请求加载模块、然后执行回调来加载和使用模块。现场有很多人提出疑问,大家无非关心的是“效率”二字。个人以为在现阶段,这种方式有一点激进,否能为广大用户所...

    从YUI2到YUI3看前端的演变

    从YUI2到YUI3看前端的演变

    YUI3 完整包

    Yahoo! UI Library (YUI) 是一个开放源代码的 JavaScript 函数库,为了能建立一个高互动的网页,它采用了AJAX, DHTML 和 DOM 等程式码技术。它也包含了许多 CSS 资源。

    yui 3.1.2 源码 6MB大小 0资源分

    YUI3 源码 非compress版 YUI3 源码 非compress版 YUI3 源码 非compress版

    yui3-3.17.2最新版

    yui对于开发者来说是绝对的好用,开发者福利,特献上最新版

    yuicompressor-yui compressor

    yuicompressor-2.4.2.jar yuicompressor-2.4.7.jar jsZip.exe yuicompressor yui compressor js压缩工具 javascript压缩工具 css压缩工具 ------------------------------------ //压缩JS java -jar yui...

    yui_2.9.0前端UI

    YUI 库,全称Yahoo! UI Library。是一组工具和控件,用JavaScript写成, 为的是用DOM 脚本,DHTML和AJAX等技术创建丰富的网页交互式应用程序。 YUI 基于BSD协议,对所有的使用方式都是免费的。YUI 项目包括YUI 库和两...

    YUI3.6文档及示例

    yui官网下载的。内容很全,示例+doc说明

    yuitest YUI测试工具

    YUI Test is a complete testing framework for JavaScript and Web applications. You can use the simple JavaScript syntax to write unit tests that can be run in web browsers or on the command line, as ...

    yui_2.6.0r2

    yui_2.6.0r2 yui_2.6.0r2 yui_2.6.0r2 yui_2.6.0r2 yui_2.6.0r2

    YUI3 中tree的两种实现

    NULL 博文链接:https://ttwang.iteye.com/blog/1741631

    高效WEB前端开发之路:YUI3.15

     本书作者便是在此背景下,以国外最优秀的JavaScript框架之一——Yahoo User Interface Library(简称YUI)的最新版本YUI 3.15为基础编写而成。本书通过通俗易懂的语言和大量丰富的实例,帮助读者解决实际生产环境...

    YUI中文文档CHM

    YAHOO YUI 中文文档 AJAX 详细 比较好用

    经典的YUI 示例中文文档

    里面包含了YUI的所有实例(element、event等一系列的实例)

    yui 资源包

    yui 源码下载,3.9.0 r2 包,最新版本

    yuicompressor,给YUI Compressor添加右键命令

    YUI Compressor非常好用,特别是JS的混淆是众多JS Coding的最爱。可惜官网提供的版本都不具备右键功能,每次压缩都要cmd输入一些命令实在是繁琐,本文就介绍如何给YUI Compressor添加右键命令,方便使用。 网上已有...

Global site tag (gtag.js) - Google Analytics