2016-11-20 123 views
0

试图创建一个向下滚动按钮,当滚动按钮处于点击位置时,或在触摸位置的平板电脑上以可设定的速度向下滚动。在页面底部,向下滚动按钮应该成为滚动到顶部按钮。不懒惰,我已经搜索了这个,但我可以找到各种滚动方法,但不是这个。任何人都可以请帮忙。一些代码让事情开始(我希望)。 JSFIDDLE HERE滚动按钮逐渐向下滚动

#scrollButtonDown{ 
 
\t position:fixed; 
 
\t width:80px; 
 
\t height:80px; 
 
\t border-radius:40px; 
 
\t bottom:0; 
 
\t background-color: rgba(0, 0, 0, 0.7); 
 
\t color:#ffffff; 
 
\t text-align:center; 
 
\t } 
 
\t 
 
\t 
 
\t #scrollButtonTop{ 
 
\t position:fixed; 
 
\t width:80px; 
 
\t height:80px; 
 
\t border-radius:40px; 
 
\t bottom:0; 
 
\t background-color: rgba(0, 0, 0, 0.7); 
 
\t color:#ffffff; 
 
\t text-align:center; 
 
\t }
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>

回答

2

使用事件处理程序,以改变 “滚动” 变量的状态。并运行setInterval以监视该变量是否成立。 您可以通过改变时间间隔改变速度,或金额的页面每间隔

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

+0

感动谢谢,泰勒!我可以请你创建一个演示。我正在添加你的代码,但它在这里不起作用。 – Eddy

+0

@Eddy - woopsies ..我不知道我在哪里想到“addEventHandler”,我更新了我的代码以包含实际的javascript。哈。让我知道现在它是否更好。 –

+0

是的,它的工作原理!再次感谢你,泰勒! – Eddy