admin管理员组文章数量:1194549
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");
});
I have tried the solutions that I have found online. But my container keeps shifting even though I am trying to fix it.
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.
Share Improve this question asked Jul 30, 2014 at 2:54 k91221221k91221221 731 gold badge1 silver badge3 bronze badges3 Answers
Reset to default 17$('#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
You are looking for scrollLeft
not marginLeft
:
$('#right-button').click(function() {
event.preventDefault();
$('#content').animate({
scrollLeft: "+=200px"
}, "slow");
});
$('#left-button').click(function() {
event.preventDefault();
$('#content').animate({
scrollLeft: "-=200px"
}, "slow");
});
Demo: http://plnkr.co/edit/ZdCw7IEYdV5YVeGg33oX?p=preview
The answer of @Vennik is truely awesome,
But in my case i used it bit differently, As i used Material Design and was making API call to display Image Carousal ,
I did in this way ,
First part is Carousel or Slider code and Second part is JS code
<!-- Carousel -->
<span style="cursor:pointer" id="left-button"><i class="material-icons">keyboard_arrow_left</i></span> 
<span style="cursor:pointer" id="right-button"> <i class="material-icons">keyboard_arrow_right</i></span>
<div class="bill-screens mdl-shadow--4dp" id="offer-pg-cont">
<?php for($t=0; $t< count($arr_get_a_user['categories']);$t++){?>
<div class="bill-pic bill-screen">
<button class="bill_class" id="bill_<?php echo $t ?>" onClick="display_image()">
<img class="bill-screen-image" src="<?php echo $btn_img1; ?>">
</button>
</div>
<?php }?>
</div>
. . . . . . . . . .
<script data-require="jquery" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
// Carausol JS
$('#right-button').click(function() {
event.preventDefault();
$('#offer-pg-cont').animate({
scrollLeft: "+=200px"
}, "slow");
});
$('#left-button').click(function() {
event.preventDefault();
$('#offer-pg-cont').animate({
scrollLeft: "-=200px"
}, "slow");
});
</script>
本文标签: javascriptJquery horizontal Scroll using buttonsStack Overflow
版权声明:本文标题:javascript - Jquery horizontal Scroll using buttons - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738450108a2087437.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论