Javascript 使用按钮的Jquery水平滚动
我正在尝试使用按钮获得水平滚动 我有一个容器,其中有几个div水平堆叠,我想使用给定的按钮滚动它们 请看一下我的计划,告诉我我做错了什么 HTML: 我已经尝试了我在网上找到的解决方案。但我的容器一直在移动,即使我正在试图修复它Javascript 使用按钮的Jquery水平滚动,javascript,jquery,html,Javascript,Jquery,Html,我正在尝试使用按钮获得水平滚动 我有一个容器,其中有几个div水平堆叠,我想使用给定的按钮滚动它们 请看一下我的计划,告诉我我做错了什么 HTML: 我已经尝试了我在网上找到的解决方案。但我的容器一直在移动,即使我正在试图修复它 $('#right-button').click(function() { event.preventDefault(); $('#content').animate({ scrollLeft: "+=200px" }, "slow"); });
$('#right-button').click(function() {
event.preventDefault();
$('#content').animate({
scrollLeft: "+=200px"
}, "slow");
});
$('#left-button').click(function() {
event.preventDefault();
$('#content').animate({
scrollLeft: "-=200px"
}, "slow");
});
编辑,解释。。。您需要设置其向左滚动的位置
您要查找的是
滚动左
而不是marginLeft
:
$('#right-button').click(function() {
event.preventDefault();
$('#content').animate({
scrollLeft: "+=200px"
}, "slow");
});
$('#left-button').click(function() {
event.preventDefault();
$('#content').animate({
scrollLeft: "-=200px"
}, "slow");
});
演示:@Vennik的答案真是太棒了,
但在我的例子中,我使用的方式有点不同,因为我使用了材质设计,并调用API来显示图像Carousal,
我是这样做的,
第一部分是旋转木马或滑块代码,第二部分是JS代码
<!-- 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>
键盘箭头左键
键盘箭头右键
当没有可滚动的内容时,如何禁用按钮?@Venkat这个答案已经有5年多的历史了。然而,由于我仍然经常使用StackOverflow,我注意到了这个问题。您只需检查#content
框的宽度,如果大于.internal
宽度,请禁用按钮。检查此处:-尝试删除.internal
元素,直到只剩下3个,重新运行时应该禁用按钮。
$('#right-button').click(function() {
event.preventDefault();
$('#content').animate({
scrollLeft: "+=200px"
}, "slow");
});
$('#left-button').click(function() {
event.preventDefault();
$('#content').animate({
scrollLeft: "-=200px"
}, "slow");
});
<!-- 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>