I'm building a Javascript application that requires some custom scrolling to be implemented for some text that overflows a container. In the environment that I'm building for there are no scroll bars, so I need to create two buttons, one up and one down, which the user can then user to scroll the text.
I've created a very basic implementation with this jsFiddle.
What I'm struggling with is to come up with a clean solution that stops the scrolling when the bottom of the text visible at the bottom of the container, and visa versa, preventing the scroll text from moving up/down forever.
Perhaps there is a well known algorithm for this? I'm looking for a way to do this without plug-ins and without jQuery.
Here is my Javascript:
var upButton = document.getElementById('upButton');
var downButton = document.getElementById('downButton');
var text = document.getElementById('inner');
var container = document.getElementById('container');
upButton.addEventListener('click', function () {
moveText(1);
});
downButton.addEventListener('click', function () {
moveText(-1);
});
function moveText(direction) {
var textTop = parseInt(window.getComputedStyle(text).top);
var moveTo = textTop + (direction * 50);
text.style.top = moveTo + "px"
}
HTML:
<div id="container">
<div id="inner">Nam liber ... very long text ... facilisi.</div>
</div>
<div class="buttons">
<button id="upButton" type="button">Up</button>
<button id="downButton" type="button">Down</button>
</div>
CSS:
#container {
background: #F1F1F1;
width: 300px;
height: 200px;
margin: 0 auto;
overflow: hidden;
font-size: 20px;
line-height: 30px;
padding: 20px;
}
#inner {
position: relative;
top: 0;
}