1
0

global.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * File: global.js
  3. * User: Baoxu
  4. * Date: 16/3/8
  5. * Time: 23:31
  6. * Desc: 全局的JS设置或函数
  7. */
  8. /**
  9. * 添加页面加载后事件
  10. * @name addLoadEvent
  11. * @param {Function} func 需要页面载入后执行的函数
  12. */
  13. function addLoadEvent(func){
  14. var oldOnloadEvent = window.onload;
  15. if(typeof oldOnloadEvent != "function"){
  16. window.onload = func;
  17. }else{
  18. window.onload = function(){
  19. oldOnloadEvent();
  20. func();
  21. }
  22. }
  23. }
  24. /**
  25. * 自定义的日志函数
  26. * @name baoxuLog
  27. * @param {String} log_class 日志记录的分类
  28. * @param {String} log_text 日志记录的语句
  29. */
  30. function baoxuLog(log_class, log_text){
  31. console.log(log_class + " : " + log_text);
  32. }
  33. /**
  34. * 为各个浏览器添加Prefix
  35. * @name addPerfix
  36. */
  37. function addPerfix(){
  38. for(var i in pfx){
  39. if(obj.style){
  40. }
  41. }
  42. }
  43. /**
  44. * 获取页面当前距离顶部的距离
  45. * @name getScrollTop
  46. * @return {Number} 返回距离
  47. */
  48. function getScrollTop(){
  49. var scrollTop = 0;
  50. if(document.documentElement && document.documentElement.scrollTop){
  51. scrollTop = document.documentElement.scrollTop;
  52. }else if(document.body){
  53. scrollTop = document.body.scrollTop;
  54. }
  55. return scrollTop;
  56. }
  57. /**
  58. * 自定义的日志函数
  59. * @name addProperty
  60. * @param {String} log_class 日志记录的分类
  61. * @param {String} log_text 日志记录的语句
  62. */
  63. function addProperty(log_class, log_text){
  64. console.log(log_class + " : " + log_text);
  65. }
  66. /**
  67. * @name EventUtil
  68. * @class 此方法处理事件
  69. */
  70. var EventUtil = {
  71. addHandler:function(element, type, handler){
  72. if(element.addEventListener){
  73. element.addEventListener(type, handler, false);
  74. }else if(element.attachEvent){
  75. element.attachEvent("on" + type, handler);
  76. }else{
  77. element["on" + type] = handler;
  78. }
  79. },
  80. getButton:function(event){
  81. if(document.implementation.hasFeature("MouseEvents", "2.0")){
  82. return event.button;
  83. }else{
  84. switch(event.button){
  85. case 0:
  86. case 1:
  87. case 3:
  88. case 5:
  89. case 7:
  90. return 0;
  91. case 2:
  92. case 6:
  93. return 2;
  94. case 4:
  95. return 1;
  96. }
  97. }
  98. },
  99. getCharCode:function(event){
  100. if(typeof event.charCode == "number"){
  101. return event.charCode;
  102. }else{
  103. return event.keyCode;
  104. }
  105. },
  106. getClipboardText:function(event){
  107. var clipboardData = (event.clipboardData || window.clipboardData);
  108. return clipboardData.getData("text");
  109. },
  110. getEvent:function(event){
  111. return event ? event : window.event;
  112. },
  113. getRelatedTarget:function(event){
  114. if(event.relatedTarget){
  115. return event.relatedTarget;
  116. }else if(event.toElement){
  117. return event.toElement;
  118. }else if(event.fromElement){
  119. return event.fromElement;
  120. }else{
  121. return null;
  122. }
  123. },
  124. getTarget:function(event){
  125. return event.target || event.srcElement;
  126. },
  127. getWheelDelta:function(event){
  128. if(event.wheelDelta){
  129. return (client.engine.opera && client.engine.opera < 9.5 ? -event.wheelDelta : event.wheelDelta);
  130. }else{
  131. return -event.detail * 40;
  132. }
  133. },
  134. preventDefault:function(event){
  135. if(event.preventDefault){
  136. event.preventDefault();
  137. }else{
  138. event.returnValue = false;
  139. }
  140. },
  141. removeHandler:function(element, type, handler){
  142. if(element.removeEventListener){
  143. element.removeEventListener(type, handler, false);
  144. }else if(element.detachEvent){
  145. element.detachEvent("on" + type, handler);
  146. }else{
  147. element["on" + type] = null;
  148. }
  149. },
  150. setClipboardText:function(event, value){
  151. if(event.clipboardData){
  152. event.clipboardData.setData("text/plain", value);
  153. }else if(window.clipboardData){
  154. window.clipboardData.setData("text", value);
  155. }
  156. },
  157. stopPropagation:function(event){
  158. if(event.stopPropagation){
  159. event.stopPropagation();
  160. }else{
  161. event.cancelBubble = true;
  162. }
  163. }
  164. };