admin管理员组文章数量:1399491
I have a button (div-element) on my website which is in the bottom-right corner of the website. Now i want to add a jquery function that when the user clicks on the button and holds it the page is scrolling down pixel by pixel on my website (not jumping to some anchor div).
My button:
<div id="scroll-icon">
<i class="fa fa-long-arrow-down" aria-hidden="true"></i>
</div>
The CSS:
#scroll-icon {
color: $color-leuchterred;
display: none;
position: fixed; right: 3.5rem; bottom: 3rem;
font-size: 3rem;
}
I have a button (div-element) on my website which is in the bottom-right corner of the website. Now i want to add a jquery function that when the user clicks on the button and holds it the page is scrolling down pixel by pixel on my website (not jumping to some anchor div).
My button:
<div id="scroll-icon">
<i class="fa fa-long-arrow-down" aria-hidden="true"></i>
</div>
The CSS:
#scroll-icon {
color: $color-leuchterred;
display: none;
position: fixed; right: 3.5rem; bottom: 3rem;
font-size: 3rem;
}
Share
Improve this question
asked Jan 10, 2017 at 19:09
public9nfpublic9nf
1,4093 gold badges20 silver badges52 bronze badges
2 Answers
Reset to default 3Take a look at this. It will run until you mouseup again on the window. Going down 1 pixel at a time. Play around with the numbers until it feels right.
JavaScript:
$('#scroll-icon').mousedown(function(){
timeout = setInterval(function(){
window.scrollBy(0,1); // May need to be -1 to go down
}, 0); // Play around with this number. May go too fast
return false;
});
$(document).mouseup(function(){
clearInterval(timeout);
return false;
});
CSS:
#scroll-icon {
color: $color-leuchterred;
display: none;
position: fixed; right: 3.5rem; bottom: 3rem;
font-size: 3rem;
}
HTML:
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="scroll-icon">
<i class="fa fa-long-arrow-down" aria-hidden="true"></i>
</div>
Try this. Page will scroll down pixel by pixel until mouse button is held down.
HTML:
<div id="scroll-icon">
<i class="fa fa-long-arrow-down" aria-hidden="true">V</i>
</div>
CSS:
#scroll-icon {
color: red;
position: fixed; right: 3.5rem; bottom: 3rem;
font-size: 3rem;
}
#scroll-icon:hover{
cursor:pointer;
}
body{
height: 1000px;
}
JS:
$("#scroll-icon").on("mousedown", function(){
timeout = setInterval(function(){
var cs = $("body").scrollTop();
$("body").scrollTop(cs+1)
}, 10); // <--- Change this value to speed up/slow down scrolling
return false;
});
$("#scroll-icon").on("mouseup", function(){
clearInterval(timeout);
return false;
});
Check also my Fiddle: https://jsfiddle/5w4zybcr/1/
I'd like to point out that this question was very helpfull and wasn't so hard to find ;) Hope I could help
本文标签: javascriptScrolling with click on buttonStack Overflow
版权声明:本文标题:javascript - Scrolling with click on button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744185407a2594268.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论