Preventing click during drag

2,138 views
Skip to first unread message

Koviko

unread,
Feb 27, 2012, 8:03:51 AM2/27/12
to isc...@googlegroups.com
Is there a way to prevent clicks on anchor elements while the page is being dragged?

Gildas THORAVAL

unread,
Mar 1, 2012, 5:03:29 PM3/1/12
to isc...@googlegroups.com
I found this solution yesterday:

$('...').click(function(){
if (myiScroll.animating) return;
...

trkaplan (tuncay)

unread,
Mar 2, 2012, 3:25:48 AM3/2/12
to iScroll
Thanks Gildas, that's working!

Koviko

unread,
Mar 5, 2012, 9:06:35 AM3/5/12
to isc...@googlegroups.com
This working great for me, as well. Thanks. :)

Gildas THORAVAL

unread,
Mar 5, 2012, 1:46:38 PM3/5/12
to isc...@googlegroups.com
a i've found a better solution: replace "animating" by "moved"
cause if you slide stop and release, that is not animating and click work

mistering

unread,
Mar 6, 2012, 6:26:06 PM3/6/12
to isc...@googlegroups.com
@Gildas,

I am kind of new to this. Can you help me put the solution code you listed above in this context below:

var myScroll;
function loaded() {
myScroll = new iScroll('contentWrapper');
}

I was able to use another code solution that worked, but that failed once I inserted images.

Gildas THORAVAL

unread,
Mar 7, 2012, 4:20:34 AM3/7/12
to isc...@googlegroups.com
In fact this solution don't realy block the click event.
It's inside the click event that I detect if the scoll was moved between mous press and mouse up

var myScroll;
function loaded() {
myScroll = new iScroll('contentWrapper');
// considering your clickable object have a "button" class.
$('#contentWrapper .button').click(function(){
if (myScroll.animating)
return;

//your code here
}
}

PS: $ is jquery function. I don't remember how to do without jquery, should be something like:
document.getElementById('contentWrapper').????('.button').AddEventListenener???('onclick', your function);

is that clear for you?

mistering

unread,
Mar 7, 2012, 5:13:51 PM3/7/12
to isc...@googlegroups.com
@Gildas

Thanks for the help; I really appreciate it. I plugged in the code, but it did not work. I am actually looking for a solution that will block the click event to prevent a modal window from firing. I found an example close to what I am looking for:  http://jsfiddle.net/B37jn/3/ but it seems no one has found a solution.

Any help appreciated. Thank you.

Gildas THORAVAL

unread,
Mar 8, 2012, 1:18:27 AM3/8/12
to isc...@googlegroups.com
cool website, I add it to my favorites...

I tried chandged your code but it appears to work only on touch devices...

mistering

unread,
Mar 8, 2012, 2:15:55 AM3/8/12
to isc...@googlegroups.com
@Gildas

Thank you. It worked for the example http://jsfiddle.net/B37jn/24/  The code is not mine but I found it to be similar since I was using a modal window.

I plugged into my code, but the modal still getting triggered. Here is what I have (using Colorbox code for the modal)

<script>
$(document).ready(function(){
$('a').colorbox({iframe:true});
});
</script>

and your code

<script>
var scrollname;
function loaded() {
  scrollname = new iScroll('wrapper');
     $('#thelist a').click(function() {
     if (scrollname.moved) return;
   }
   );
}
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', loaded, false);
</script>

mistering

unread,
Mar 8, 2012, 3:46:03 PM3/8/12
to isc...@googlegroups.com
update. the code worked with the modal once i added 'false' after 'return'

...
if (scrollname.moved) return false;
...

i hope that does not cause other issues with the rest of the codes. please let me know if that is an improper way to do it. thanks for the help.

Gildas THORAVAL

unread,
Mar 9, 2012, 2:28:38 AM3/9/12
to isc...@googlegroups.com
if I'm not wrong
return false in a event is the equivalent to event.stopPropagation() call ( ! ? )
so the click event stop here and don't go to parent objects.
So no it's not improper.

Mindon Feng

unread,
Oct 21, 2012, 11:05:19 PM10/21/12
to isc...@googlegroups.com
my patch to prevent unwanted  click event in a scroll area

 \+ line 336  ( in _start: function )

that.distance = 0;


 \+ line 438 ( in _move: function )

that.distance += Math.sqrt( deltaX * deltaX + deltaY * deltaY );


 \+ line 913 ( in refresh: function )

that.distance = 0;



example


// usage
function clickHandler() {
 if( myScroll.distance > 20 ) 
   return;
 // click task here

Андрей Нахабов

unread,
Jan 25, 2013, 9:02:13 AM1/25/13
to isc...@googlegroups.com
I just check moved property and distX+distY , so no need to patch the source.

onClick = function() {
    var d = Math.abs(scroller.distX) + Math.abs(scroller.distY); //no need to extract the root for my tasks
    if (scroller.moved || (d > 10) return false;
    /*  click event handling */
}

Chris Fennell

unread,
Jan 26, 2013, 9:12:28 AM1/26/13
to isc...@googlegroups.com
On my applications, I personally don't use iScroll's onClick event.
I only used the moved flag. Like this.

$('.link').on('click', function() {
  if (myScroll.moved) return; 
  // add click functionality here
});
Reply all
Reply to author
Forward
0 new messages