开发者

Drag and Drop in jQuery using Touchevent (iPhone browser)

I using jQuery to listen to the touchstart,touchmove and touchend, and I able to drag the 'dragitem' in iphone Safari(position change that base on the touchmove). But now the issue is how i can make the dropArea response when the 'dragitem' drag to the 'dropArea'.

For example the 'dropArea' will highlight/glow, change background color, and etc when the 'dragitem' is drag within the 'dropArea', but when it is away the 'dropArea' will remain normal. Any idea?

Thank in advance.

HTML:

<div class='dragArea' >
 <div id='box1' class='dragitem'>
 </div>
 <div id='box2' class='dragitem'>
 </div>
</div>

<div class='dropArea'></div>

jQuery:

var startTouchX = null;
var startTouchY = null;
var moveTouchX = nu开发者_如何学Goll;
var moveTouchY = null;
var startPositionX = null;
var startPositionY = null;

$('.dragitem').bind('touchstart',function(event){
    event.preventDefault();
    var e = event.originalEvent;
    startTouchX = e.targetTouches[0].pageX;
    startTouchY = e.targetTouches[0].pageY;
    startPositionX = $(this).css('left');
    startPositionY = $(this).css('top');
});
$('.dragitem').bind('touchmove', function(event){
    event.preventDefault();
    var e = event.originalEvent;
    moveTouchX = e.targetTouches[0].pageX;
    moveTouchY = e.targetTouches[0].pageY;
    $('#movex').text(moveTouchX);
    $('#movey').text(moveTouchY);
    $(this).css({top: (moveTouchY - 50), left: (moveTouchX - 5)});
});
$('.dragitem').bind('touchend', function(event){
    $(this).animate({top: startPositionY, left: startPositionX}, 'fast');
});


i would check the offset() of the $('.dragitem') array and $('.dropArea.') in the move Handler ... or an interval.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜