How to only play HTML5 video when in viewport without looping/replaying?
I've got an HTML5 video that I need to autoplay when it is scrolled to, and then pause on the last frame of the video, remaining like that for the rest of the time the user is on the page, regardless of where they are on it.
So far I've got the video to autoplay on scroll using JQuery, and it pauses when you scroll past it, but the video is only about 10 seconds long. Once the video finishes, if I scroll at all it restarts the video, which I don't want.
My code right now is set up as such:
HTML:
<div id="div-id">
<video id="video_id"
width="400px"
poster="image.jpg"
src="video.mp4" type="video/mp4" muted>
</video>
</div>
Javascript:
$(window).scroll(function(e)
var offsetRange = $(window).height() / 3,
offsetTop = $(window).scrollTop() + offsetRange +
$("#div-id").outerHeight(true),
offsetBottom = offsetTop + offsetRange;
$("#video_id").each(function () y1 > offsetBottom)
this.pause();
else
this.play();
);
);
});
Any ideas on how to modify the Javascript so that once the video finishes it won't replay on scroll or as it comes in/out of viewport? I've tried a few different things with no luck.
javascript jquery html5 html5-video
add a comment |
I've got an HTML5 video that I need to autoplay when it is scrolled to, and then pause on the last frame of the video, remaining like that for the rest of the time the user is on the page, regardless of where they are on it.
So far I've got the video to autoplay on scroll using JQuery, and it pauses when you scroll past it, but the video is only about 10 seconds long. Once the video finishes, if I scroll at all it restarts the video, which I don't want.
My code right now is set up as such:
HTML:
<div id="div-id">
<video id="video_id"
width="400px"
poster="image.jpg"
src="video.mp4" type="video/mp4" muted>
</video>
</div>
Javascript:
$(window).scroll(function(e)
var offsetRange = $(window).height() / 3,
offsetTop = $(window).scrollTop() + offsetRange +
$("#div-id").outerHeight(true),
offsetBottom = offsetTop + offsetRange;
$("#video_id").each(function () y1 > offsetBottom)
this.pause();
else
this.play();
);
);
});
Any ideas on how to modify the Javascript so that once the video finishes it won't replay on scroll or as it comes in/out of viewport? I've tried a few different things with no luck.
javascript jquery html5 html5-video
add a comment |
I've got an HTML5 video that I need to autoplay when it is scrolled to, and then pause on the last frame of the video, remaining like that for the rest of the time the user is on the page, regardless of where they are on it.
So far I've got the video to autoplay on scroll using JQuery, and it pauses when you scroll past it, but the video is only about 10 seconds long. Once the video finishes, if I scroll at all it restarts the video, which I don't want.
My code right now is set up as such:
HTML:
<div id="div-id">
<video id="video_id"
width="400px"
poster="image.jpg"
src="video.mp4" type="video/mp4" muted>
</video>
</div>
Javascript:
$(window).scroll(function(e)
var offsetRange = $(window).height() / 3,
offsetTop = $(window).scrollTop() + offsetRange +
$("#div-id").outerHeight(true),
offsetBottom = offsetTop + offsetRange;
$("#video_id").each(function () y1 > offsetBottom)
this.pause();
else
this.play();
);
);
});
Any ideas on how to modify the Javascript so that once the video finishes it won't replay on scroll or as it comes in/out of viewport? I've tried a few different things with no luck.
javascript jquery html5 html5-video
I've got an HTML5 video that I need to autoplay when it is scrolled to, and then pause on the last frame of the video, remaining like that for the rest of the time the user is on the page, regardless of where they are on it.
So far I've got the video to autoplay on scroll using JQuery, and it pauses when you scroll past it, but the video is only about 10 seconds long. Once the video finishes, if I scroll at all it restarts the video, which I don't want.
My code right now is set up as such:
HTML:
<div id="div-id">
<video id="video_id"
width="400px"
poster="image.jpg"
src="video.mp4" type="video/mp4" muted>
</video>
</div>
Javascript:
$(window).scroll(function(e)
var offsetRange = $(window).height() / 3,
offsetTop = $(window).scrollTop() + offsetRange +
$("#div-id").outerHeight(true),
offsetBottom = offsetTop + offsetRange;
$("#video_id").each(function () y1 > offsetBottom)
this.pause();
else
this.play();
);
);
});
Any ideas on how to modify the Javascript so that once the video finishes it won't replay on scroll or as it comes in/out of viewport? I've tried a few different things with no luck.
javascript jquery html5 html5-video
javascript jquery html5 html5-video
asked Nov 15 '18 at 20:55
MERICAMERICA
12
12
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327762%2fhow-to-only-play-html5-video-when-in-viewport-without-looping-replaying%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327762%2fhow-to-only-play-html5-video-when-in-viewport-without-looping-replaying%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown