Monday, August 13, 2012

Div Scrolling Left and Right Through JQuery




In this example I am creating 2 buttons dynamically on page load through JQuery & We can scroll left & right on click on these 2 buttons

 $(document).ready(function () {

     // Creating two Left Right Scroll Button through JQuery
        var  btnRht= $("<input type='button' id='btnLeft' value='>>' class='ButtonStyle' width='5px' />");
     var  btnLeft = $("<input type='button' id='btnRight' value='<<' class='ButtonStyle' />");
           // appending buttons to a td inside a html div 
        $('#tdDoE').append(btnLeft);
    $('#tdDoE').append(btnRht);
    // Left / Rht Scroll scroll     
                $("#btnLeft").click(function () { 
                var leftPos = $('.DivDataMain').scrollLeft();
                $(".DivDataMain").animate({scrollLeft: leftPos + 250}, 600);
                });   
                    $("#btnRight").click(function () { 

                var leftPos2 = $('.DivDataMain').scrollLeft();
                $(".DivDataMain").animate({scrollLeft: leftPos2 - 250}, 600);
                });   

    });


HTML Code :-
       <div style="width: 300px; overflow: hidden;" class="DivDataMain">
        <table>
            <tr>
                <td id="tdDoE">
                        Scroll Buttons will be appended after this text.
                </td>
            </tr>
        </table>
    </div>

No comments:

Post a Comment