Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * "Fast Buttons" / Ghost Click Buster.
- * Based on http://code.google.com/intl/en/mobile/articles/fast_buttons.html
- *
- *
- */
- //
- // START CLICKBUSTER
- //
- var clickPointX = Array(); // PRIVATE: last X coordinate of a touch
- var clickPointY = Array(); // PRIVATE: last Y coordiante of a touch
- /**
- *
- * add a click to the clickPointX/Y array. if nopop is specified, no timeout is set to clear the click.
- *
- * @param x x-coordinate
- * @param y y-coordinate
- * @param nopop if not specified, or FALSE, the coordinate is removed from the array after 5s. if TRUE, remains
- * there forever.
- */
- function addClick ( x, y, nopop )
- {
- clickPointX.push (x);
- clickPointY.push (y);
- if (!nopop)
- {
- setTimeout (popClick, 5000)
- }
- }
- /**
- *
- * Removes the first item off the clickPointX/Y array.
- *
- */
- function popClick ()
- {
- clickPointX.splice(0,1);
- clickPointY.splice(0,1);
- }
- /**
- *
- * Determines if a click is within 15 pixels of a previous click in the clickPointX/Y array.
- * If it is, it is added to via addClick. Returns TRUE if the click SHOULD BE IGNORED;
- * false if it is not to be ignored.
- *
- * @param x x-coordinate
- * @param y y-coordinate
- * @param nopop no use here; passed on to addclick
- *
- */
- function ignoreClick (x, y, nopop)
- {
- for (var i=0;i<clickPointX.length;i++)
- {
- var testX = clickPointX[i];
- var testY = clickPointY[i];
- if ( ( Math.abs(x - testX) < 15 ) && ( Math.abs(y - testY) < 15 ) )
- {
- return true;
- }
- }
- addClick (x, y, nopop);
- return false;
- }
- /**
- *
- * Attached to the document in order to prevent duplicate clicks from occuring.
- * iOS' webkit has a nasty habit of throwing duplicate onClick events. Not good.
- *
- * Also prevents (or tries to) a click if the scrollers are in movement.
- */
- function clickBuster (event)
- {
- console.log ("Clicked " + event.clientX + ", " + event.clientY );
- if (ignoreClick(event.clientX, event.clientY) || isScrolling())
- {
- console.log ("... and ignored it.");
- setTimeout ( function() { sbScrolling=Array(false, false); console.log ("Nuked scrolling."); }, 1000 );
- event.stopPropagation();
- event.preventDefault();
- }
- }
- document.addEventListener ( 'click', clickBuster, true );
- //
- // END CLICKBUSTER
- //
- var sbScrolling = Array(false, false); // PUBLIC: indicate if a scroller is scrolling.
- //
- // BEGIN SCROLLING STUFF
- //
- var scrollingTimeout;
- function handleScrolling ( me, e, scrolling )
- {
- // first, set who is or is not scrolling.
- if (scrolling)
- {
- sbScrolling[me.whichScrollerAmI] = scrolling;
- console.log ( 'Scrolling for ' + me.whichScrollerAmI + ' is ' + scrolling );
- }
- else
- {
- // reset me in a few ms.
- if (scrollingTimeout)
- {
- clearTimeout (scrollingTimeout);
- }
- scrollingTimeout = setTimeout ( function() {
- sbScrolling[me.whichScrollerAmI] = false;
- console.log ( 'Scrolling for ' + me.whichScrollerAmI + ' is false' );
- }, 1000 );
- }
- // next, do we have an event? if so, add it as a click
- if (e)
- {
- console.log ( e );
- addClick(e.touches[0].clientX, e.touches[0].clientY);
- }
- }
- /**
- *
- * isScrolling will return TRUE if any scroller has indicated it is in a scrolling state, and
- * FALSE if no scroller is scrolling.
- *
- */
- function isScrolling()
- {
- return sbScrolling[0] || sbScrolling[1];
- }
- // and init a new scroller like this
- scroller = new iScroll ( element, { onScrollMove: function(me) {handleScrolling(this,null, true);},
- onScrollEnd: function(me) {handleScrolling(this,null, false);}
- } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement