Hello, everyone,
Unfortunately, since I am not really competent in JavaScript, I have problems with the following code. I found the code snippet on the Internet and installed it. It should cause a video to start as soon as it becomes visible on the screen when scrolling. Unfortunately, my code doesn’t work - the video won’t start.
I have to admit that I no longer know if the code is generic JavaScript or belongs to JQuery or some other collection, maybe JS Bin. To be on the safe side, I integrated JQuery. Since I urgently need the function for commissioned work, I would be very grateful if you could take a look at it. It would be even better if I could get some code for JavaScript or JQuery from you that does this job. Would it be possible? Here is the code, for a better overview the whole HTML page:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="javascript/jquery-3.6.0.js" type="text/javascript"></script>.
<style>
#header{
/* height:500px;
background-color: #000; */
}
#video1 {
position: absolute;
left: 200px;
top: 1000px;
}
</style>
<script language="JavaScript" type="text/JavaScript">
<!--
$(window).scroll(function(e)
{
var offsetRange = $(window).height() / 3,
offsetTop = $(window).scrollTop() + offsetRange + $("#header").outerHeight(true),
offsetBottom = offsetTop + offsetRange;
$("#video1").each(function () {
var y1 = $(this).offset().top;
var y2 = offsetTop;
if (y1 + $(this).outerHeight(true) < y2 || y1 > offsetBottom) {
this.pause();
} else {
this.play();
}
});
});
}
//-->
</script>
</head>
<body style="width: 4000px; height: 4000px;">
<div id="header"></div>
<div id="info"></div>
<div id="down">scroll down please...</div>
<div>
<video id="video1" source src="test1.mp4" width="30%"></video>
</div>
</body>
</html>
I would be very grateful for your help.