Autoplay video when any part of containing div is in viewport; problem in IE
Beneath some other content on a page, I have an embedded Vimeo video in an iframe within a containing div. Here's an idea of the what the HTML looks like:
<div>Some content goes here</div>
<div id="video-container">
<iframe id="video" src="https://player.vimeo.com/video/123456789?autoplay=1&loop=1&muted=1&title=0&byline=0&portrait=0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen="" width="530" height="298" frameborder="0"></iframe>
</div>
What I would like to do is have the video autoplay as soon as any part of the div containing the iframe is in the viewport, and stop it when the div is out of the viewport. I've been working on JS based on this: https://gomakethings.com/how-to-test-if-an-element-is-in-the-viewport-with-vanilla-javascript/ and this: https://gist.github.com/davidtheclark/5515733 and also this: Autoplay Youtube Video When Scrolled to. Here's what I've come up with:
<script>
var dv = document.getElementById('video-container');
var v = document.getElementById('video');
function isAnyPartOfElementInViewport(dv) {
const rect = dv.getBoundingClientRect();
const windowHeight = (window.innerHeight || document.documentElement.clientHeight);
const windowWidth = (window.innerWidth || document.documentElement.clientWidth);
const vertInView = (rect.top <= windowHeight) && ((rect.top + rect.height) >= 0);
const horInView = (rect.left <= windowWidth) && ((rect.left + rect.width) >= 0);
return (vertInView && horInView);
}
window.addEventListener('scroll', function (event) {
if (isAnyPartOfElementInViewport(dv)) {
if (v.src=='' || v.src==location.href) {
v.src='https:/player.vimeo.com/video/123456789?autoplay=1&loop=1&muted=1&title=0&byline=0&portrait=0';
}
}
else {
v.src='';
}
}, false);
</script>
This works the way I want it to on Firefox, Safari, and Chrome, but doesn't work correctly on IE11 and IE10. The video only plays in IE if it's within the viewport on page load and I don't have to scroll to it. If the video is out of the viewport to begin with, it does not show when I scroll to it.
Thanks for any help on this issue! Quick additional note: I am looking for a plain JavaScript solution (no jQuery).
Edited to add: I think the problem is with this part:
if (v.src=='' || v.src==location.href) {
If I remove this conditional statement and set a new src for the video regardless of whether the video is already playing, then it works in IE11. However, a conditional is needed because I don't want the video to reload if it's already playing.
javascript
add a comment |
Beneath some other content on a page, I have an embedded Vimeo video in an iframe within a containing div. Here's an idea of the what the HTML looks like:
<div>Some content goes here</div>
<div id="video-container">
<iframe id="video" src="https://player.vimeo.com/video/123456789?autoplay=1&loop=1&muted=1&title=0&byline=0&portrait=0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen="" width="530" height="298" frameborder="0"></iframe>
</div>
What I would like to do is have the video autoplay as soon as any part of the div containing the iframe is in the viewport, and stop it when the div is out of the viewport. I've been working on JS based on this: https://gomakethings.com/how-to-test-if-an-element-is-in-the-viewport-with-vanilla-javascript/ and this: https://gist.github.com/davidtheclark/5515733 and also this: Autoplay Youtube Video When Scrolled to. Here's what I've come up with:
<script>
var dv = document.getElementById('video-container');
var v = document.getElementById('video');
function isAnyPartOfElementInViewport(dv) {
const rect = dv.getBoundingClientRect();
const windowHeight = (window.innerHeight || document.documentElement.clientHeight);
const windowWidth = (window.innerWidth || document.documentElement.clientWidth);
const vertInView = (rect.top <= windowHeight) && ((rect.top + rect.height) >= 0);
const horInView = (rect.left <= windowWidth) && ((rect.left + rect.width) >= 0);
return (vertInView && horInView);
}
window.addEventListener('scroll', function (event) {
if (isAnyPartOfElementInViewport(dv)) {
if (v.src=='' || v.src==location.href) {
v.src='https:/player.vimeo.com/video/123456789?autoplay=1&loop=1&muted=1&title=0&byline=0&portrait=0';
}
}
else {
v.src='';
}
}, false);
</script>
This works the way I want it to on Firefox, Safari, and Chrome, but doesn't work correctly on IE11 and IE10. The video only plays in IE if it's within the viewport on page load and I don't have to scroll to it. If the video is out of the viewport to begin with, it does not show when I scroll to it.
Thanks for any help on this issue! Quick additional note: I am looking for a plain JavaScript solution (no jQuery).
Edited to add: I think the problem is with this part:
if (v.src=='' || v.src==location.href) {
If I remove this conditional statement and set a new src for the video regardless of whether the video is already playing, then it works in IE11. However, a conditional is needed because I don't want the video to reload if it's already playing.
javascript
1
if (v.src=='') || v.src==location.href) {
- that is not even syntactically correct JavaScript, you messed up the parentheses …
– misorude
Nov 14 '18 at 8:09
Do you want the video to restart from the beginning if it was already playing, I scroll it out of view, and then back to it? If not, then switching between the player URL and an empty src for the iframe doesn’t make that much sense to begin with. You would need to use the player’s API to pause the video instead.
– misorude
Nov 14 '18 at 8:12
@misorude: Thank you for catching that! That was a typo in my example; I corrected it to match my actual code.
– Mary James
Nov 14 '18 at 20:13
add a comment |
Beneath some other content on a page, I have an embedded Vimeo video in an iframe within a containing div. Here's an idea of the what the HTML looks like:
<div>Some content goes here</div>
<div id="video-container">
<iframe id="video" src="https://player.vimeo.com/video/123456789?autoplay=1&loop=1&muted=1&title=0&byline=0&portrait=0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen="" width="530" height="298" frameborder="0"></iframe>
</div>
What I would like to do is have the video autoplay as soon as any part of the div containing the iframe is in the viewport, and stop it when the div is out of the viewport. I've been working on JS based on this: https://gomakethings.com/how-to-test-if-an-element-is-in-the-viewport-with-vanilla-javascript/ and this: https://gist.github.com/davidtheclark/5515733 and also this: Autoplay Youtube Video When Scrolled to. Here's what I've come up with:
<script>
var dv = document.getElementById('video-container');
var v = document.getElementById('video');
function isAnyPartOfElementInViewport(dv) {
const rect = dv.getBoundingClientRect();
const windowHeight = (window.innerHeight || document.documentElement.clientHeight);
const windowWidth = (window.innerWidth || document.documentElement.clientWidth);
const vertInView = (rect.top <= windowHeight) && ((rect.top + rect.height) >= 0);
const horInView = (rect.left <= windowWidth) && ((rect.left + rect.width) >= 0);
return (vertInView && horInView);
}
window.addEventListener('scroll', function (event) {
if (isAnyPartOfElementInViewport(dv)) {
if (v.src=='' || v.src==location.href) {
v.src='https:/player.vimeo.com/video/123456789?autoplay=1&loop=1&muted=1&title=0&byline=0&portrait=0';
}
}
else {
v.src='';
}
}, false);
</script>
This works the way I want it to on Firefox, Safari, and Chrome, but doesn't work correctly on IE11 and IE10. The video only plays in IE if it's within the viewport on page load and I don't have to scroll to it. If the video is out of the viewport to begin with, it does not show when I scroll to it.
Thanks for any help on this issue! Quick additional note: I am looking for a plain JavaScript solution (no jQuery).
Edited to add: I think the problem is with this part:
if (v.src=='' || v.src==location.href) {
If I remove this conditional statement and set a new src for the video regardless of whether the video is already playing, then it works in IE11. However, a conditional is needed because I don't want the video to reload if it's already playing.
javascript
Beneath some other content on a page, I have an embedded Vimeo video in an iframe within a containing div. Here's an idea of the what the HTML looks like:
<div>Some content goes here</div>
<div id="video-container">
<iframe id="video" src="https://player.vimeo.com/video/123456789?autoplay=1&loop=1&muted=1&title=0&byline=0&portrait=0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen="" width="530" height="298" frameborder="0"></iframe>
</div>
What I would like to do is have the video autoplay as soon as any part of the div containing the iframe is in the viewport, and stop it when the div is out of the viewport. I've been working on JS based on this: https://gomakethings.com/how-to-test-if-an-element-is-in-the-viewport-with-vanilla-javascript/ and this: https://gist.github.com/davidtheclark/5515733 and also this: Autoplay Youtube Video When Scrolled to. Here's what I've come up with:
<script>
var dv = document.getElementById('video-container');
var v = document.getElementById('video');
function isAnyPartOfElementInViewport(dv) {
const rect = dv.getBoundingClientRect();
const windowHeight = (window.innerHeight || document.documentElement.clientHeight);
const windowWidth = (window.innerWidth || document.documentElement.clientWidth);
const vertInView = (rect.top <= windowHeight) && ((rect.top + rect.height) >= 0);
const horInView = (rect.left <= windowWidth) && ((rect.left + rect.width) >= 0);
return (vertInView && horInView);
}
window.addEventListener('scroll', function (event) {
if (isAnyPartOfElementInViewport(dv)) {
if (v.src=='' || v.src==location.href) {
v.src='https:/player.vimeo.com/video/123456789?autoplay=1&loop=1&muted=1&title=0&byline=0&portrait=0';
}
}
else {
v.src='';
}
}, false);
</script>
This works the way I want it to on Firefox, Safari, and Chrome, but doesn't work correctly on IE11 and IE10. The video only plays in IE if it's within the viewport on page load and I don't have to scroll to it. If the video is out of the viewport to begin with, it does not show when I scroll to it.
Thanks for any help on this issue! Quick additional note: I am looking for a plain JavaScript solution (no jQuery).
Edited to add: I think the problem is with this part:
if (v.src=='' || v.src==location.href) {
If I remove this conditional statement and set a new src for the video regardless of whether the video is already playing, then it works in IE11. However, a conditional is needed because I don't want the video to reload if it's already playing.
javascript
javascript
edited Nov 14 '18 at 20:07
asked Nov 13 '18 at 5:22
Mary James
14218
14218
1
if (v.src=='') || v.src==location.href) {
- that is not even syntactically correct JavaScript, you messed up the parentheses …
– misorude
Nov 14 '18 at 8:09
Do you want the video to restart from the beginning if it was already playing, I scroll it out of view, and then back to it? If not, then switching between the player URL and an empty src for the iframe doesn’t make that much sense to begin with. You would need to use the player’s API to pause the video instead.
– misorude
Nov 14 '18 at 8:12
@misorude: Thank you for catching that! That was a typo in my example; I corrected it to match my actual code.
– Mary James
Nov 14 '18 at 20:13
add a comment |
1
if (v.src=='') || v.src==location.href) {
- that is not even syntactically correct JavaScript, you messed up the parentheses …
– misorude
Nov 14 '18 at 8:09
Do you want the video to restart from the beginning if it was already playing, I scroll it out of view, and then back to it? If not, then switching between the player URL and an empty src for the iframe doesn’t make that much sense to begin with. You would need to use the player’s API to pause the video instead.
– misorude
Nov 14 '18 at 8:12
@misorude: Thank you for catching that! That was a typo in my example; I corrected it to match my actual code.
– Mary James
Nov 14 '18 at 20:13
1
1
if (v.src=='') || v.src==location.href) {
- that is not even syntactically correct JavaScript, you messed up the parentheses …– misorude
Nov 14 '18 at 8:09
if (v.src=='') || v.src==location.href) {
- that is not even syntactically correct JavaScript, you messed up the parentheses …– misorude
Nov 14 '18 at 8:09
Do you want the video to restart from the beginning if it was already playing, I scroll it out of view, and then back to it? If not, then switching between the player URL and an empty src for the iframe doesn’t make that much sense to begin with. You would need to use the player’s API to pause the video instead.
– misorude
Nov 14 '18 at 8:12
Do you want the video to restart from the beginning if it was already playing, I scroll it out of view, and then back to it? If not, then switching between the player URL and an empty src for the iframe doesn’t make that much sense to begin with. You would need to use the player’s API to pause the video instead.
– misorude
Nov 14 '18 at 8:12
@misorude: Thank you for catching that! That was a typo in my example; I corrected it to match my actual code.
– Mary James
Nov 14 '18 at 20:13
@misorude: Thank you for catching that! That was a typo in my example; I corrected it to match my actual code.
– Mary James
Nov 14 '18 at 20:13
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%2f53274317%2fautoplay-video-when-any-part-of-containing-div-is-in-viewport-problem-in-ie%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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53274317%2fautoplay-video-when-any-part-of-containing-div-is-in-viewport-problem-in-ie%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
1
if (v.src=='') || v.src==location.href) {
- that is not even syntactically correct JavaScript, you messed up the parentheses …– misorude
Nov 14 '18 at 8:09
Do you want the video to restart from the beginning if it was already playing, I scroll it out of view, and then back to it? If not, then switching between the player URL and an empty src for the iframe doesn’t make that much sense to begin with. You would need to use the player’s API to pause the video instead.
– misorude
Nov 14 '18 at 8:12
@misorude: Thank you for catching that! That was a typo in my example; I corrected it to match my actual code.
– Mary James
Nov 14 '18 at 20:13