jQuery-UI mouse.js源码解读

2023-11-02 22:48

本文主要是介绍jQuery-UI mouse.js源码解读,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.总体架构

为两类事件提供接口,点击事件和拖拽事件,其中点击事件需要向元素添加preventClickEvent,以阻止默认的表单提交或文件上传事件发生。

两类事件都是通过向元素绑定mouseDown事件触发函数发生。

点击事件在_mouseDown函数中判断配置项中是否要求延时以及拖拽距离,如果没有,在_mouseDown函数中触发_mouseStart函数,并记录_mouseStarted(两次点击有效),第二次点击的时候调用_mouseUp函数,触发用户定义的_mouseStop函数,mouseStop函数里向元素添加preventClickEvent,该事件有过多疑问处;

拖拽事件在_mouseDown向文档节点绑定mousemove和mouseup事件,mouseMove事件中执行用户定义的_mouseStart函数,并记录_mouseStarted,方便第二次移动的时候直接判断该值,调用_mouseDrag函数,_mouseUp函数中执行_mouseStop函数。

 

2.插件的使用

配置项options

包括cancel(免于交互的element元素)、delay(规定的延时,在_mouseMove中判断该值,由结果决定_mouseDrag是否被触发)、distance(规定的移动距离,同样在_mouseMove中判断该值,由结果决定_mouseDrag是否被触发)

方法

_mouseDelayMet判断是否有延时;

_mouseDistanceMet判断移动距离是否符合要求;

_mouseInit绑定mousedown和click事件,click事件需要判断元素的preventClickEvent属性,由此决定是否执行;

_mouseDown将事件拆分成点击事件和拖拽事件两类,由配置项决定是否有函数被执行,点击事件触发_mouseUp和_mouseStart、_mouseStop函数,拖拽事件先向文档节点绑定mousemove、mouseup事件,再触发_mouseMove、_mouseUp和_mouseStart、_mouseDrag、_mouseStop函数;

_mouseMove拖拽事件触发_mouseStart、_mouseDrag函数;

_mouseUp触发_mouseStart函数;

接口

_mouseCapture返回否时意味着不触发交互事件;

_mouseDown鼠标按下时触发的函数;

_mouseDrag鼠标移动时触发的函数;

_mouseUp鼠标松开时触发的函数。

 

 http://api.jqueryui.com/mouse/

 

3.JS及jQuery的新认识

在类中的function函数中,使用self或that代替this,用来指代类,避免this值可能的变更;

closest()从当前元素开始,沿着dom树向上寻找,包含当前元素;

parentsUntil()向上遍历寻找父级元素,传入的参数是遍历的截止位置;

contents()查询文本节点和html元素节点,可以获取iframe内的元素节点,部分功能同find();

event.pageX.left/top鼠标偏离文档编辑器的坐标;

event.which鼠标左键值为1,中键为2,右键为3,没有按鼠标为0;

event.button判断鼠标按键,ie9开始和标准浏览器相同,左键为0,中键为1,右键为2,ie8左键为1,中键为4,右键为2。

 

// 使用自调用匿名函数将构建的function函数作为factory的实参,define未定义时使用require模块化工具,否则将jquery传入function函数中
( function( factory ) {if ( typeof define === "function" && define.amd ) {define( ["jquery","../ie","../version","../widget"], factory );} else {factory( jQuery );}
}( function( $ ) {// 各记录项mouseHandled、_mouseMoved、_mouseStarted在事件过程中记录为真,开始时为否则不执行该事件,事件结尾记录为否
// mouseHandled鼠标按下时判断,mousedown函数结尾处记录为真,阻止其他widget部件触发事件,鼠标松开时记录为否,代表执行完毕
// _mouseMoved鼠标按下时记录为否,鼠标移动时记录为真,处理间歇性的两次鼠标移动事件
/** * _mouseStarted当mouseStart返回值为真时记录为真,mouseUp事件开始的时候将其置为false,mousedrag执行前需要为真* _mouseDown函数中mouseStart启动需要无延时、无移动距离,鼠标按下触发mouseStart函数,直接跳转至mouseUp函数,mouseUp函数中执行mouseStop函数,没有执行mouseMove函数* _mouseMove函数中mouseStart启动时可以有延时,需要移动距离,鼠标移动时触发mouseMove函数,mouseMove函数中执行mouseStart、mouseDrag函数,最终跳转到mouseUp函数,并执行mouseStop函数* _mouseMove函数随着鼠标每移动一个像素点反复执行,*/
// mouseDelayMet为否时有延时,options.delay存在时延时触发,mouseDelayMet延时过程中赋值为真
var mouseHandled = false;
$( document ).on( "mouseup", function() {mouseHandled = false;
} );return $.widget( "ui.mouse", {version: "@VERSION",options: {cancel: "input, textarea, button, select, option",// 免于交互的元素distance: 1,delay: 0},// 绑定mouseDown事件,同时click不触发点击事件,this.started记录初始化是否完成// click事件包含mousedown和mouseup事件_mouseInit: function() {var that = this;this.element.on( "mousedown." + this.widgetName, function( event ) {return that._mouseDown( event );} ).on( "click." + this.widgetName, function( event ) {if ( true === $.data( event.target, that.widgetName + ".preventClickEvent" ) ) {$.removeData( event.target, that.widgetName + ".preventClickEvent" );// 包含阻止事件冒泡stopPropgation的功能之外,也阻止该元素绑定的同类型事件event.stopImmediatePropagation();return false;}} );this.started = false;},/** *  mouseDown函数中率先判断是否为无延时、无移动距离的事件,是则执行mouseStart函数,即鼠标按下时触发的函数*  不是则在文档节点上绑定mousemove、mouseup事件,先后触发mouseStart、mouseMove函数,即鼠标移动时触发的函数*/// mouseDestory函数用于在事件结尾处清空绑定在文档节点上的事件,问题是mouseUp函数中已经解除绑定???_mouseDestroy: function() {this.element.off( "." + this.widgetName );if ( this._mouseMoveDelegate ) {this.document.off( "mousemove." + this.widgetName, this._mouseMoveDelegate ).off( "mouseup." + this.widgetName, this._mouseUpDelegate );}},_mouseDown: function( event ) {if ( mouseHandled ) {return;}this._mouseMoved = false;// _mouseStarted两次点击的时候有有效值?促发_mouseUp函数???( this._mouseStarted && this._mouseUp( event ) );this._mouseDownEvent = event;var that = this,btnIsLeft = ( event.which === 1 ),// 鼠标左键// event.target.nodeName works around a bug in IE 8 with disabled inputs (#7620)// closest()从当前元素开始,沿着dom树向上寻找,包含当前元素// 不是鼠标左键点击,或者触发元素是表单元素及其子元素,或者_mouseCapture函数返回为否,则推出mouseDown函数,mouseCapture返回为否禁用交互事件elIsCancel = ( typeof this.options.cancel === "string" && event.target.nodeName ? $( event.target ).closest( this.options.cancel ).length : false );if ( !btnIsLeft || elIsCancel || !this._mouseCapture( event ) ) {return true;}// setTimeout回调函数的异步机制,后续代码块会先执行,即有延迟事件delay或规定距离distance的时候,后一条语句不被执行// 在function中,用that代替this,避免this值可能发生的变化this.mouseDelayMet = !this.options.delay;if ( !this.mouseDelayMet ) {this._mouseDelayTimer = setTimeout( function() {that.mouseDelayMet = true;}, this.options.delay );}// 点击事件触发时event怎样传入回调函数中???event的C++接口原理???// distance和delay均为0、mouseStart有返回值的情况,利用if语句执行mouseStart函数,阻止默认事件,如提交表单、按钮上传文件按钮// 语句的作用是设置鼠标按下时的事件,_mouseStart即是_mousedown事件触发时执行的函数,取代默认的点击事件if ( this._mouseDistanceMet( event ) && this._mouseDelayMet( event ) ) {this._mouseStarted = ( this._mouseStart( event ) !== false );if ( !this._mouseStarted ) {event.preventDefault();return true;}}/*** mouse.js将事件分为两类,一类是鼠标按下又松开,和点击事件起冲突,需要记录preventClickEvent阻止默认的点击事件,这一类事件触发的函数是mouseDown、mouseUp、mouseStart、mouseStop* 另一类是鼠标按下后移动了一段距离再松开,和点击事件没有冲突,不需要preventClickEvent阻止默认的点击事件,这一类事件触发的函数是mouseDown、mouseMove、mouseUp、mouseStart、mouseDrag、mouseStop*/if ( true === $.data( event.target, this.widgetName + ".preventClickEvent" ) ) {$.removeData( event.target, this.widgetName + ".preventClickEvent" );}this._mouseMoveDelegate = function( event ) {return that._mouseMove( event );};this._mouseUpDelegate = function( event ) {return that._mouseUp( event );};this.document.on( "mousemove." + this.widgetName, this._mouseMoveDelegate ).on( "mouseup." + this.widgetName, this._mouseUpDelegate );event.preventDefault();mouseHandled = true;return true;},_mouseMove: function( event ) {	//随着鼠标移动反复执行// 怎样判断ie中鼠标越出文档范围,以及safari的跨iframe拖拽的问题???// Only check for mouseups outside the document if you've moved inside the document// at least once. This prevents the firing of mouseup in the case of IE<9, which will// fire a mousemove event if content is placed under the cursor. See #7778// Support: IE <9if ( this._mouseMoved ) {// IE mouseup check - mouseup happened when mouse was out of window// documentMode文档兼容模式,低版本中没有,判断ie渲染页面的方式,也即ie的版本// event.button判断鼠标按键,ie9开始和标准浏览器相同,左键为0,中键为1,右键为2,ie8左键为1,中键为4,右键为2if ( $.ui.ie && ( !document.documentMode || document.documentMode < 9 ) && !event.button ) {return this._mouseUp( event );// Iframe mouseup check - mouseup occurred in another document} else if ( !event.which ) {// Support: Safari <=8 - 9// Safari sets which to 0 if you press any of the following keys// during a drag (#14461)// event.originalEvent保存了浏览器原生事件对象,是jquery的机制,altkey指alt键被点击且保持状态if ( event.originalEvent.altKey || event.originalEvent.ctrlKey ||event.originalEvent.metaKey || event.originalEvent.shiftKey ) {this.ignoreMissingWhich = true;} else if ( !this.ignoreMissingWhich ) {return this._mouseUp( event );}}}// event.which鼠标左键值为1,中键为2,右键为3,没有按鼠标为0,mouseDown函数中有要求event.which为1???if ( event.which || event.button ) {this._mouseMoved = true;}// 鼠标未松开时第二次移动时,_mouseStarted和_mouseMoved记录在案,方便调用if ( this._mouseStarted ) {this._mouseDrag( event );return event.preventDefault();}// 移动距离、延迟时间合乎规范的时候调用mouseStart和mouseDrag函数,延迟时间从鼠标按下时计时,定时mouseDelayMet变为真if ( this._mouseDistanceMet( event ) && this._mouseDelayMet( event ) ) {this._mouseStarted =( this._mouseStart( this._mouseDownEvent, event ) !== false );( this._mouseStarted ? this._mouseDrag( event ) : this._mouseUp( event ) );}return !this._mouseStarted;},_mouseUp: function( event ) {this.document.off( "mousemove." + this.widgetName, this._mouseMoveDelegate ).off( "mouseup." + this.widgetName, this._mouseUpDelegate );if ( this._mouseStarted ) {this._mouseStarted = false;// 鼠标按下又松开的事件,没有向文档节点绑定事件,mouseDown和mouseUp事件中的事件元素相同,鼠标移动事件绑定了文档节点if ( event.target === this._mouseDownEvent.target ) {$.data( event.target, this.widgetName + ".preventClickEvent", true );}this._mouseStop( event );}if ( this._mouseDelayTimer ) {clearTimeout( this._mouseDelayTimer );delete this._mouseDelayTimer;}this.ignoreMissingWhich = false;mouseHandled = false;event.preventDefault();},// 当前事件的坐标和鼠标按下时的坐标相比较得出水平及垂直距离_mouseDistanceMet: function( event ) {return ( Math.max(Math.abs( this._mouseDownEvent.pageX - event.pageX ),Math.abs( this._mouseDownEvent.pageY - event.pageY )) >= this.options.distance);},_mouseDelayMet: function( /* event */ ) {return this.mouseDelayMet;},// _mouseCapture决定交互是否应该基于交互的事件目标开始,返回为否的时候禁用交互事件// _mouseStart处理交互的开始,_mouseDrag处理鼠标移动事件,_mouseStop交互的结束_mouseStart: function( /* event */ ) {},_mouseDrag: function( /* event */ ) {},_mouseStop: function( /* event */ ) {},_mouseCapture: function( /* event */ ) { return true; }
} );} ) );

这篇关于jQuery-UI mouse.js源码解读的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/333827

相关文章

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

html 滚动条滚动过快会留下边框线的解决方案

《html滚动条滚动过快会留下边框线的解决方案》:本文主要介绍了html滚动条滚动过快会留下边框线的解决方案,解决方法很简单,详细内容请阅读本文,希望能对你有所帮助... 滚动条滚动过快时,会留下边框线但其实大部分时候是这样的,没有多出边框线的滚动条滚动过快时留下边框线的问题通常与滚动条样式和滚动行

Nacos注册中心和配置中心的底层原理全面解读

《Nacos注册中心和配置中心的底层原理全面解读》:本文主要介绍Nacos注册中心和配置中心的底层原理的全面解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录临时实例和永久实例为什么 Nacos 要将服务实例分为临时实例和永久实例?1.x 版本和2.x版本的区别

使用vscode搭建pywebview集成vue项目实践

《使用vscode搭建pywebview集成vue项目实践》:本文主要介绍使用vscode搭建pywebview集成vue项目实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录环境准备项目源码下载项目说明调试与生成可执行文件核心代码说明总结本节我们使用pythonpywebv

C++类和对象之默认成员函数的使用解读

《C++类和对象之默认成员函数的使用解读》:本文主要介绍C++类和对象之默认成员函数的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、默认成员函数有哪些二、各默认成员函数详解默认构造函数析构函数拷贝构造函数拷贝赋值运算符三、默认成员函数的注意事项总结一

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

MySQL的ALTER TABLE命令的使用解读

《MySQL的ALTERTABLE命令的使用解读》:本文主要介绍MySQL的ALTERTABLE命令的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、查看所建表的编China编程码格式2、修改表的编码格式3、修改列队数据类型4、添加列5、修改列的位置5.1、把列

Linux CPU飙升排查五步法解读

《LinuxCPU飙升排查五步法解读》:本文主要介绍LinuxCPU飙升排查五步法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录排查思路-五步法1. top命令定位应用进程pid2.php top-Hp[pid]定位应用进程对应的线程tid3. printf"%