jquery.usermode.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @author trixta
  3. */
  4. (function($){
  5. $.userMode = (function(){
  6. var userBg,
  7. timer,
  8. testDiv,
  9. boundEvents = 0;
  10. function testBg(){
  11. testDiv = testDiv || $('<div></div>').css({position: 'absolute', left: '-999em', top: '-999px', width: '0px', height: '0px'}).appendTo('body');
  12. var black = $.curCSS( testDiv.css({backgroundColor: '#000000'})[0], 'backgroundColor', true),
  13. white = $.curCSS( testDiv.css({backgroundColor: '#ffffff'})[0], 'backgroundColor', true),
  14. newBgStatus = (black === white || white === 'transparent');
  15. if(newBgStatus != userBg){
  16. userBg = newBgStatus;
  17. $.event.trigger('_internalusermode');
  18. }
  19. return userBg;
  20. }
  21. function init(){
  22. testBg();
  23. timer = setInterval(testBg, 3000);
  24. }
  25. function stop(){
  26. clearInterval(timer);
  27. testDiv.remove();
  28. testDiv = null;
  29. }
  30. $.event.special.usermode = {
  31. setup: function(){
  32. (!boundEvents && init());
  33. boundEvents++;
  34. var jElem = $(this)
  35. .bind('_internalusermode', $.event.special.usermode.handler);
  36. //always trigger
  37. setTimeout(function(){
  38. jElem.triggerHandler('_internalusermode');
  39. }, 1);
  40. return true;
  41. },
  42. teardown: function(){
  43. boundEvents--;
  44. (!boundEvents && stop());
  45. $(this).unbind('_internalusermode', $.event.special.usermode.handler);
  46. return true;
  47. },
  48. handler: function(e){
  49. e.type = 'usermode';
  50. e.disabled = !userBg;
  51. e.enabled = userBg;
  52. return jQuery.event.handle.apply(this, arguments);
  53. }
  54. };
  55. return {
  56. get: testBg
  57. };
  58. })();
  59. $.fn.userMode = function(fn){
  60. return this[(fn) ? 'bind' : 'trigger']('usermode', fn);
  61. };
  62. $(function(){
  63. $('html').userMode(function(e){
  64. $('html')[e.enabled ? 'addClass' : 'removeClass']('hcm');
  65. });
  66. });
  67. })(jQuery);