jQuery 滚动对吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8905832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery scrollRight?
提问by Э?ad Дьdulя?мaи
I have the following code that seems to scroll a div on click all the way to the left. I am wondering if:
我有以下代码似乎在一直向左单击时滚动 div。我想知道是否:
- there is a way to get it to get it to scroll only 200px at a time.
- 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.
- 有一种方法可以让它一次只滚动 200 像素。
- 我也可以让它向右滚动。试图查看 jQuery 文档,但找不到 scrollToRight 函数。
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.
任何帮助将不胜感激。
回答by mrtsherman
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!
scrollLeft 是 scrollRight。有点。它所做的只是设置水平滚动量。如果您将其设置为零,那么它将一直向左。如果您将其设置为大于零的值,那么它将向右移动!
As far as making it go in increments, you would have to get the current scrollLeft distance and then subtract 200.
至于让它以增量方式进行,您必须获得当前的 scrollLeft 距离,然后减去 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);
});
回答by animalgraphics
$('.leftArrow').click(function(event){
event.preventDefault();
$('.innerWrapper').animate({scrollLeft:'+=1500'},500);
});
Simple as that.
就那么简单。
回答by Manigandan Arjunan
use the below like script.. DEMO
使用下面的脚本..演示
<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
回答by i31nGo
I found this solution without animation:
我找到了没有动画的解决方案:
$("#rightArrow").click(function () {
$('#outerWrapper').scrollLeft($('#outerWrapper').scrollLeft() + 20);
});
hope it helps.
希望能帮助到你。