Skip to content

Instantly share code, notes, and snippets.

@k4zuki02h4t4
Forked from rokkohorosi/gist:4282291
Last active August 29, 2015 14:23
Show Gist options
  • Save k4zuki02h4t4/ddc686e7c6afa4a585f3 to your computer and use it in GitHub Desktop.
Save k4zuki02h4t4/ddc686e7c6afa4a585f3 to your computer and use it in GitHub Desktop.
(function (window) {
var hoverClass = "";
var tapClass = "";
var trigger = "";
var action = {};
if (Modernizr.touch){
action = {
"start":"touchstart",
"end": "touchend"
};
} else {
action = {
"start":"mouseover",
"end": "mouseleave"
};
}
var Hover = window.Hover = function (ele) {
return new Hover.fn.init(ele);
};
var thisRemoveHoverClass = function (e, t, h, et) {
var delay = "touchmove" === et ? 500: 100;
setTimeout(function (e, t, h) {
$(t).removeClass(h);
}, delay, e, t, h);
};
Hover.fn = {
//Hover Instance
init : function (ele) {
this.prop = ele;
},
on : function (_hoverClass, _tapClass) {
hoverClass = _hoverClass;
tapClass = _tapClass;
trigger = "a, .panel";
$(trigger).on(action.start, function(event) {
var target = event.target || window.target;
var bindElement = null;
var href = $(target).attr('href');
if (target.tagName == "A" || $(target).hasClass(tapClass)) {
bindElement = $(target);
} else if ($(target).parents(trigger).length > 0) {
bindElement = $(target).parents(trigger);
} else if ($(target).parents("." + tapClass).length > 0) {
bindElement = $(target).parents("." + tapClass);
}
if (bindElement != null) {
Hover().touchstartHoverElement(bindElement);
if ( "#" === typeof href ) {
setTimeout(function (e, b, h) {
b.removeClass(h);
}, 100, event, bindElement, hoverClass);
}
}
//if ( "undefined" === typeof href ) {
// event.preventDefault();
//}
});
},
touchstartHoverElement : function (bindElement) {
bindElement.addClass(hoverClass);
if (Modernizr.touch){
bindElement.off("touchmove", function (event) {
thisRemoveHoverClass(event, this, hoverClass, event.type);
});
bindElement.on("touchmove", function (event) {
thisRemoveHoverClass(event, this, hoverClass, event.type);
});
}
bindElement.off(action.end, function (event) {
thisRemoveHoverClass(event, this, hoverClass, event.type);
});
bindElement.on(action.end, function (event) {
thisRemoveHoverClass(event, this, hoverClass, event.type);
});
}
};
Hover.fn.init.prototype = Hover.fn;
Hover().on("hover", "tap");
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment