I use the jquery inview plugin top automatically play videos when in view. I have try many solutions from stackoverflow but it does not seem to work (solution 1, solution 2)
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery.inview/0.2/jquery.inview.min.js"></script>
<script type="text/javascript">
$('.video-autoplay').on('inview', function(event, isInView) {
if (isInView) {
$('video').trigger('play');
} else {
$('video').trigger('pause');
}
});
</script>
<body>
<div id="main_container">
<div class="navbar gallery-sub-header" style="z-index:9;">
<div class="container">
<div class="pull-left desc-follow">
<div class="single-item">
<video class=".video-autoplay" preload="auto" autoplay loop muted playsinline onclick="this.paused ? this.play() : this.pause();" poster="">
<source src="{{ Config::get('site.uploads_dir') . '/images/' . substr($item->pic_url, 0, strripos($item->pic_url, '.')) . '.webm' }}" type="video/webm">
<source src="{{ Config::get('site.uploads_dir') . '/video-gif/' . substr($item->pic_url, 0, strripos($item->pic_url, '.')) . '.mp4' }}" type="video/mp4"> Your browser does not support the video tag.
</video>
</div>
<div class="single-item">
<video class=".video-autoplay" preload="auto" autoplay loop muted playsinline onclick="this.paused ? this.play() : this.pause();" poster="">
<source src="{{ Config::get('site.uploads_dir') . '/images/' . substr($item->pic_url, 0, strripos($item->pic_url, '.')) . '.webm' }}" type="video/webm">
<source src="{{ Config::get('site.uploads_dir') . '/video-gif/' . substr($item->pic_url, 0, strripos($item->pic_url, '.')) . '.mp4' }}" type="video/mp4"> Your browser does not support the video tag.
</video>
</div>
<div class="single-item">
<video class=".video-autoplay" preload="auto" autoplay loop muted playsinline onclick="this.paused ? this.play() : this.pause();" poster="">
<source src="{{ Config::get('site.uploads_dir') . '/images/' . substr($item->pic_url, 0, strripos($item->pic_url, '.')) . '.webm' }}" type="video/webm">
<source src="{{ Config::get('site.uploads_dir') . '/video-gif/' . substr($item->pic_url, 0, strripos($item->pic_url, '.')) . '.mp4' }}" type="video/mp4"> Your browser does not support the video tag.
</video>
</div>
<div class="single-item">
<video class=".video-autoplay" preload="auto" autoplay loop muted playsinline onclick="this.paused ? this.play() : this.pause();" poster="">
<source src="{{ Config::get('site.uploads_dir') . '/images/' . substr($item->pic_url, 0, strripos($item->pic_url, '.')) . '.webm' }}" type="video/webm">
<source src="{{ Config::get('site.uploads_dir') . '/video-gif/' . substr($item->pic_url, 0, strripos($item->pic_url, '.')) . '.mp4' }}" type="video/mp4"> Your browser does not support the video tag.
</video>
</div>
</div>
</div>
</div>
</div>
</body>
You have two issues here. Firstly, due to the location of your jQuery logic, you need to wrap it in a document.ready handler. Currently you're executing the JS before the DOM has been loaded, and no elements exist.
Secondly, when the event fires you select all video
elements. From the context of the event handler I would assume you instead want to start the video which is now visible/invisible, so using $(this)
would seem more applicable. Try this:
$(function() {
$('.video-autoplay').on('inview', function(event, isInView) {
$(this).trigger(isInView ? 'play' : 'pause');
});
});