【问题标题】:jquery Scroll automaticly, play videos, restartjquery 自动滚动,播放视频,重启
【发布时间】:2016-01-03 23:17:14
【问题描述】:

我有一个网页,其中包含使用 javascript video 嵌入的文本和视频。我可以使用哪些 javascript/jquery 代码以设定的速度向下滚动页面、停止滚动、播放视频,然后继续滚动直到页面底部重新启动?

编辑: 我试过http://www.jquerybyexample.net/2013/05/jquery-scroll-page-automatically-by-few-pixels-after-every-few-seconds.html和http://jdsharp.us/jQuery/plugins/AutoScroll/和http://jsfiddle.net/NaP8D/11/。

//run instantly and then goes after (setTimeout interval)

$("html, body").animate({ scrollTop: $(document).height() }, 4000);
setTimeout(function() {
   $('html, body').animate({scrollTop:0}, 4000); 
},4000);
setInterval(function(){
     // 4000 - it will take 4 secound in total from the top of the page to the bottom
$("html, body").animate({ scrollTop: $(document).height() }, 4000);
setTimeout(function() {
   $('html, body').animate({scrollTop:0}, 4000); 
},4000);

},8000);

我无法让代码暂停其中任何一个。

【问题讨论】:

  • 编辑了帖子并添加了我尝试过的内容。
  • 您的代码中没有任何地方等待视频播放/等待视频停止播放。这就是你遇到的问题吗?
  • 该代码来自 jsfiddle。但是,是的,该代码有效,暂停滚动并使其等待是问题所在。
  • 好的,给我几分钟时间整理一下代码。

标签: javascript jquery scroll html5-video autoscroll


【解决方案1】:

这应该让您大致了解您需要什么。如果没有一些 HTML 代码或更多的 JS,我无法给出具体的答案。这里,#myVideo 是视频的id。

// scrolls to the video
$("html, body").animate({ scrollTop: $("#myVideo").offset().top }, 4000);

// $("#myVideo") should return a video element
var video = $("#myVideo");

// called when the video/playlist ends
video.onended = function() {
     // scrolls to the bottom of the page
     $("html, body").animate({ scrollTop: $(document).height() }, 4000);
};

您将不得不稍微修改它以匹配您的代码。如果你想循环它:

while(1){
  $("html, body").animate({ scrollTop: $("#myVideo").offset().top }, 4000);
  var video = $("#myVideo");
  video.onended = function() {
    $("html, body").animate({ scrollTop: $(document).height() }, 4000);
    setTimeout(function() {
        $("html, body").animate({ scrollTop: $(0).height() }, 4000);
    },4000);
  };
}

【讨论】:

  • 代码中的“4000”是做什么的?如果改变它会控制什么?
  • 向下滚动到视频需要多长时间。
  • 另外,为了刷新一切需要添加什么,即创建一个无限运行的页面?
  • 4000 = 4 秒吗?
  • 是的,4 秒。有几种方法可以做到这一点,最愚蠢的是将所有内容放在 while(1) 循环中。我会将其添加到我的答案中。
猜你喜欢
  • 2017-04-04
  • 2013-02-13
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
  • 2014-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多