-
Notifications
You must be signed in to change notification settings - Fork 0
/
customCursor.min.js
1 lines (1 loc) · 10.2 KB
/
customCursor.min.js
1
(function(){for(var a=0,b=["ms","moz","webkit","o"],c=0,d=b.length;d>c&&!window.requestAnimationFrame;++c)window.requestAnimationFrame=window[b[c]+"RequestAnimationFrame"],window.cancelAnimationFrame=window[b[c]+"CancelAnimationFrame"]||window[b[c]+"CancelRequestAnimationFrame"];window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||(window.requestAnimationFrame=function(b){var d=(new Date).getTime(),e=Math.max(0,16-(d-a)),f=window.setTimeout(function(){b(d+e)},e);return a=d+e,f}),window.cancelAnimationFrame||(window.cancelAnimationFrame=function(a){clearTimeout(a)})})(),function(a,b,c){function p(){var c,e,a=b.createElement("div");return d.appendChild(a),a.style.cursor="url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNSBNYWNpbnRvc2giIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6MkZCM0Q3MkQ1Njk0MTFFMkEzRUNENDk1OUE5QkRDNTQiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6MkZCM0Q3MkU1Njk0MTFFMkEzRUNENDk1OUE5QkRDNTQiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDoyRkIzRDcyQjU2OTQxMUUyQTNFQ0Q0OTU5QTlCREM1NCIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDoyRkIzRDcyQzU2OTQxMUUyQTNFQ0Q0OTU5QTlCREM1NCIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PoFc8z4AAAAQSURBVHjaYvj//z8DQIABAAj8Av7bok0WAAAAAElFTkSuQmCC'), default",c=m(node),e=c.cursor,d.removeChild(a),e&&"default"!==e}function s(){this.listOfNodes=[],this.listOfCursors=[]}function u(a,b,c){this.cache={regexp:{html:/<?\w+\s+[^>]*>/,filename:/[/]{0,1}([^/]+[/])*([^/]*)/}},this.setPosition(b,c),this.elements=[],this.type,this.source,this.setSource(a),this.scrollHappened=!1,this.previousCursor="default",this._registerScroll=g(this._registerScroll,this),this._showCursor=g(this._showCursor,this),this._mousemove=g(this._mousemove,this),this._followCursor=g(this._followCursor,this),this._hideCursor=g(this._hideCursor,this)}var d=b.body,e=function(a,b,c,d){if(a&&a.length){for(var e=0,f=a.length;f>e;e++)if((d=b.call(c,e,a[e]))&&"boolean"==typeof d)return a[e]}else if("object"==typeof a)for(var e in a)if(a.hasOwnProperty(e)&&(d=b.call(c,e,a[e]))&&"boolean"==typeof d)return a[e]},f=function(a,b){var d=-1;return a.indexOf?d=a.indexOf(b):e(a,function(a,e){return e===b?(d=a,!0):c},this),d},g=function(a,b){if(Function.prototype.bind&&a instanceof Function&&a.bind===Function.prototype.bind)return a.bind(b);var c=Array.prototype.slice;return args&&args.splice||(args=[]),function(){return a.apply(b,args.concat(c.call(arguments)))}},h={},i=function(){return b.addEventListener?function(b,c,d,f){b&&b.nodeName||b===a?b.addEventListener(c,d,!!f):b&&b.length&&e(b,function(a,b){i(b,c,d)},this)}:function(b,c,d){if(b&&b.nodeName||b===a){var g=function(){return d.call(b,a.event)};h[c]||(h[c]=[]),h[c].push({index:h[c].length,element:b,fn:d,listener:g}),b.attachEvent("on"+c,g)}else b&&b.length&&e(b,function(a,b){i(b,c,d)},this)}}(),j=function(){return b.removeEventListener?function(b,c,d){b&&b.nodeName||b===a?b.removeEventListener(c,d,!1):b&&b.length&&e(b,function(a,b){j(b,c,d)},this)}:function(b,d,f){if((b&&b.nodeName||b===a)&&h[d]){var g=e(h[d],function(a,d){return d?d.element===b&&d.fn===f:c},this);if(!g)return;delete h[d][g.index],b.detachEvent("on"+d,g.listener)}}}(),k=function(){var a=0;return b.createEvent?function(b,c){c.target=b,setTimeout(function(){try{b.dispatchEvent(c)}catch(d){a++}},a)}:function(b,c){c.target=b,setTimeout(function(){try{b.fireEvent("on"+c.type,c)}catch(d){a++}},a)}}(),l=function(){return a.requestAnimationFrame||a.webkitRequestAnimationFrame||a.mozRequestAnimationFrame||a.msRequestAnimationFrame||a.oRequestAnimationFrame||function(a){return setTimeout(function(){a()},1e3/60)}}(),m=function(b){return b&&1===b.nodeType?a.getComputedStyle?a.getComputedStyle(b,null):b.currentStyle?b.currentStyle:{}:{}},n=function(a,b){var c,e;if(a&&1===a.nodeType&&a.offsetParent){b||(b=d),c=a.offsetTop,e=a.offsetLeft;for(var f=a,g=a.offsetParent;f&&f!==d;){if(f===g&&(c+=f.offsetTop,e+=f.offsetLeft,g=f.offsetParent),f===b){c-=b.offsetTop,e-=b.offsetLeft;break}f=f.parentNode}}return{top:c,left:e}},o=function(b){var c={relativeTop:0,relativeLeft:0,relativeBottom:0,relativeRight:0,absoluteTop:0,absoluteLeft:0,absoluteBottom:0,absoluteRight:0,width:0,height:0,children:[],node:b};if(b&&1===b.nodeType&&b.offsetParent){c.width=b.offsetWidth,c.height=b.offsetHeight;var d=n(b);c.relativeTop=d.top,c.relativeLeft=d.left;var e=a.pageYOffset,f=a.pageXOffset;if(c.absoluteTop=c.relativeTop-e,c.absoluteLeft=c.relativeLeft-f,c.relativeRight=c.relativeLeft+c.width,c.relativeBottom=c.relativeTop+c.height,c.absoluteRight=c.absoluteLeft+c.width,c.absoluteBottom=c.absoluteTop+c.height,b.childNodes.length)for(var j,k,g=0,h=b.childNodes,i=h.length;i>g;g++)(j=h[g])&&1===j.nodeType&&(k=n(j,b),k.width=j.offsetWidth,k.height=j.offsetHeight,k.right=k.left+k.width,k.bottom=k.top+k.height,c.children.push(k))}return c},q=p(),r="custom-cursor-";s.prototype={isCursor:function(a){return a&&a instanceof u},attachCursor:function(a,b){if(a&&1===a.nodeType&&this.isCursor(b)){var c=f(this.listOfNodes,a);if(-1!==c){var b=this.listOfCursors[c];b.removeElement(a)}this.listOfNodes.push(a),this.listOfCursors.push(b)}},detachCursor:function(a){if(a&&1===a.nodeType){var b=f(this.listOfNodes,a);-1!==b&&(this.listOfNodes.splice(b,1),this.listOfCursors.splice(b,1))}},destroy:function(a){this.isCursor(a)&&(a.removeElements(a.elements),delete a)}};var t=new s;u.prototype={setPosition:function(a,b){var c=this.position||{};if(this.position={x:"number"==typeof a?a:c.x||0,y:"number"==typeof b?b:c.y||0},"html"===this.type){var d=this.source.style;d.marginTop="-"+this.position.y+"px",d.marginLeft="-"+this.position.x+"px"}},setSource:function(a,c){if(a){var f=this.type;if((c||!this.originalSource)&&(this.originalSource=a),1===a.nodeType)q&&"CANVAS"===a.nodeName?(this.type="img",this.source=a.toDataURL("image/png")):q&&"IMG"===a.nodeName?(this.type="img",this.source=a.getAttribute("src")):(this.type="html",this.source=a);else if("string"==typeof a)if(this.cache.regexp.html.test(a)){var h=b.createElement("div");if(h.innerHTML=a,1===h.childNodes.length)return this.setSource(h.firstChild,!1);h.setAttribute("class",r+"wrapper"),this.type="html",this.source=h}else this.cache.regexp.filename.test(a)&&(q?(this.type="img",this.source=a):(this.type="html",this.source=new Image,this.source.src=a));if("html"===this.type){var j=this.source.style;j.display="none",j.cursor="none",j.position="fixed",j.marginTop="-"+this.position.y+"px",j.marginLeft="-"+this.position.x+"px",j.width=j.width||"auto",j.height=j.height||"auto",d.appendChild(this.source),e("mousedown mousemove mouseup click".split(" "),function(a,b){i(this.source,b,g(function(a){this.currentTarget&&k(this.currentTarget.node,a)},this))},this)}if(this.elements.length&&f!==this.type){var l=this.elements;this.elements=[],this.setElements(l)}}},update:function(){this.originalSource&&this.setSource(this.originalSource)},_isInRange:function(a,b,c){return a>=b&&c>=a},_isPointInTarget:function(a,b){if("number"==typeof a&&"number"==typeof b&&this.currentTarget){var d=this.currentTarget,e=this._isInRange(a,d.absoluteLeft,d.absoluteRight)&&this._isInRange(b,d.absoluteTop,d.absoluteBottom);if(e&&d.children&&d.children.length)for(var f=0,g=d.children,h=d.children.length;h>f;f++)if(this._isInRange(a,d.absoluteLeft+g[f].left,d.absoluteLeft+g[f].right)&&this._isInRange(b,d.absoluteTop+g[f].top,d.absoluteTop+g[f].bottom))return!1;return e}},_registerScroll:function(){this.scrollHappened=!0},_showCursor:function(b){if(this.source&&"html"===this.type&&-1!==f(this.elements,b.target)&&(this.currentTarget=o(b.target),this._isPointInTarget(b.clientX,b.clientY))){j(b.target,"mousemove",this._showCursor),this.previousCursor=m(b.target).cursor,b.target.style.cursor="none";var c=this.source.style;c.display="block",c.cursor="none",c.left=b.clientX+"px",c.top=b.clientY+"px",i(d,"mousemove",this._mousemove),i(a,"scroll",this._registerScroll)}},_mousemove:function(a){this.e=a,l(this._followCursor)},_followCursor:function(){if(this.currentTarget){if(this.scrollHappened){this.scrollHappened=!1;var b=a.pageYOffset,d=a.pageXOffset,f=this.currentTarget;f.absoluteTop=f.relativeTop-b,f.absoluteBottom=f.absoluteTop+f.height,f.absoluteLeft=f.relativeLeft-d,f.absoluteRight=f.absoluteLeft+f.width}var h,g=this.e;if(this._isPointInTarget(g.clientX,g.clientY))h=this.source.style,h.left=g.clientX+"px",h.top=g.clientY+"px";else{var i=this.currentTarget,j=!1;this.currentTarget=o(this.currentTarget.node),e(this.currentMeasures,function(a,b){return b!==i[a]?j=!0:c}),j?this._followCursor():this._hideCursor()}}},_hideCursor:function(b){j(d,"mousemove",this._mousemove),j(a,"scroll",this._registerScroll);var e=this.currentTarget.node,f=this.source.style,g=this.e;e.style.cursor=this.previousCursor,f.display="none",f.left="-"+this.currentTarget.width+"px",f.top="-"+this.currentTarget.height+"px",g.type="mouseout",k(e,g),this.currentTarget=this.e=c,b||i(e,"mousemove",this._showCursor)},setElement:function(a){a&&1===a.nodeType&&-1===f(this.elements,a)&&(this.elements.push(a),t.attachCursor(a,this),q&&"img"===this.type?a.style.cursor="url("+this.source+") "+this.position.x+" "+this.position.y+", default":i(a,"mousemove",this._showCursor))},setElements:function(a){a&&a.length&&e(a,function(a,b){this.setElement(b)},this)},removeElement:function(a){if(a&&1===a.nodeType){var b=f(this.elements,a);if(-1!==b){if(this.currentTarget&&this.currentTarget.node===a)try{this._hideCursor(!0)}catch(c){}else j(a,"mousemove",this._showCursor);this.elements.splice(b,1),t.detachCursor(a)}}},removeElements:function(a){a&&a.length&&e(a,function(a,b){this.removeElement(b)},this)},destroy:function(){t.destroy(this)}},a.CustomCursor=u}(window,window.document);