Introduction 1
, script.
mobile, . , script.
2
:
mouse, touch.mousemove, node, , touchmove , . , node a node b, event.target node a.
, , touchstart touchmove , , .
var matrix = $('#our_table td').map(function(){
var e = $(this),
o = e.offset(),
w = e.width(),
h = e.height();
return {
top: o.top,
left: o.left,
right: o.left + w,
bottom: o.top + h,
e: e
}
}).get();
var currentTarget = $(),
activeTarget = $();
var touchF = function(e) {
var touch = e.originalEvent.touches[0];
currentTarget = getCurrent(
{
clientX: touch.clientX,
clientY: touch.clientY
}
);
if (currentTarget && currentTarget != activeTarget) {
activeTarget = currentTarget;
activeTarget.toggleClass('highlighted');
}
}
$('#our_table').bind({
touchstart: touchF,
touchmove: touchF
});
function getCurrent(touch) {
var a = matrix.filter(function(obj) {
var b = (
touch.clientX > obj.left &&
touch.clientX < obj.right &&
touch.clientY < obj.bottom &&
touch.clientY > obj.top
);
return b;
});
return a.length > 0 ? a[0].e : null;
}
table td {
width:100px;
height:100px;
text-align:center;
vertical-align:middle;
background-color:#ccc;
border:1px solid #fff;
}
table td.highlighted {
background-color:#999;
}
<table cellpadding="0" cellspacing="0" id="our_table">
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
</tr>
<tr>
<td>d</td>
<td>e</td>
<td>f</td>
</tr>
<tr>
<td>g</td>
<td>h</td>
<td>i</td>
</tr>
</table>
: http://jsbin.com/favobu