Skip to content

Instantly share code, notes, and snippets.

@nielk
Created June 11, 2014 13:40
Show Gist options
  • Save nielk/55cc052aae174b340acc to your computer and use it in GitHub Desktop.
Save nielk/55cc052aae174b340acc to your computer and use it in GitHub Desktop.
Event scroll touch click
//
// .important touch/click event
// ==========================================================================
/**
* Event factory
*/
var eventFactory = function($elt, callback) {
var flag = true;
var touchStartPos;
var distance;
var $clickableItem;
$elt
.bind('touchstart', function(e){
started();
})
.bind('touchend', function(e){
ended($(this));
}).on('mousedown',function(e){
started();
}).on('mouseup', function(e){
ended($(this));
});
var getPosition = function() {
return $('#draggable').position().left;
};
var started = function() {
touchStartPos = getPosition();
};
var ended = function($this) {
var distance = touchStartPos - getPosition();
$clickableItem = $this;
$clickableItem.addClass("touched");
if (distance > 20 || distance < -20){
// touch moved
} else {
if($(this).hasClass("touched")) $(this).removeClass("touched");
callback($this, flag);
flag = !flag;
}
};
};
/**
* gestion touch/click/scroll pour .important elements
*/
var event01 = new eventFactory($('.important .timeline-box-body', '#draggable'), function($this, flag) {
console.log('clicked');
if(flag) {
$this.find('.more').fadeOut('fast');
$this.addClass('opened');
} else {
$this.removeClass('opened');
$this.find('.more').fadeIn('fast');
}
}
);
/**
* gestion touch/click/scroll pour .not-important elements
*/
var event02 = new eventFactory($('.not-important .timeline-box-body', '#draggable'), function($this, flag) {
console.log('clicked');
if(flag) {
$this.find('.more').fadeOut('fast');
$this.addClass('opened');
} else {
$this.removeClass('opened');
$this.find('.more').fadeIn('fast');
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment