Eddy Eddy - 8 days ago 5
Javascript Question

Video not showing in safari, but only does show after a bit of scrolling

I have a video that, while using Safari, is not responding the way it should. It does load because I can hear the sound of the video, but I can only see the video after I've scrolled a bit. In other words: the video is not visible till i scroll the browser window. Please Check here and click on a red bag.

This is the code I have used:



$('#video-togglebutton').on('click', function() {
var videoDiv = $('#videoDiv').toggle();

if (videoDiv.is(':visible')) {
$('#video').get(0).load();
$('#video').get(0).play();
} else {
$('#video').get(0).pause();
}
});

$(document).ready(function() {
$('#video').on('ended', function() {
$('#video').get(0).pause();
$('#videoDiv').toggle();
});
});

#videoDiv {
display:none;
left: 50%;
transform: translate(-50%, 0);
height: 35vw;
position: relative;
text-align:center;

}

#videoBlock{
width:60vw;
height: 35vw;
position: absolute;
top: 0;
left: 0;
left: 50%;
transform: translate(-50%, 0);

}

.videoClick {
text-align: center;
}

.videoClick a {
color: white;
font-size: 1.7em;
cursor: pointer;
cursor: hand
}


video {
background-image: url("{{ 'fotel-photography-loading-3.svg' | url_asset }}");
background-repeat: no-repeat;
background-size: 100px 100px;
background-position: center;
margin-top:-34px;
width:100%;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="video-togglebutton">Toggle video</button>
<div id="videoDiv" style="display:none">
<div id="videoBlock">
<video preload="preload" id="video">
<source src="https://static.webshopapp.com/shops/054833/files/093143183/fotel-photography-course-tour-workshop-video-4.mp4" type="video/mp4">
</video>
</div>
</div>





Now I considered a hack, using this extra code below into the if statement above, but I does not work.

if(video.readyState == 4){
$(window).scrollTop($(window).scrollTop()+1);
$(window).scrollTop($(window).scrollTop()-1);
}

Answer

This works (bit of a hack):

/*makes window scroll down and up again one pixel, after page is loaded*/  
$(window).load(function(){
   $(window).scrollTop($(window).scrollTop()+1);
   $(window).scrollTop($(window).scrollTop()-1); 
}