Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
113
rated 0 times [  118] [ 5]  / answers: 1 / hits: 33689  / 10 Years ago, wed, july 30, 2014, 12:00:00

I am trying to obtain horizontal scroll using buttons.



I have a container which has several divs stacked horizontally and I want to scroll through them using the buttons given.



Please take a look at my attemp and tell me what I am doing wrong.



HTML:



   <div class=left>
left div
<button id=left-button>
swipe left
</button>
</div>
<div class=center id=content>
<div class=internal>
div 1
</div>
<div class=internal>
div 2
</div>
<div class=internal>
div 3
</div>
<div class=internal>
div 4
</div>
<div class=internal>
div 5
</div>
<div class=internal>
div 6
</div>
<div class=internal>
div 7
</div>
<div class=internal>
div 8
</div>
</div>
<div class=right>
<button id=right-button>
swipe right
</button>
right div
</div>


JQUERY:



  $('#right-button').click(function() {
event.preventDefault();
$('#content').animate({
marginLeft: +=200px
}, slow);
});

$('#left-button').click(function() {
event.preventDefault();
$('#content').animate({
marginLeft: -=200px
}, slow);
});


http://plnkr.co/edit/GxufhJaRJn2SfGb4ilIl?p=preview



I have tried the solutions that I have found online. But my container keeps shifting even though I am trying to fix it.


More From » jquery

 Answers
133
$('#right-button').click(function() {
event.preventDefault();
$('#content').animate({
scrollLeft: +=200px
}, slow);
});

$('#left-button').click(function() {
event.preventDefault();
$('#content').animate({
scrollLeft: -=200px
}, slow);
});


Edit, to explain... you need to set its scroll left position.



DEMO PLUNKR


[#70000] Saturday, July 26, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
raynamadilynl

Total Points: 653
Total Questions: 110
Total Answers: 98

Location: Honduras
Member since Sat, Jul 24, 2021
3 Years ago
raynamadilynl questions
;