|
4 | 4 | * https://github.com/sdecima/javascript-detect-element-resize |
5 | 5 | * Sebastian Decima |
6 | 6 | * |
7 | | -* version: 0.5.2 |
| 7 | +* version: 0.5.3 |
8 | 8 | **/ |
9 | 9 |
|
10 | 10 | (function () { |
|
56 | 56 | } |
57 | 57 | }); |
58 | 58 | }; |
| 59 | + |
| 60 | + /* Detect CSS Animations support to detect element display/re-attach */ |
| 61 | + var animation = false, |
| 62 | + animationstring = 'animation', |
| 63 | + keyframeprefix = '', |
| 64 | + animationstartevent = 'animationstart', |
| 65 | + domPrefixes = 'Webkit Moz O ms'.split(' '), |
| 66 | + startEvents = 'webkitAnimationStart animationstart oAnimationStart MSAnimationStart'.split(' '), |
| 67 | + pfx = ''; |
| 68 | + { |
| 69 | + var elm = document.createElement('fakeelement'); |
| 70 | + if( elm.style.animationName !== undefined ) { animation = true; } |
| 71 | + |
| 72 | + if( animation === false ) { |
| 73 | + for( var i = 0; i < domPrefixes.length; i++ ) { |
| 74 | + if( elm.style[ domPrefixes[i] + 'AnimationName' ] !== undefined ) { |
| 75 | + pfx = domPrefixes[ i ]; |
| 76 | + animationstring = pfx + 'Animation'; |
| 77 | + keyframeprefix = '-' + pfx.toLowerCase() + '-'; |
| 78 | + animationstartevent = startEvents[ i ]; |
| 79 | + animation = true; |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + var animationName = 'resizeanim'; |
| 87 | + var animationKeyframes = '@' + keyframeprefix + 'keyframes ' + animationName + ' { from { opacity: 0; } to { opacity: 0; } } '; |
| 88 | + var animationStyle = keyframeprefix + 'animation: 1ms ' + animationName + '; '; |
59 | 89 | } |
60 | 90 |
|
61 | 91 | function createStyles() { |
62 | 92 | if (!stylesCreated) { |
63 | 93 | //opacity:0 works around a chrome bug https://code.google.com/p/chromium/issues/detail?id=286360 |
64 | | - var css = '.resize-triggers { visibility: hidden; opacity: 0; } .resize-triggers, .resize-triggers > div, .contract-trigger:before { content: \" \"; display: block; position: absolute; top: 0; left: 0; height: 100%; width: 100%; overflow: hidden; } .resize-triggers > div { background: #eee; overflow: auto; } .contract-trigger:before { width: 200%; height: 200%; }', |
| 94 | + var css = (animationKeyframes ? animationKeyframes : '') + |
| 95 | + '.resize-triggers { ' + (animationStyle ? animationStyle : '') + 'visibility: hidden; opacity: 0; } ' + |
| 96 | + '.resize-triggers, .resize-triggers > div, .contract-trigger:before { content: \" \"; display: block; position: absolute; top: 0; left: 0; height: 100%; width: 100%; overflow: hidden; } .resize-triggers > div { background: #eee; overflow: auto; } .contract-trigger:before { width: 200%; height: 200%; }', |
65 | 97 | head = document.head || document.getElementsByTagName('head')[0], |
66 | 98 | style = document.createElement('style'); |
67 | 99 |
|
|
87 | 119 | element.__resizeListeners__ = []; |
88 | 120 | (element.__resizeTriggers__ = document.createElement('div')).className = 'resize-triggers'; |
89 | 121 | element.__resizeTriggers__.innerHTML = '<div class="expand-trigger"><div></div></div>' + |
90 | | - '<div class="contract-trigger"></div>'; |
| 122 | + '<div class="contract-trigger"></div>'; |
91 | 123 | element.appendChild(element.__resizeTriggers__); |
92 | 124 | resetTriggers(element); |
93 | 125 | element.addEventListener('scroll', scrollListener, true); |
| 126 | + |
| 127 | + /* Listen for a css animation to detect element display/re-attach */ |
| 128 | + animationstartevent && element.__resizeTriggers__.addEventListener(animationstartevent, function(e) { |
| 129 | + if(e.animationName == animationName) |
| 130 | + resetTriggers(element); |
| 131 | + }); |
94 | 132 | } |
95 | 133 | element.__resizeListeners__.push(fn); |
96 | 134 | } |
|
0 commit comments