简体   繁体   English

滚动按钮可逐步向下滚动

[英]Scroll button for gradual scroll down

Trying to create a Scroll Down Button that scrolls down at a settable speed while the scroll button is in click down position, or on tablets in touch position. 尝试创建一个向下滚动按钮,当滚动按钮处于单击向下位置时,或在处于触摸位置的平板电脑上,以可设置的速度向下滚动。 At the bottom of the page the Scroll Down Button should become a Scroll To Top Button. 在页面底部,向下滚动按钮应变为向上滚动按钮。 Not being lazy, I have searched for this but I could find all sorts of scroll methods but not this one. 并不是很懒,我已经搜索了这个,但是我可以找到各种各样的滚动方法,但是没有找到。 Can anyone please help. 谁能帮忙。 Some code to get things started (I hope). 一些使事情开始的代码(我希望)。 JSFIDDLE HERE JSFIDDLE这里

 #scrollButtonDown{ position:fixed; width:80px; height:80px; border-radius:40px; bottom:0; background-color: rgba(0, 0, 0, 0.7); color:#ffffff; text-align:center; } #scrollButtonTop{ position:fixed; width:80px; height:80px; border-radius:40px; bottom:0; background-color: rgba(0, 0, 0, 0.7); color:#ffffff; text-align:center; } 
 What is Lorem Ipsum?<br> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. <br><br> Why do we use it?<br> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). <br><br> Where does it come from?<br> Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. <br><br> The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. <br><br> Where can I get some?<br> There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc. <br><br> What is Lorem Ipsum?<br> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. <br><br> Why do we use it?<br> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). <br><br> Where does it come from?<br> Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. <br><br> The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. <br><br> Where can I get some?<br> There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc. <button id="scrollButtonDown" class="glyphicon glyphiconwchevron-down">SCROLL DOWN</button> <button id="scrollButtonTop" class="glyphicon glyphiconwchevron-up">SCROLL TOP</button> 

Use event handlers to change the state of a "scrolling" variable. 使用事件处理程序可以更改“滚动”变量的状态。 and have a setInterval running to watch for that variable to become true. 并运行setInterval来监视该变量为真。 You can change the speed by either changing the interval time, or the amount the page is moved per interval 您可以通过更改间隔时间或每个间隔移动页面的数量来更改速度

var scrolling = false;
var atBottom = false;

window.onload = function(){
  var eButton = document.getElementById("scrollButtonTop");
  eButton.addEventListener("mousedown", bDown);
  eButton.addEventListener("mouseup", bUp);
  eButton.addEventListener("mouseout", bUp);
  eButton.addEventListener("touchstart", bDown);
  eButton.addEventListener("touchend", bUp);
}
window.onscroll = function(){
  var eButton = document.getElementById("scrollButtonTop");
  var scrollPos = window.pageYOffset ||
                  document.documentElement.scrollTop || 
                  document.body.scrollTop || 
                  0;
  if(scrollPos + window.innerHeight > document.body.clientHeight - 10){
    atBottom = true;
    eButton.innerHTML = "GO&thinsp;TO<br>TOP";
    eButton.className = "glyphicon glyphicon-chevron-up";
  } else {
    atBottom = false;
    eButton.innerHTML = "SCROLL<br>DOWN";
    eButton.className = "glyphicon glyphicon-chevron-down";
  }
}
function bDown(){
  if(atBottom) window.scrollTo(0,0);
  else scrolling = true;
}
function bUp(){
  scrolling = false;
}

setInterval(function(){
  if(scrolling) window.scrollBy(0,1);
},50);

JSfiddle Here JSfiddle在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM