簡體   English   中英

無限自動上下滾動,如何控制/配置速度,結束暫停時間?

[英]Infinite auto scroll top-to-bottom and back, how to control/configure speed, time of pause at end?

我正在嘗試使一長頁圖像無限地上下滾動(用於展覽)。

這就是我一直在使用的代碼(在這里找到的代碼非常有用!): https : //jsfiddle.net/p7r73tke/

基本上可以滿足我的需求,但我需要對速度和暫停進行更多控制。

如何使頂部的停頓時間更長?

有沒有辦法讓它隨機暫停約1秒鍾?

有誰知道做我在想什么的更簡單方法? 也許正如samuel-liew建議的那樣,javascript並不是解決問題的最佳方法

謝謝,謝謝!

function scrollpage() {
function f() {
  window.scrollTo(0, i); //idk
  if (status == 0) {
    i = i + 50; //scroll speed top to bottom?
    if (i >= Height) {
      status = 30; //idk?
    }
  } else {
    i = i - 10; //scroll speed bottom to top?
    if (i <= 1) { // if you don't want continue scroll then remove this if condition 
      status = 0; //idk
    }
  }
  setTimeout(f, 0.01); //idk
}
f();
}
var Height = 15000; //doc height input manually
var i = 1, //idk
j = Height,
status = 0; //idk
scrollpage();

(我是新手,對JavaScript很溫柔,正如您在評論中看到的那樣)

謝謝你的幫助!

jQuery解決方案:

var speed = 10000; // 10000 = 10 seconds

var doScroll = function() {
  var direction = $(window).scrollTop() != 0 ? 0 : $(document).height() - $(window).height();
  $('html, body').animate({ scrollTop: direction }, speed, 'linear');
}

doScroll(); // once on page load
setInterval(doScroll, speed + 10); // once every X ms

演示: http : //jsfiddle.net/samliew/k35tbgau/


JavaScript的:

抱歉,我不建議您使用純JavaScript,因為您必須考慮以下因素:

  1. 跨瀏覽器問題,獲取窗口高度,文檔高度和當前滾動位置
  2. 每次調整瀏覽器大小時,都會根據內容高度重新計算滾動速度
  3. 編程動畫功能
  4. 跟蹤時間間隔和超時以及何時需要清除它們
  5. 滾動方向/狀態
  6. 考慮用戶是否手動滾動滾動條

    可能還有更多...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM