Eyad Fallatah
Eyad Fallatah

Reputation: 1948

jQuery scrollRight?

I have the following code that seems to scroll a div on click all the way to the left. I am wondering if:

  1. there is a way to get it to get it to scroll only 200px at a time.
  2. I can get it to scroll to the right as well. Tried to look at the jQuery documentations but could not find a scrollToRight function.

Here is my code:

$(".leftArrow").click(function () { 
  $(".innerWrapper").animate({scrollLeft: 250}, 800);
});
.innerWrapper
{
	float: left;
	margin-right:-32767px;
}
.outerWrapper
{
	width: 780px;
	height: 180px;
	overflow: hidden;
}
<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>

Any help would be greatly appreciated.

Upvotes: 47

Views: 118595

Answers (4)

Manigandan Arjunan
Manigandan Arjunan

Reputation: 2265

use the below like script..

 <script type="text/javascript">
        $(window).load(function() {
            $("div#makeMeScrollable").smoothDivScroll({ 
                autoScroll: "onstart" , 
                autoScrollDirection: "backandforth", 
                autoScrollStep: 1, 
                autoScrollInterval: 15, 
                startAtElementId: "startAtMe", 
                visibleHotSpots: "always"
            });
        });
    </script>


<div id="makeMeScrollable">
    <div class="scrollingHotSpotLeft"></div>
    <div class="scrollingHotSpotRight"></div>
    <div class="scrollWrapper">
        <div class="scrollableArea">
            <img src="images/demo/field.jpg" alt="Demo image" />
            <img src="images/demo/gnome.jpg" alt="Demo image" />
            <img src="images/demo/pencils.jpg" alt="Demo image" />
            <img src="images/demo/golf.jpg" alt="Demo image" id="startAtMe" />
            <img src="images/demo/river.jpg" alt="Demo image" />
            <img src="images/demo/train.jpg" alt="Demo image" />
            <img src="images/demo/leaf.jpg" alt="Demo image" />
            <img src="images/demo/dog.jpg" alt="Demo image" />
        </div>
    </div>
</div

Upvotes: 0

animalgraphics
animalgraphics

Reputation: 51

$('.leftArrow').click(function(event){
    event.preventDefault();
    $('.innerWrapper').animate({scrollLeft:'+=1500'},500);
});

Simple as that.

Upvotes: 5

i31nGo
i31nGo

Reputation: 1502

I found this solution without animation:

$("#rightArrow").click(function () { 
   $('#outerWrapper').scrollLeft($('#outerWrapper').scrollLeft() + 20);
});

hope it helps.

Upvotes: 1

mrtsherman
mrtsherman

Reputation: 39872

scrollLeft IS scrollRight. Sort of. All it does is set the amount of horizontal scroll. If you set it to zero then it will be all the way left. If you set it to something greater than zero then it will move to the right!

As far as making it go in increments, you would have to get the current scrollLeft distance and then subtract 200.

$(".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);
});

Upvotes: 121

Related Questions