嘿那里,我有一个启用了 jQuery Draggable() 的页面#person
然后#person
被限制为#map
.
(插件链接:http://docs.jquery.com/UI/Draggables/draggable http://docs.jquery.com/UI/Draggables/draggable )
不过,我还使用键盘上的箭头键添加了控制。现在,它们都配合得很好,直到我到达约束区域的边缘(#map
).
如果我拖动,那么#person
受到限制#map
- 如果我使用箭头键,#person
可以超越的边缘#map
.
如何限制箭头键和可拖动键?
我的键盘控制代码:
$(document).bind('keydown',function(e){ //set the keydown function as...
switch(e.which) {
case 37: $(characterName).animate({
left: '-=40'
}, 250, function() {
});
break;
}
});
****唷****干杯。
[编辑]请问有人吗? [/编辑]
如果有人觉得有点懒,这就是我刚刚编码的(简化的):
$('body').on('keypress', handleKeys);
function handleKeys(e) {
var position,
draggable = $('#draggable'),
container = $('#container'),
distance = 1; // Distance in pixels the draggable should be moved
position = draggable.position();
// Reposition if one of the directional keys is pressed
switch (e.keyCode) {
case 37: position.left -= distance; break; // Left
case 38: position.top -= distance; break; // Up
case 39: position.left += distance; break; // Right
case 40: position.top += distance; break; // Down
default: return true; // Exit and bubble
}
// Keep draggable within container
if (position.left >= 0 && position.top >= 0 &&
position.left + draggable.width() <= container.width() &&
position.top + draggable.height() <= container.height()) {
draggable.css(position);
}
// Don't scroll page
e.preventDefault();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)