javascript - stop animation on window blur -
i have following code, fades elements in , out in repeating cycle. when tab inactive text within divs piles on top of each other split second before being cleared when tab activated again. there way stop animation when window blurs , start again on focus?
(cycle = function () { settimeout(function(){$('#right').fadeout(1000)},10000); settimeout(function(){$('right').fadein(1000)}, 11000); settimeout(function(){$('#left').fadeout(1000)},13000); settimeout(function(){$('#left').fadein(1000)},14000); settimeout(function(){$('#left').fadeout(1000)},15000); settimeout(function(){$('#left').fadein(1000)},17000); })();
i gave suggestion in comment, remembered that solution not cross-browser compatible, had come across before, creation of plugin.
suffice say, $(window).blur()
, focus
not work expected on browsers. don't remember exact list of problems ran into, know things like; clicking on tab (in ff, think) did not trigger blur, clicking on program trigger blur despite fact main browser window still open , tab had focus, ddnt have windows focus, etc...
the following plugin created might helpful in i've filed down work in "most" browsers , versions (not tested on versions) , functions expect work. goes blur if exact browser window's tab loses focus tab of same browser. , of course vice versa focus.
see jsfiddle example usage , unminified code
minified plugin:
simply add js file called after jquery or place @ top of code
(function(jquery){jquery.winfocus||(jquery.extend({winfocus:function(b){function c(a){a=a||window.event;a.hidden=a.type in{focus:"visible",focusin:"visible",pageshow:"visible",blur:"hidden",focusout:"hidden",pagehide:"hidden"}?"focusout"===a.type:this[d];jquery(window).data("visible",!a.hidden);jquery.winfocus.methods.execb(a)}var d="hidden";d in document?document.addeventlistener("visibilitychange",c):(d="mozhidden")in document?document.addeventlistener("mozvisibilitychange",c):(d="webkithidden")in document? document.addeventlistener("webkitvisibilitychange",c):(d="mshidden")in document?document.addeventlistener("msvisibilitychange",c):"onfocusin"in document?document.onfocusin=document.onfocusout=c:window.onpageshow=window.onpagehide=window.onfocus=window.onblur=c;for(x in arguments)"object"==typeof arguments[x]?(arguments[x].blur&&(jquery.winfocus.methods.blur=arguments[x].blur),arguments[x].focus&&(jquery.winfocus.methods.focus=arguments[x].focus),arguments[x].blurfocus&&(jquery.winfocus.methods.blurfocus= arguments[x].focus)):"function"==typeof arguments[x]&&(void 0===jquery.winfocus.methods.blurfocus?jquery.winfocus.methods.blurfocus=arguments[x]:(jquery.winfocus.methods.blur=jquery.winfocus.methods.blurfocus,jquery.winfocus.methods.blurfocus=void 0,jquery.winfocus.methods.focus=arguments[x]))}}),jquery.winfocus.methods={blurfocus:void 0,blur:void 0,focus:void 0,execb:function(b){jquery.winfocus.methods.blurfocus?jquery.winfocus.methods.blurfocus(b,!b.hidden):b.hidden?jquery.winfocus.methods.blur&& jquery.winfocus.methods.blur(b):jquery.winfocus.methods.focus&&jquery.winfocus.methods.focus(b)}})})(jquery);
also: #line-o 's referenced question first inspired write plugin , have plugin answer posted there. lol
Comments
Post a Comment