如何让 Iframe 像电影演职员表一样向下滚动

How do I get an Iframe to scroll down like movie credits?

本文关键字:一样 滚动 Iframe 演职员 电影      更新时间:2023-09-26

我有一个Iframe,它循环浏览一系列已发布的Google表格。 有没有办法让 iframe 随着时间的推移向下滚动以显示已发布 URL 中的所有数据?

理想情况下,我想让结果表从顶部开始,超过 30 秒,向下滚动到底部,然后在底部时切换到下一个结果表。 然后,这将不断从站点 1 循环到站点 5。

这是我的代码:

    var sites = [
      "https://docs.google.com/spreadsheets/d/1Gd4eYAukSIvU0VS_zt37TWNiKi15-lB3V8f5AA3IXJU/pubhtml?gid=0&single=true",
      "https://docs.google.com/spreadsheets/d/1Gd4eYAukSIvU0VS_zt37TWNiKi15-lB3V8f5AA3IXJU/pubhtml?gid=1991236368&single=true",
      "https://docs.google.com/spreadsheets/d/1Gd4eYAukSIvU0VS_zt37TWNiKi15-lB3V8f5AA3IXJU/pubhtml?gid=205619546&single=true",
      "https://docs.google.com/spreadsheets/d/1Gd4eYAukSIvU0VS_zt37TWNiKi15-lB3V8f5AA3IXJU/pubhtml?gid=1480439822&single=true",
      "https://docs.google.com/spreadsheets/d/1Gd4eYAukSIvU0VS_zt37TWNiKi15-lB3V8f5AA3IXJU/pubhtml?gid=1101347808&single=true"
    ];
    var currentSite = sites.length;
    $(document).ready(function() {
      var $iframe = $("iframe").attr("src", "https://docs.google.com/spreadsheets/d/1Gd4eYAukSIvU0VS_zt37TWNiKi15-lB3V8f5AA3IXJU/pubhtml?gid=0&single=true");
      setInterval(function() {
        (currentSite == 1) ? currentSite = sites.length - 1: currentSite = currentSite - 1;
        $iframe.attr("src", sites[currentSite]);
      }, 10000);
    });
    iframe {
      height: 768px;
      width: 1024px;
      border: 2px solid gray;
    }
    body {
      width: 1024px;
      length: 768px;
      background-color: #C9C9C9;
      margin: 0 auto;
    }
    #heading-wrapper {
      margin: 0 auto;
      background-color: #FF6600;
      border: 2px solid black;
      text-align: center;
      width: 1024px;
    }
    #heading-wrapper h1 {
      font-family: Arial;
      vertical-align: middle;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<head>
  <title>Live Results</title>
</head>
<body>
  <div id="heading-wrapper">
    <h1>Live Results</h1>
  </div>
  <iframe></iframe>
</body>

我什至不知道是否可以让它慢慢向下滚动。

看看

iframe.contentWindow.scrollTo(x, y);

您可以创建一个函数,该函数以递增的 y 滚动量定期调用该函数。

使用设置间隔并更改包含元素的 scrollTop 属性:

setInterval(changeScrollPosition, 50); // every 50 milliseconds
function changeScrollPosition(el)
{
    var currentTop = el.scrollTop;
    el.scrollTop = currentTop + howMuchYouWantToScrollBy;
}