1// setClassOnHover 2;(function($){ 3 // options 4 function setClassOnHover(options) { 5 this.options = $.extend({ 6 hoverElem: '>li', // specify which element will trigger setClassOnHover function 7 setOnElem: 'body', // specify element on which class will be set 8 hoverClass: 'active' // specify class name that will be set on element 9 }, options); 10 11 this.init(); 12 } 13 14 // prototype 15 setClassOnHover.prototype = { 16 init: function() { 17 this.findElements(); 18 this.controlClass(); 19 }, 20 findElements: function() { 21 this.holder = $(this.options.holder), 22 this.hoverElem = this.holder.find(this.options.hoverElem), 23 this.setOnElem = $(this.options.setOnElem); 24 }, 25 controlClass: function() { 26 var self = this; 27 28 // detect device type 29 var isTouchDevice = ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch, 30 isWinPhoneDevice = /Windows Phone/.test(navigator.userAgent); 31 32 // define events 33 var eventOn = (isTouchDevice && 'touchstart') || (isWinPhoneDevice && navigator.pointerEnabled && 'pointerdown') || (isWinPhoneDevice && navigator.msPointerEnabled && 'MSPointerDown') || 'mouseenter'; 34 35 if(isTouchDevice || isWinPhoneDevice) { // if mobiles 36 // prevent default 37 preventHandler = function(e) { 38 e.preventDefault(); 39 }; 40 41 this.hoverElem.on(eventOn, function() { 42 var toggleOff = function(e) { 43 var target = $(e.target); 44 45 if (!target.is(self.hoverElem) && !target.closest(self.hoverElem).length) { 46 self.hoverElem.off('click', preventHandler); 47 $(document).off(eventOn, toggleOff); 48 self.setOnElem.removeClass(self.options.hoverClass); 49 } 50 }; 51 52 if(!self.setOnElem.hasClass(self.options.hoverClass)) { 53 self.hoverElem.one('click', preventHandler); 54 $(document).on(eventOn, toggleOff); 55 self.setOnElem.addClass(self.options.hoverClass); 56 } 57 }); 58 } else { // if not mobiles 59 this.hoverElem.hover(function() { 60 self.setClass(); 61 }, 62 function() { 63 self.deleteClass(); 64 }); 65 } 66 }, 67 setClass: function() { 68 if(!this.setOnElem.hasClass(this.options.hoverClass)) return this.setOnElem.addClass(this.options.hoverClass); 69 }, 70 deleteClass: function() { 71 if(this.setOnElem.hasClass(this.options.hoverClass)) return this.setOnElem.removeClass(this.options.hoverClass); 72 } 73 }; 74 75 // jquery plugin 76 $.fn.setClassOnHover = function(opt) { 77 return this.each(function() { 78 $(this).data('setClassOnHover', new setClassOnHover($.extend(opt, { holder: this }))); 79 }); 80 }; 81})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.