How to animate List from Down to Up

161 views Asked by At

I am creating a small animation which work like Up to Down and once it will scroll till end then it will go Down to Up and it will repeat again and again.

<style>
#scrollList{height:150px; width:200px; overflow:auto; margin:40px auto;}
#scrollList li{height:45px;}
</style>
<script type="text/javascript">
//document.getElementById("scrollList").setAttribute("onClick","scrollList()");
function scrollList() {
//var scrollHeight = document.getElementById("scrollList").scrollTop;
//console.log(scrollHeight);

var ActualscrollHeight = document.getElementById("scrollList").clientHeight;
console.log(ActualscrollHeight);

setInterval(function(){
var scrollHeight = document.getElementById("scrollList").scrollTop;
if(scrollHeight <= ActualscrollHeight){
scrollHeight = scrollHeight + 1;
//console.log(scrollHeight);
document.getElementById("scrollList").scrollTop=scrollHeight;
}


}, 3);
}
</script>

<body onload="scrollList()">
<ul id="scrollList">
<li>Scroll Text</li>
<li>Scroll Text</li>
<li>Scroll Text</li>
<li>Scroll Text</li>
<li>Scroll Text</li>
<li>Scroll Text</li>
<li>Scroll Text</li>
</ul>
2

There are 2 answers

0
Anujith On BEST ANSWER

Try this:

function scrollList() {
    var ActualscrollHeight = document.getElementById("scrollList").clientHeight;
    var down = true;
    setInterval(function () {
        var scrollHeight = document.getElementById("scrollList").scrollTop;
        if (scrollHeight == 0) {
            down = true;
        } else if (scrollHeight >= ActualscrollHeight) {
            down = false;
        }
        scrollHeight = (!down) ? scrollHeight - 1 : scrollHeight + 1;
        document.getElementById("scrollList").scrollTop = scrollHeight;
    }, 3);
}

Fiddle

2
Syed Muhammad Zeeshan On

You can do this by using jQuery i.e:

$("#scrollList").load(function(){

    while(1){
        $(this).slideUp(1000);
        $(this).slideDown(1000);
    }

});

Hope this helps.