<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>jQuery | ユガラボ</title>
	<atom:link href="https://yugalab.net/archives/tag/4003_1_jquery/feed" rel="self" type="application/rss+xml" />
	<link>https://yugalab.net</link>
	<description>pursue the beauty with yuga lab.</description>
	<lastBuildDate>Tue, 20 Jan 2015 07:01:40 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.7.7</generator>
	<item>
		<title>【jQuery】コピペOK！タッチ操作とマウス操作の動作定義をまとめる</title>
		<link>https://yugalab.net/archives/289</link>
					<comments>https://yugalab.net/archives/289#respond</comments>
		
		<dc:creator><![CDATA[ユーリ]]></dc:creator>
		<pubDate>Fri, 28 Nov 2014 11:00:08 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[javascript]]></category>
		<guid isPermaLink="false">http://yugalab.net/?p=289</guid>

					<description><![CDATA[<p>さいきん、スマホでもPCでも同じような動作を求められるWebAPIが増えていますよね。 動作はjQueryで書くことになるのですが、もともと用意されているイベント(click,mousemoveなど)ではタッチ操作を拾い [&#8230;]</p>
<p>The post <a href="https://yugalab.net/archives/289">【jQuery】コピペOK！タッチ操作とマウス操作の動作定義をまとめる</a> first appeared on <a href="https://yugalab.net">ユガラボ</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>さいきん、スマホでもPCでも同じような動作を求められるWebAPIが増えていますよね。<br /> 動作はjQueryで書くことになるのですが、もともと用意されているイベント(click,mousemoveなど)ではタッチ操作を拾いきれないことがあります。<br /> そこで、二つの環境の動作を同じ関数にまとめる定義を作りました。</p>
<div class="fold_post"><br /> まず、こちらがサンプルボタンです！PCでもスマホでも共通のコードで動作するところがポイントです！
<div id="demo_button_20141127_A" style="text-align: center; border: 1px solid #666; border-radius: 6px; width: 50%; padding: 16px; margin: 10px auto; background-color: white; box-shadow: 2px 2px 3px #666;">demo</div>
<br /> 基本のクリック動作、押下、移動、キャンセルの４種を定義できます。<br /> <br /> サンプルコードです！<br /> 動作定義部分と、jQuery拡張部分の二つに分けてご紹介します。<br /> <br /> まずは、動作定義部分です。<br /> 上に置いてあるボタンは、こちらのコードで動作しています。<br />
<pre class="brush:js" title="(前半)動作定義部分">$(function(){
	$('#hoge').touchInterface(
	function(e,$_this){ //タッチorクリックの動作
		$_this.empty().append('action:up/end');
		$_this.css('background-color','#FFF');
	},
	function(e,$_this){ //(オプション)押下
		$_this.empty().append('action:down/start');
		$_this.css('background-color','#AAF');
	},
	function(e,$_this){ //(オプション)移動
		$_this.empty().append('action:move');
		$_this.css('background-color','#AFA');
	},
	function(e,$_this){ //(オプション)範囲外
		$_this.empty().append('action:out/cancel');
		$_this.css('background-color','#FFA');
	}
	);
});</pre>
はい、わりとシンプルな作りですね！<br /> jQueryエレメントの挙動を拡張して&#8221;touchInterface&#8221;というアクションを命令し、<br /> その引数として関数を４つ渡しています。<br /> まとめるとこんな感じです。<br />
<div class="putit_column">$(<span style="color: red;">element</span>).<span style="color: blue;">touchInterface</span>(<span style="color: green;">up</span>,<span style="color: green;">[down]</span>,<span style="color: green;">[move]</span>,<span style="color: green;">[out]</span>);<br /> 引数はすべてfunction(関数)です。<br /> また、2〜4つ目の引数は省略できます。</div>
<br /> 関数内では、<span class="l_blue"> $(this) </span>という表記が使えない為、代わりに<span class="l_blue"> $_this </span>を使用して下さい。<br /> また、タッチ位置などを使いたい場合は、<span class="l_blue">e </span>の中から引っ張って下さい。<br /> <br /> 続いて、肝心のjQuery拡張部分です。<br /> jQueryの機能を拡張して、「touchInterface」というアクションを定義しています。<br />
<pre class="brush:js" title="(後半)jQuery拡張部分">$.fn.extend({
	touchInterface : function(up){
		var down	= (arguments.length &gt; 1 ) ? arguments[1] : false;
		var move	= (arguments.length &gt; 2 ) ? arguments[2] : false;
		var cancel	= (arguments.length &gt; 3 ) ? arguments[3] : false;
		return $(this).on({
			'touchstart mousedown'	: function(e){
				e.preventDefault();
				this.touching = true;
				if(down)down(e,$(this));
			},
			'touchmove mousemove'	: function(e) {
			 	if(!this.touching)return;
				e.preventDefault();
				if(move)move(e,$(this));
				if('ontouchstart' in window){
					var touch = e.originalEvent.changedTouches[0];
					var offset = $(this).offset();
					if(	touch.pageX &lt; offset.left || touch.pageX &gt; $(this).outerWidth()  + offset.left ||
						touch.pageY &lt; offset.top  || touch.pageY &gt; $(this).outerHeight() + offset.top  ){
						this.touching = false;
						if(cancel)cancel(e,$(this));
					}
				}
			},
			'touchcancel mouseout'	: function(e){
				if(!this.touching)return;
				e.preventDefault();
				if(cancel)cancel(e,$(this));
				this.touching = false;
			},
			'touchend mouseup'		: function(e){
				if(!this.touching)return;
				e.preventDefault();
				up(e,$(this));
				this.touching = false;
			}
		}).css('cursor','pointer');
	}
});</pre>
上記のjQuery拡張部分のコードは、JavaScriptソース内のjQuery動作定義部分の外側に記述してください。<br /> もちろんjQuery本体も忘れずに読み込んでくださいね！<br /> <br /> 拡張部分のポイントは、タッチ系のイベントとマウス系のイベント両方に対して同じイベントをバインドしているところです。<br /> また、オブジェクトに対してタッチした後、タッチ移動した際にタッチ位置がオブジェクトの外に出た場合、 PCではmouseoutが呼ばれるのに対し、スマートフォンでは対応するイベントがありません。<br /> そこで、挙動を統一する為に、タッチ操作のときのmoveイベント内で、オブジェクトの外に出ていないか判定する処理を設けています。<br /> <br /> さらに詳しくは、デモページを用意いたしましたので、併せてご覧ください。<br /> デモページのソースにはコメントも併記しています。<br /><br /> <a class="demo_link" href="/demo/20141128_demo.htm"> &gt;&gt; demo page </a><br /> 今回のコードを書くにあたり、こちらの記事を参考にさせていただきました！<br /> <a class="quote_link" href="http://blog.fenrir-inc.com/jp/2011/06/ios_android_pc_touchevent.html"> Developer’s Blog | iPhone/Android/PC 対応。jQuery で書くタッチイベント </a> <br /> 以上です！拙い部分もあるやもしれませんが、ご容赦ください。<br /><script type="text/javascript">// <![CDATA[
$.fn.extend({
	touchInterface : function(up){
		var down	= (arguments.length > 1 ) ? arguments[1] : false;
		var move	= (arguments.length > 2 ) ? arguments[2] : false;
		var cancel	= (arguments.length > 3 ) ? arguments[3] : false;
		return $(this).on({
			'touchstart mousedown'	: function(e){
				e.preventDefault();
				this.touching = true;
				if(down)down(e,$(this));
			},
			'touchmove mousemove'	: function(e) {
			 	if(!this.touching)return;
				e.preventDefault();
				if(move)move(e,$(this));
				if('ontouchstart' in window){
					var touch = e.originalEvent.changedTouches[0];
					var offset = $(this).offset();
					if(	touch.pageX < offset.left || touch.pageX > $(this).outerWidth()  + offset.left ||
						touch.pageY < offset.top  || touch.pageY > $(this).outerHeight() + offset.top  ){
						this.touching = false;
						if(cancel)cancel(e,$(this));
					}
				}
			},
			'touchcancel mouseout'	: function(e){
				if(!this.touching)return;
				e.preventDefault();
				if(cancel)cancel(e,$(this));
				this.touching = false;
			},
			'touchend mouseup'		: function(e){
				if(!this.touching)return;
				e.preventDefault();
				up(e,$(this));
				this.touching = false;
			}
		}).css('cursor','pointer');
	}
});

$(function(){
	$('#demo_button_20141127_A').touchInterface(
	function(e,$_this){
		$_this.empty().append('action:up/end');
		$_this.css('background-color','#FFF');
	},
	function(e,$_this){
		$_this.empty().append('action:down/start');
		$_this.css('background-color','#AAF');
	},
	function(e,$_this){
		$_this.empty().append('action:move');
		$_this.css('background-color','#AFA');
	},
	function(e,$_this){
		$_this.empty().append('action:out/cancel');
		$_this.css('background-color','#FFA');
	}
	);
});
// ]]&gt;</script></div><p>The post <a href="https://yugalab.net/archives/289">【jQuery】コピペOK！タッチ操作とマウス操作の動作定義をまとめる</a> first appeared on <a href="https://yugalab.net">ユガラボ</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://yugalab.net/archives/289/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
