After reading the book "proficient in javascript", I talked about many tool functions and sorted them out first.
Code mainly used for interaction between javascript and css:
[Javascript]
1. function text (e ){
2. // obtain the element text content
3. var t = "";
4. e = e. childNodes | e;
5. for (var I = 0, count = e. length; I <count; I ++ ){
6. t + = e [I]. nodeType! = 1? E [I]. nodeValue: text (e [I]. childNodes );
7 .}
8. return t;
9 .}
10. function attr (elem, name, value ){
11. // get or set the element property value
12. if (! Name | name. constructor! = String) return "";
13. name = {"for": "htmlFor", "class": "className"} [name] | name;
14. if (typeof value! = "Undefined "){
15. elem [name] = value;
16. if (elem. setAttribute ){
17. elem. setAttribute (value );
18 .}
19 .}
20. return elem [name] | elem. getAttribute (name) | "";
21 .}
22. function stopDefault (e ){
23. // prevent default browser behavior
24. if (e & e. preventDefault ){
25. e. preventDefault ();
26 .}
27. else {
28. window. event. returnValue = false;
29 .}
30. return false;
31 .}
32. function stopBubble (e ){
33. // prevent event bubbles
34. if (e & e. stopPropagation ){
35. e. stopPropagation ();
36 .}
37. else {
38. window. event. cancelBubble = true;
39 .}
40 .}
41. // call example <a href = "#" onclick = "alert ('OK'); stopBubble (event);"> demo </a>
42. function pageX (elem ){
43. // obtain the horizontal position of the element
44. return elem. offsetParent? Elem. offsetLeft + pageX (elem. offsetParent): elem. offsetLeft;
45 .}
46. function pageY (elem ){
47. // obtain the vertical position of the element
48. return elem. offsetParent? Elem. offsetTop + pageY (elem. offsetParent): elem. offsetTop;
49 .}
50. function parentX (elem ){
51. // obtain the horizontal position of the element relative to the parent
52. return elem. parentNode = elem. offsetParent? Elem. offsetLeft: pageX (elem)-pageX (elem. parentNode );
53 .}
54. function parentY (elem ){
55. // obtain the vertical position of the element relative to the parent
56. return elem. parentNode = elem. offsetParent? Elem. offsetTop: pageY (elem)-pageY (elem. parentNode );
57 .}
58. function resetCss (elem, prop ){
59. // set a set of css attributes, which can be restored to the original settings
60. var old = {};
61. for (var I in prop ){
62. old [I] = elem. style [I];
63. elem. style [I] = prop [I];
64 .}
65. return old;
66 .}
67. function restoreCss (elem, prop ){
68. for (var I in prop ){
69. elem. style [I] = prop [I];
70 .}
71 .}
72. function getStyle (elem, styleName ){
73. if (elem. style [styleName]) {// inline style
74. return elem. style [styleName];
75 .}
76. else if (elem. currentStyle) {// IE
77. return elem. currentStyle [styleName];
78 .}
79. else if (document. defaultView & document. defaultView. getComputedStyle) {// DOM
80. styleName = styleName. replace (/([A-Z])/g, '-$ 1'). toLowerCase ();
81. var s = document. defaultView. getComputedStyle (elem ,'');
82. return s & s. getPropertyValue (styleName );
83 .}
84. else {// other, for example, Safari
85. return null;
86 .}
87 .}
88. function fullHeight (elem ){
89. // possible height of the search Element
90. // If the element is displayed, use offsetHeight to get the height. If there is no offsetHeight, use getHeight ()
91. if (getStyle (elem, "display ")! = "None "){
92. return elem. offsetHeight;
93 .}
94. var oldcss = resetCss (elem, {display: "", visibility: "hidden", position: "absolute "});
95. var height = elem. clientHeight;
96. restoreCss (elem, oldcss );
97. return height;
98 .}
99. function fullWidth (elem ){
100. // possible height of the search Element
101. // If the element is displayed, use offsetWidth to get the height. If there is no offsetWidth, use getWidth ()
102. if (getStyle (elem, "display ")! = "None "){
103. return elem. offsetWidth;
104 .}
105. var oldcss = resetCss (elem, {display: "", visibility: "hidden", position: "absolute "});
106. var width = elem. clientWidth;
107. restoreCss (elem, oldcss );
108. return width;
109 .}
110. function setOpacity (elem, level ){
111. // This is the element transparency
112. if (elem. filiters ){
113. elem. style. filters = "alpha (opacity = '" + level + "')";
114 .}
115. else {
116. elem. style. opacity = level/100;
117 .}
118 .}
119. function cursorX (e ){
120. // obtain the horizontal position of the cursor
121. e = e | window. event;
122. // check the location of the non-IE browser before checking the IE location
123. return e. pageX | e. clientX + document. body. scrollLeft;
124 .}
125. function cursorY (e ){
126. // obtain the vertical position of the cursor
127. e = e | window. event;
128. return e. pageY | e. clientY + document. body. scrollTop;
129 .}
130. function scrollX (){
131. // determine the horizontal scroll bar position of the browser
132. var de = document.doc umentElement;
133. return self. pageXOffset | (de & de. scrollLeft) | document. body. scrollLeft;
134 .}
135. function scrollY (){
136. // determine the vertical scroll bar position of the browser
137. var de = document.doc umentElement;
138. return self. pageYOffset | (de & de. scrollTop) | document. body. scrollTop;
139 .}
140. function compute wheight (){
141. // obtain the height of the window
142. var de = document.doc umentElement;
143. return self. innerHeight | (de | de. clientHeight) | document. body. clientHeight;
144 .}
145. function compute wwidth (){
146. // obtain the height of the window
147. var de = document.doc umentElement;
148. return self. innerWidth | (de | de. clientWidth) | document. body. clientWidth;
149 .}
Author: dz45693