Skip to main content

Scrolling a Div in JavaScript

Here is a example to scroll div through javascript by clicking a button or linkbutton bellow is 5 linkbutton for scrolling different-different way to a div content Scroll Top,Bottom,Absolute,Up and Down.
Just copy and paste java script code to the page and set function on click of your control for doing some action and pass the id of div to the function.

See Example Here
Id EmpName
1 vivek gupta
2 ranu gupta
3 vijay
4 Ramkesh
5 Pushkar
6 Dinash
7 Anand
8 Yogesh
9 Lohitha
10 Ritu
11 Ashish


JavaScript Code                                            
<script type="text/javascript">

    function toTop(id) {
        document.getElementById(id).scrollTop = 0
    }

    defaultStep = 1
    step = defaultStep
    function scrollDivDown(id) {
        document.getElementById(id).scrollTop += step
        timerDown = setTimeout("scrollDivDown('" + id + "')", 10)
    }

    function scrollDivUp(id) {
        document.getElementById(id).scrollTop -= step
        timerUp = setTimeout("scrollDivUp('" + id + "')", 10)
    }

    function toBottom(id) {
        document.getElementById(id).scrollTop = document.getElementById(id).scrollHeight
    }

    function toPoint(id) {
        document.getElementById(id).scrollTop = 100
    }
   
</script>



Html Code
<div style="width:400px">
        <input type="button" style="cursor: pointer; color: Blue" onclick="toTop('div1')"
            value="Top" />
        <input type="button" value="ScrollDown" style="cursor: pointer; color: Blue" onmousedown="scrollDivDown('div1')"
            onmouseup="clearTimeout(timerDown)" />
        <input type="button" style="cursor: pointer; color: Blue" onmousedown="scrollDivUp('div1')"
            value="Scroll Up" onmouseup="clearTimeout(timerUp)" />
        <input type="button" style="cursor: pointer; color: Blue" value="Bottom" onclick="toBottom('div1')" />
        <input type="button" style="cursor: pointer; color: Blue" value="Point" onclick="toPoint('div1')" />
    </div>

    <div id="div1" style="overflow: scroll; height: 200px; width:400px;
        border-style: double">
        <table align="center" border="1" cellpadding="10px" cellspacing="10px">
            <tr>
                <th>
                    Id
                </th>
                <th>
                    EmpName
                </th>
            </tr>
            <tr>
                <td>
                    1
                </td>
                <td>
                    vivek gupta
                </td>
            </tr>
            <tr>
                <td>
                    2
                </td>
                <td>
                    ranu gupta
                </td>
            </tr>
            <tr>
                <td>
                    3
                </td>
                <td>
                    vijay
                </td>
            </tr>
            <tr>
                <td>
                    4
                </td>
                <td>
                    Ramkesh
                </td>
            </tr>
            <tr>
                <td>
                    5
                </td>
                <td>
                    Pushkar
                </td>
            </tr>
            <tr>
                <td>
                    6
                </td>
                <td>
                    Dinash
                </td>
            </tr>
            <tr>
                <td>
                    7
                </td>
                <td>
                    Anand
                </td>
            </tr>
            <tr>
                <td>
                    8
                </td>
                <td>
                    Yogesh
                </td>
            </tr>
            <tr>
                <td>
                    9
                </td>
                <td>
                    Lohitha
                </td>
            </tr>
            <tr>
                <td>
                    10
                </td>
                <td>
                    Ritu
                </td>
            </tr>
            <tr>
                <td>
                    11
                </td>
                <td>
                    Ashish
                </td>
            </tr>
        </table>
    </div>

Popular posts from this blog

Merging multiple PDFs using iTextSharp in asp.net c#

In this article i will show you how to merge multiple pdfs into one using ITextSharp below is the two approach one is to pass your input files path, output file path (will be created if not exist) and another is pass direct input stream, output stream and it will write the merge files into output stream.

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -

Uploading large file in chunks in Asp.net Mvc c# from Javascript ajax

Often we have a requirement to upload files in Asp.net, Mvc c# application but when it comes to uploading larger file, we always think how to do it as uploading large file in one go have many challenges like UI responsiveness, If network fluctuate for a moment in between then uploading task get breaks and user have to upload it again etc.

Scrollable Gridview with fixed headers in asp.net

horizontal and vertical scrollable Gridview with static header row.fixed header in gridview in asp.net.static header in gridview through java script.I was looking for a solution for this for a long time and found most of the answers are not working or not suitable for my situation i also find most of the java script code for that they worked but only with the vertical scroll not with the horizontal scroll and also combination of header and rows doesn't match.