Advertisement
afsarwebdev

Window OffsetWidthHeight,ScrollWidthHeight,ClientWidthHeight

Sep 2nd, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.24 KB | None | 0 0
  1. Html:
  2.  
  3. <div id="content">
  4. Note how the browser reserves some space to the right of this paragraph, just enough to fit a scrollbar. This happens even though there is a decent amount of padding-right.
  5. <div class="nested">
  6. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
  7. </div>
  8. <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
  9. <p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat. </p>
  10. </div>
  11. <div id="overlay">
  12. <div id="scrollWidth"></div>
  13. <div id="clientWidth"></div>
  14. <div id="offsetWidth"></div>
  15.  
  16. <div id="scrollHeight"></div>
  17. <div id="clientHeight"></div>
  18. <div id="offsetHeight"></div>
  19.  
  20. <div id="paddingLeft"></div>
  21. <div id="paddingRight"></div>
  22. <div id="paddingTop"></div>
  23. <div id="paddingBottom"></div>
  24.  
  25. <div id="cssWidth"></div>
  26. <div id="cssHeight"></div>
  27.  
  28. </div>
  29. -----------------------------------------------------------------
  30. SCSS:
  31.  
  32. /* you can experiment with these */
  33. $width: 180px;
  34. $height: 180px;
  35. $nestedWidth: 400px; /* try auto */
  36. $paddingTop: 35px;
  37. $paddingRight: 120px;
  38. $paddingBottom: 60px;
  39. $paddingLeft: 90px;
  40. $borderTop: 10px;
  41. $borderRight: 25px;
  42. $borderBottom: 20px;
  43. $borderLeft: 15px;
  44. $overflow: auto; /* try auto, hidden, scroll, visible */
  45.  
  46. /* leave these alone */
  47. $labelSpacing: 5px;
  48. $labelWidth: 20px;
  49. $margin: 5 * $labelSpacing + 4 * $labelWidth;
  50. $marginLeft: 120px;
  51.  
  52. $offsetHeight: $borderTop + $paddingTop + $height + $paddingBottom + $borderBottom;
  53. $offsetWidth: $borderLeft + $paddingLeft + $width + $paddingRight + $borderRight;
  54. html {
  55. font-family: Arial, sans-serif;
  56. overflow: hidden;
  57. }
  58. #content, #overlay, #clone {
  59. width: $width;
  60. height: $height;
  61. padding: $paddingTop $paddingRight $paddingBottom $paddingLeft;
  62. border: $borderTop solid cyan;
  63. border-right-width: $borderRight;
  64. border-bottom-width: $borderBottom;
  65. border-left-width: $borderLeft;
  66. background: #ffe;
  67. overflow: $overflow;
  68. margin-top: $margin;
  69. margin-left: $margin;
  70. text-align: justify;
  71. }
  72. #content, #clone {
  73. font-size: 12px;
  74. }
  75. #clone {
  76. position: absolute;
  77. top: $margin;
  78. left: $margin;
  79. margin: 0;
  80. opacity: .2;
  81. pointer-events: none;
  82. border-color: transparent;
  83. background: transparent;
  84. overflow: visible;
  85. width: $width;
  86. }
  87. .nested {
  88. width: $nestedWidth;
  89. background: #eee;
  90. }
  91. #overlay {
  92. position: absolute;
  93. background: transparent;
  94. left: 0;
  95. top: 0;
  96. overflow: visible;
  97. height: 0;
  98. width: 0;
  99. padding: 0;
  100. border-color: transparent;
  101. &, & * {
  102. pointer-events: none;
  103. }
  104. div {
  105. position: absolute;
  106. width: $labelWidth;
  107. height: $labelWidth;
  108. overflow: visible;
  109. &[id^="css"] {
  110. color: #00c;
  111. background: #def;
  112. top: -($borderTop + $labelSpacing + $labelWidth);
  113. left: -($borderLeft + $labelSpacing + $labelWidth);
  114. }
  115. &[id^="client"] {
  116. color: #c00;
  117. background: #fcc;
  118. top: -($borderTop + 2 * ($labelSpacing + $labelWidth));
  119. left: -($borderLeft + 2 * ($labelSpacing + $labelWidth));
  120. }
  121. &[id^="scroll"] {
  122. color: #080;
  123. background: #cfc;
  124. top: -($borderTop + 3 * ($labelSpacing + $labelWidth));
  125. left: -($borderLeft + 3 * ($labelSpacing + $labelWidth));
  126. }
  127. &[id^="offset"] {
  128. background: #ccc;
  129. top: -($borderTop + 4 * ($labelSpacing + $labelWidth));
  130. left: -($borderLeft + 4 * ($labelSpacing + $labelWidth));
  131. }
  132. &[id^="padding"] {
  133. background: gold;
  134. color: saddlebrown;
  135. }
  136. &:before, &:after {
  137. position: absolute;
  138. display: block;
  139. z-index: 1;
  140. height: 10000px;
  141. width: 10000px;
  142. }
  143. &:before {
  144. content: attr(id);
  145. }
  146. &:after {
  147. content: "";
  148. }
  149. &[id$="Width"]:before {
  150. border-left: 1px dashed;
  151. }
  152. &[id$="Width"]:before, &#paddingRight:before, &#paddingLeft:before {
  153. width: 100%;
  154. text-align: center;
  155. }
  156. &[id$="Width"]:after, &#paddingRight:after {
  157. border-right: 1px dashed;
  158. right: 0;
  159. }
  160. &#paddingRight:after {
  161. top: -($labelSpacing + $borderBottom);
  162. }
  163. &[id$="Height"]:before {
  164. border-top: 1px dashed;
  165. }
  166. &[id$="Height"]:after, &#paddingBottom:after, &#paddingTop:after {
  167. border-bottom: 1px dashed;
  168. bottom: 0;
  169. }
  170. &#paddingTop:after {
  171. left: -($paddingLeft + $width + $paddingRight + $borderRight + $labelSpacing);
  172. opacity: 0;
  173. bottom: -1px;
  174. }
  175. &#paddingTop.scrolled:after {
  176. opacity: 1;
  177. }
  178. &#paddingBottom:after {
  179. left: -($labelSpacing + $borderRight);
  180. }
  181. &#cssWidth {
  182. left: $paddingLeft;
  183. width: $width;
  184. }
  185. &#scrollWidth, &#clientWidth {
  186. left: 0;
  187. }
  188. &#offsetWidth {
  189. left: -$borderLeft;
  190. }
  191. &#cssHeight {
  192. top: $paddingTop;
  193. height: $height;
  194. text-indent: -2em;
  195. }
  196. &#scrollHeight, &#clientHeight {
  197. top: 0;
  198. text-indent: -1.5em;
  199. }
  200. &#offsetHeight {
  201. top: -$borderTop;
  202. }
  203. &#paddingTop {
  204. top: 0;
  205. height: $paddingTop;
  206. }
  207. &#paddingBottom {
  208. top: $paddingTop + $height;
  209. height: $paddingBottom;
  210. }
  211. &#paddingTop, &#paddingBottom {
  212. margin-left: $labelSpacing;
  213. left: $offsetWidth - $borderLeft;
  214. }
  215. &#paddingLeft {
  216. left: 0;
  217. width: $paddingLeft;
  218. }
  219. &#paddingRight {
  220. left: $paddingLeft + $width;
  221. width: $paddingRight;
  222. }
  223. &#paddingLeft, &#paddingRight {
  224. margin-top: $labelSpacing;
  225. top: $offsetHeight - $borderTop;
  226. }
  227. &#cssHeight {
  228. line-height: $height / 2;
  229. }
  230. &#clientHeight {
  231. line-height: $height + 2 * $paddingTop;
  232. }
  233. &#scrollHeight, &#paddingTop {
  234. line-height: $paddingTop;
  235. }
  236. &#paddingBottom {
  237. line-height: $paddingBottom;
  238. }
  239. &#offsetHeight {
  240. height: $offsetHeight;
  241. line-height: $height / 2 + $paddingTop;
  242. }
  243. &#offsetWidth {
  244. width: $offsetWidth;
  245. }
  246. }
  247. }
  248. #overlay:after {
  249. content: "Border";
  250. position: absolute;
  251. top: $offsetHeight - $borderTop;
  252. left: $offsetWidth - $borderLeft;
  253. color: #0dd;
  254. }
  255. * {
  256. -webkit-transition: opacity 1s ease;
  257. transition: opacity 1s ease;
  258. }
  259.  
  260. --------------------------------------------------------------
  261. JS:
  262.  
  263. var div = document.getElementById('content');
  264.  
  265. if (window.console && console.clear) console.clear();
  266. var scrollbarWidth = div.offsetWidth - div.clientWidth - parseFloat(getComputedStyle(div).borderLeftWidth) - parseFloat(getComputedStyle(div).borderRightWidth);
  267. if (window.console) console.log('Scrollbar width (including rounding errors):', scrollbarWidth);
  268.  
  269. ['clientWidth', 'scrollWidth', 'clientHeight', 'scrollHeight'].forEach(function (id) {
  270. document.getElementById(id).style[id.replace(/^[a-z]+/, '').toLowerCase()] = div[id] + 'px';
  271. });
  272.  
  273. div.addEventListener('scroll', function (e) {
  274. ['paddingTop', 'cssHeight', 'scrollHeight', 'clone'].forEach(function (id) {
  275. document.getElementById(id).style.marginTop = -div.scrollTop + 'px'
  276. });
  277. ['paddingLeft', 'cssWidth', 'scrollWidth', 'clone'].forEach(function (id) {
  278. document.getElementById(id).style.marginLeft = -div.scrollLeft + 'px'
  279. });
  280. document.getElementById('paddingRight').style.opacity = div.scrollLeft ? 0 : 1;
  281. document.getElementById('paddingBottom').style.opacity = div.scrollTop ? 0 : 1;
  282. document.getElementById('cssHeight').style.opacity = div.scrollTop ? 0 : 1;
  283. document.getElementById('paddingTop').classList[div.scrollTop ? 'add' : 'remove']('scrolled');
  284. });
  285.  
  286. var clone = div.cloneNode(true);
  287. clone.id = 'clone';
  288. // scrollbar spaing
  289. clone.style.width = (div.clientWidth - parseFloat(getComputedStyle(div).paddingLeft) - parseFloat(getComputedStyle(div).paddingRight)) + 'px';
  290. div.parentNode.appendChild(clone);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement