locked
How to place div scroll bar to right most side on page load RRS feed

  • Question

  • User-454825017 posted

    i have search working code to know how can i programmatically take or drag div scroll bar to right side.

    i tried this sample but could not simulate.

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <input type='button' class='leftArrow' value='left'>
    <input type='button' class='rightArrow' value='right'>
    
    <div class='outerWrapper'>
       <div class='innerWrapper'>
             Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
       </div>
    </div>
    $(".leftArrow").click(function () { 
      var leftPos = $('.innerWrapper').scrollLeft();
      $(".innerWrapper").animate({scrollLeft: leftPos - 200}, 800);
    });
    
    $(".rightArrow").click(function () { 
      var leftPos = $('.innerWrapper').scrollLeft();
      $(".innerWrapper").animate({scrollLeft: leftPos + 200}, 800);
    });

    the code i have posted here 

     $(".innerWrapper").animate({scrollLeft: leftPos + 200}, 800);

    where leftpos added with 200 hard code value which i do not want to do rather i want to see different approach where it will be extracted programmatically how much we need to move to right side rather hard code value.

    please help me to do it. i need to drag div's scrollbar to right side by jquery. thanks

    Tuesday, July 7, 2020 3:55 PM

Answers

  • User-454825017 posted

    I got the solution. here is the code

    <style>
        .outerWrapper {
            width: 30em;
            overflow-x: auto;
            white-space: nowrap;
        }
    </style>
    <input type='button' class='leftArrow' value='left'>
    <input type='button' class='rightArrow' value='right'>
    
    <div class='outerWrapper'>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </div>
    
    @section scripts {
    <script type="text/javascript" >
        $(document).ready(function () {
    
            $(".rightArrow").click(function () {
                var leftPos = $('.outerWrapper').scrollLeft();
                var divwidth = $('.outerWrapper')[0].scrollWidth;
                $(".outerWrapper").animate({ scrollLeft: leftPos + divwidth }, 800);
            });
        });
    
    </script>
    }

    thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 7, 2020 5:59 PM

All replies

  • User-474980206 posted

    then use a variable:

    $(".leftArrow").click(function () { 
      var leftPos = $('.innerWrapper').scrollLeft();
      var amountToScroll = 100;
      $(".innerWrapper").animate({scrollLeft: leftPos - amountToScroll}, 800);
    });

    to go all the way left its just

    $(".innerWrapper").animate({scrollLeft: 0}, 800);

    Tuesday, July 7, 2020 5:26 PM
  • User-454825017 posted

    to go to right most side in scroll bar say like user drag the scroll bar to right end....how to do it by jquery?

    if i hard code any value like 100 or 200 that may work or may not. so i am looking for best idea which will work in any pc resolution and in any browser.

    I tried this but still no luck

    <style>
        .outerWrapper {
            width: 30em;
            overflow-x: auto;
            white-space: nowrap;
        }
    </style>
    <input type='button' class='leftArrow' value='left'>
    <input type='button' class='rightArrow' value='right'>
    
    <div class='outerWrapper'>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </div>
    
    @section scripts {
    <script type="text/javascript" >
        $(document).ready(function () {
    
            $(".rightArrow").click(function () {
                //alert('www ' + $('.outerWrapper').outerWidth());
                var leftPos = $('.outerWrapper').scrollLeft();
                var divwidth = $('.outerWrapper').outerWidth();
                $(".outerWrapper").animate({ scrollLeft: leftPos + divwidth }, 800);
            });
        });
    </script>
    }

    so please share something best. thanks

    Tuesday, July 7, 2020 5:33 PM
  • User-454825017 posted

    I got the solution. here is the code

    <style>
        .outerWrapper {
            width: 30em;
            overflow-x: auto;
            white-space: nowrap;
        }
    </style>
    <input type='button' class='leftArrow' value='left'>
    <input type='button' class='rightArrow' value='right'>
    
    <div class='outerWrapper'>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </div>
    
    @section scripts {
    <script type="text/javascript" >
        $(document).ready(function () {
    
            $(".rightArrow").click(function () {
                var leftPos = $('.outerWrapper').scrollLeft();
                var divwidth = $('.outerWrapper')[0].scrollWidth;
                $(".outerWrapper").animate({ scrollLeft: leftPos + divwidth }, 800);
            });
        });
    
    </script>
    }

    thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 7, 2020 5:59 PM