|
11 | 11 | // Default is to fail on error, no placeholder
|
12 | 12 | imagePlaceholder: undefined,
|
13 | 13 | // Default cache bust is false, it will use the cache
|
14 |
| - cacheBust: false |
| 14 | + cacheBust: false, |
| 15 | + // Default scroll fix is false, it will not try to fix scrollbars |
| 16 | + scrollFix: false |
15 | 17 | };
|
16 | 18 |
|
17 | 19 | var domtoimage = {
|
|
147 | 149 | } else {
|
148 | 150 | domtoimage.impl.options.cacheBust = options.cacheBust;
|
149 | 151 | }
|
| 152 | + |
| 153 | + if(typeof(options.scrollFix) === 'undefined') { |
| 154 | + domtoimage.impl.options.scrollFix = defaultOptions.scrollFix; |
| 155 | + } else { |
| 156 | + domtoimage.impl.options.scrollFix = options.scrollFix; |
| 157 | + } |
150 | 158 | }
|
151 | 159 |
|
152 | 160 | function draw(domNode, options) {
|
|
244 | 252 | });
|
245 | 253 | }
|
246 | 254 | }
|
| 255 | + |
| 256 | + if(domtoimage.impl.options.scrollFix && |
| 257 | + (original.scrollTop || original.scrollLeft)) { |
| 258 | + // Setup container for absolute positioning of children |
| 259 | + clone.style.position = 'relative'; |
| 260 | + clone.style.overflow = 'hidden'; |
| 261 | + clone.style.width = original.offsetWidth + 'px'; |
| 262 | + clone.style.height = original.offsetHeight + 'px'; |
| 263 | + var scrollTopRemaining = original.scrollTop > 0 ? original.scrollTop : null; |
| 264 | + var scrollLeftRemaining = original.scrollLeft > 0 ? original.scrollLeft : null; |
| 265 | + var originalOffsetTop = original.offsetTop; |
| 266 | + var originalOffsetLeft = original.offsetLeft; |
| 267 | + |
| 268 | + var childTop, childTop2, childLeft, childLeft2, isStackingLeft, isStackingTop; |
| 269 | + // Loop through children and set position based on original |
| 270 | + // childs position and original contains scroll position |
| 271 | + for(var i = 0; i < clone.children.length; i++) { |
| 272 | + // Make sure this element is stylable |
| 273 | + if(typeof(clone.children[i]) === 'undefined' || |
| 274 | + clone.children[i] === null || |
| 275 | + typeof(clone.children[i].style) === 'undefined') { |
| 276 | + |
| 277 | + continue; |
| 278 | + } |
| 279 | + |
| 280 | + // Set child to absolute positioning relative to parent (container) |
| 281 | + clone.children[i].style.position = 'absolute'; |
| 282 | + if(typeof(original.children[i - 1]) !== 'undefined') { |
| 283 | + childTop = original.children[i - 1].offsetTop; |
| 284 | + childTop2 = original.children[i].offsetTop; |
| 285 | + |
| 286 | + childLeft = original.children[i - 1].offsetLeft; |
| 287 | + childLeft2 = original.children[i].offsetLeft; |
| 288 | + |
| 289 | + // isStackingLeft is true when elements are being displayed inline |
| 290 | + isStackingLeft = childLeft !== childLeft2; |
| 291 | + // isStackingLeft is true when elements are being displayed block |
| 292 | + isStackingTop = childTop !== childTop2; |
| 293 | + |
| 294 | + if(scrollTopRemaining && isStackingTop) { |
| 295 | + // Subtract the previous child's height from the scroll top |
| 296 | + // so that our current child will display underneath it |
| 297 | + scrollTopRemaining -= original.children[i - 1].offsetHeight; |
| 298 | + } |
| 299 | + if(scrollLeftRemaining && isStackingLeft) { |
| 300 | + // Subtract the previous child's width from the scroll left |
| 301 | + // so that our current child will display beside it |
| 302 | + scrollLeftRemaining -= original.children[i - 1].offsetWidth; |
| 303 | + } |
| 304 | + } |
| 305 | + |
| 306 | + |
| 307 | + if(scrollTopRemaining) { |
| 308 | + clone.children[i].style.top = -scrollTopRemaining + 'px'; |
| 309 | + } |
| 310 | + else { |
| 311 | + // We don't have a scroll top, but we still need to set |
| 312 | + // the top positioning so that our absolute elements don't |
| 313 | + // appear overlapping vertically |
| 314 | + childTop2 = original.children[i].offsetTop; |
| 315 | + clone.children[i].style.top = (childTop2 - originalOffsetTop) + 'px'; |
| 316 | + } |
| 317 | + |
| 318 | + if(scrollLeftRemaining) { |
| 319 | + clone.children[i].style.left = -scrollLeftRemaining + 'px'; |
| 320 | + } |
| 321 | + else { |
| 322 | + // We don't have a scroll left, but we still need to set |
| 323 | + // the left positioning so that our absolute elements don't |
| 324 | + // appear overlapping horizontally |
| 325 | + childLeft2 = original.children[i].offsetLeft; |
| 326 | + clone.children[i].style.left = (childLeft2 - originalOffsetLeft) + 'px'; |
| 327 | + } |
| 328 | + } |
| 329 | + } |
247 | 330 | }
|
248 | 331 |
|
249 | 332 | function clonePseudoElements() {
|
|
0 commit comments