@@ -22,15 +22,49 @@ export async function init(id) {
2222 vision . iWndIndex = result . iWndIndex ;
2323 vision . inited = true ;
2424
25+ const observer = new IntersectionObserver ( ( ) => {
26+ if ( checkVisibility ( el ) ) {
27+ WebVideoCtrl . I_Resize ( el . offsetWidth , el . offsetHeight ) ;
28+ }
29+ } ) ;
30+ observer . observe ( el ) ;
31+ vision . observer = observer ;
32+
2533 return true ;
2634}
2735
2836const hackJSResize = function ( ) {
2937 const originalResize = JSVideoPlugin . prototype . JS_Resize ;
3038 JSVideoPlugin . prototype . JS_Resize = function ( e , t ) {
3139 const { szId } = this . oOptions ;
32- if ( document . getElementById ( szId ) ) {
33- return originalResize . call ( this , e , t ) ;
40+ const el = document . getElementById ( szId ) ;
41+ if ( el ) {
42+ const visible = checkVisibility ( el ) ;
43+ if ( visible ) {
44+ return originalResize . call ( this , e , t ) ;
45+ }
46+ else {
47+ WebVideoCtrl . I_HidPlugin ( ) ;
48+ }
49+ }
50+ }
51+ }
52+
53+ const hackJSShowWnd = function ( ) {
54+ const originalShowWnd = JSVideoPlugin . prototype . JS_ShowWnd ;
55+ JSVideoPlugin . prototype . JS_ShowWnd = function ( ) {
56+ const { szId } = this . oOptions ;
57+ const el = document . getElementById ( szId ) ;
58+ if ( el ) {
59+ const visible = checkVisibility ( el ) ;
60+ if ( visible ) {
61+ return originalShowWnd . call ( this ) ;
62+ }
63+ else {
64+ return new Promise ( ( resolve , reject ) => {
65+ resolve ( ) ;
66+ } ) ;
67+ }
3468 }
3569 }
3670}
@@ -91,6 +125,7 @@ const initWindow = id => {
91125 if ( result . inited === false || ( result . inited && result . iWndIndex !== - 1 ) ) {
92126 clearInterval ( handler ) ;
93127 hackJSResize ( ) ;
128+ hackJSShowWnd ( ) ;
94129 hackJSDestroyPlugin ( ) ;
95130 resolve ( result ) ;
96131 }
@@ -309,7 +344,10 @@ export function dispose(id) {
309344 const vision = Data . get ( id ) ;
310345 Data . remove ( id ) ;
311346
312- const { realPlaying, logined } = vision ;
347+ const { realPlaying, logined, observer } = vision ;
348+ if ( observer ) {
349+ observer . disconnect ( ) ;
350+ }
313351 if ( realPlaying === true ) {
314352 stopRealPlay ( id ) ;
315353 }
@@ -330,3 +368,37 @@ const getTagNameFirstValue = (xmlDoc, tagName, defaultValue = '0') => {
330368const getTagNameValues = ( xmlDoc , tagName ) => {
331369 return xmlDoc . getElementsByTagName ( tagName ) ;
332370}
371+
372+ const checkVisibility = el => {
373+ if ( el . checkVisibility ) {
374+ return el . checkVisibility ( ) ;
375+ }
376+ else {
377+ return isVisible ( el ) ;
378+ }
379+ }
380+
381+ const isVisible = ( element ) => {
382+ if ( ! element ) return false ;
383+
384+ const style = window . getComputedStyle ( element ) ;
385+ if ( style . display === 'none' || style . visibility === 'hidden' || parseFloat ( style . opacity ) < 0.01 ) {
386+ return false ;
387+ }
388+
389+ const rect = element . getBoundingClientRect ( ) ;
390+ if ( rect . width === 0 || rect . height === 0 ) {
391+ return false ;
392+ }
393+
394+ let parent = element . parentElement ;
395+ while ( parent ) {
396+ const parentStyle = window . getComputedStyle ( parent ) ;
397+ if ( parentStyle . display === 'none' || parentStyle . visibility === 'hidden' ) {
398+ return false ;
399+ }
400+ parent = parent . parentElement ;
401+ }
402+
403+ return true ;
404+ }
0 commit comments