简体   繁体   English

限制HTML5画布元素上的滚动功能

[英]Limit scroll ability on HTML5 canvas elements

I've got a series of rectangles drawn on a canvas and use a scroll event listener to move the boxes up and down. 我在画布上绘制了一系列矩形,并使用滚动事件监听器来上下移动框。

I'm trying to add in some validation so that the boxes cannot be scrolled past a certain point. 我正在尝试添加一些验证,以便框不能滚动超过某一点。

Due to the acceleration, the scroll values don't always increment by 1, so when scrolling quickly, sometimes my validation kicks in too early. 由于加速,滚动值并不总是递增1,因此当快速滚动时,有时我的验证会过早开始。

Any ideas how to solve this? 任何想法如何解决这个问题?

So in my event listener I have: 所以在我的事件监听器中,我有:

lScroll += e.deltaY;

if (lScroll > 0) {
    canScroll = false;
    lScroll = 0;
} else {
    canScroll = true;
}

https://jsfiddle.net/kr85k3us/3/ https://jsfiddle.net/kr85k3us/3/

Please check if this is working for you: https://jsfiddle.net/kr85k3us/4/ 请检查这是否适合您: https//jsfiddle.net/kr85k3us/4/

I tested it and it should work, but perhaps you can move your mousewheel faster :) 我测试了它应该可以工作,但也许你可以更快地移动你的鼠标轮:)

if (!canScroll && lScroll == 0) {
    var first = Object.keys(boxes)[0];

    if (boxes[first]['y'] < 10) {
        var delta = 10 - boxes[first]['y'];
        Object.keys(boxes).forEach(function(k){ boxes[k]['y'] += delta; });
    }
}

This is the piece of code I added. 这是我添加的一段代码。 If you can't scroll and lScroll is 0, it means that we reached the top. 如果您无法滚动并且lScroll为0,则表示我们已达到顶部。 Next, I check if the first box is where it should be. 接下来,我检查第一个框是否应该在哪里。 If not (ie boxes[first]['y'] < 10 ) then it adjusts all the boxes' y position. 如果不是(即boxes[first]['y'] < 10 )则调整所有方框的y位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM