-->

Stop JWPlayer when hidden the div with the player

2020-07-27 02:40发布

问题:

I have a site where I'm using a JavaScript to add or remove a css class. Using this method I hide a div or show a div, as I need it. It works great.

The problem is one div opens a Video with a JWplayer, when I hide the "window" or better say, the div. The sound is still playing, so I need a code to put stop on the video when someone clicks the "hide" button or close button.

How do you do that? I have no idea.

Thanks

HTML

 <div class="videoquon">
         <div class='my-video-close'></div>
<div id='my-video'></div>
<script type='text/javascript'>
    jwplayer('my-video').setup({
        file: 'qvideo.f4v',
        width: '600',
        height: '337'
    });
</script>
</div>

JavaScript

  $('div.vidreveal a').click(
                function(event) {
                    event.stopPropagation();
                    $('div.videoquon').slideToggle(300);
                }
            );

   $('div.my-video-close').click(
                function(event) {
                    event.stopPropagation();
                    $('div.videoquon').slideToggle(300);
                }
            );

回答1:

I don't know JW Player, but judging by their docs, I'm going to go with

$( 'div.my-video-close' ).click( function( event ) {
    event.stopPropagation();
    jwplayer( 'my-video' ).stop();
    $( 'div.videoquon' ).slideToggle( 300 );
});

...or jwplayer( 'my-video' ).pause(); depending on the desired effect.