How to only play HTML5 video when in viewport without looping/replaying?










0















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.










share|improve this question


























    0















    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.










    share|improve this question
























      0












      0








      0


      1






      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 20:55









      MERICAMERICA

      12




      12






















          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
          );



          );













          draft saved

          draft discarded


















          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















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          政党