【问题标题】:How do I get an Iframe to scroll down like movie credits?如何让 iframe 像电影学分一样向下滚动?
【发布时间】:2016-03-08 16:52:00
【问题描述】:

我有一个 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>

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

【问题讨论】:

    标签: javascript jquery html css iframe


    【解决方案1】:

    看看

    iframe.contentWindow.scrollTo(x, y);
    

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-04
        • 1970-01-01
        • 1970-01-01
        • 2011-02-12
        • 2023-01-24
        • 2011-01-22
        • 1970-01-01
        相关资源
        最近更新 更多