[英]How to disable scroll left?
I got a div element (" #parent
") that includes multiple child elements (" .item
"). 我有一个div元素 (“
#parent
包括多个子元素(‘’) .item
”)。 I want to enable scrolling the parent element just in one direction (left OR right). 我想启用只在一个方向 (左或右)滚动父元素。 Otherwise nothing should happen.
否则什么都不会发生。
See my code: 看我的代码:
$("#parent").scroll(function() { // >>> scroll event // >>> console.log("SCROLLED " + new Date().getMilliseconds()) })
#container { position: absolute; width: 100%; height: 100%; } #parent { position: relative; width: 90%; height: 40%; overflow-y: hidden; overflow-x: scroll; white-space: nowrap; background: red; } .child { display: inline-block; position: relative; margin-left: 3%; width: 40px; height: 40px; background: gray; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <div id="parent"> <div class="child">1</div> <div class="child">2</div> <div class="child">3</div> <div class="child">4</div> <div class="child">5</div> <div class="child">6</div> <div class="child">7</div> <div class="child">8</div> <div class="child">9</div> <div class="child">10</div> <div class="child">12</div> <div class="child">13</div> <div class="child">14</div> <div class="child">15</div> <div class="child">16</div> <div class="child">17</div> <div class="child">18</div> <div class="child">19</div> <div class="child">20</div> </div> </div>
So to my question: I'd like to disable scrolling the element to the right hand side (backwards). 所以我的问题是:我想禁用将元素滚动到右侧(向后)。 I'd like to just enable the scroll of the items to the left hand side (forward).
我想将项目的滚动设置到左侧(向前)。
How can I use jQuery to implement this method? 如何使用jQuery实现此方法? Any help would be very appreciated.
任何帮助将非常感激。 Thanks in advance!
提前致谢!
The following code should do the job 1 . 以下代码应该完成工作1 。
previousX
stores the last position that the element was scrolled to on the X axis. previousX
存储元素在X轴上滚动到的最后位置。
When the scroll event triggers, newX
is set to the scrollLeft()
value (this returns how far the element has been scrolled from it's left-most side, in pixels). 当scroll事件触发时,
newX
被设置为scrollLeft()
值(这将返回元素从其最左侧滚动的距离,以像素为单位)。
If this value is greater than previousX
, then they have scrolled to the right, so we allow the scroll, and update previousX
to the new x value. 如果此值大于
previousX
,则它们向右滚动,因此我们允许滚动,并将previousX
更新为新的x值。
If the value is lesser than previousX
, they have scrolled to the right - so we need to cancel the scroll. 如果该值大于较小
previousX
,他们已经滚动到正确的-所以我们需要取消滚动。 To do this, we can use the scrollLeft()
function again - but this time, we provide a value to it - this allows us to set the scroll position; 为此,我们可以再次使用
scrollLeft()
函数 - 但这一次,我们为它提供了一个值 - 这允许我们设置滚动位置; rather than retrieve it. 而不是检索它。 By setting it to
previousX
, we can prevent the scroll. 通过将其设置为
previousX
,我们可以阻止滚动。
Note that if the value is equal , we do nothing. 请注意,如果值相等 ,我们什么都不做。
let previousX = -1; $("#parent").scroll(function(e){ let newX = $("#parent").scrollLeft(); if (newX>previousX) { previousX = newX; } else if (newX<previousX) { $("#parent").scrollLeft(previousX); } })
#container { position: absolute; width: 100%; height: 100%; overflow:hidden; } #parent { position: relative; width: 90%; height: 40%; overflow-y: hidden; overflow-x: scroll; white-space: nowrap; background: red; } .child { display: inline-block; position: relative; margin-left: 3%; width: 40px; height: 40px; background: gray; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <div id="parent"> <div class="child">1</div> <div class="child">2</div> <div class="child">3</div> <div class="child">4</div> <div class="child">5</div> <div class="child">6</div> <div class="child">7</div> <div class="child">8</div> <div class="child">9</div> <div class="child">10</div> <div class="child">12</div> <div class="child">13</div> <div class="child">14</div> <div class="child">15</div> <div class="child">16</div> <div class="child">17</div> <div class="child">18</div> <div class="child">19</div> <div class="child">20</div> </div> </div>
1 : Unfortunately, it doesn't seem to work at all when I use it with my Magic Trackpad - but when dragging the scroll bars it works just fine. 1 : 不幸的是,当我使用我的Magic Trackpad时,它似乎根本不起作用 - 但是当拖动滚动条时它工作得很好。 I haven't tested it on a touch-screen device / Windows / using a proper mouse & scroll wheel, so I don't know how it behaves in those cases either.
我没有在触摸屏设备/ Windows /使用适当的鼠标和滚轮测试它,所以我不知道它在这些情况下的行为。 It would definitely be worth you doing some proper testing / improving this code, as it probably will not work in all cases, or even catch all possible scroll events.
绝对值得你做一些正确的测试/改进这个代码,因为它可能不适用于所有情况,甚至捕获所有可能的滚动事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.