jQuery SimplyScroll 插件在某些缩放级别上中断

发布于 2024-12-07 15:08:16 字数 628 浏览 0 评论 0原文

您实际上可以在 SimplyScroll 演示页面上看到此效果:使用 Safari、Chrome 或 Firefox,然后缩小以至于页面元素非常小,并且滚动条停止移动。

我已经进行了测试和实验,看来scrollLeft 值在setInterval 循环内递增,但它不会“粘住”,而只是停留在前一个滚动值。更奇怪的是,从控制台手动更改滚动值效果很好。

这是相关的片段:

self.interval = setInterval(function() {
    // snip

    // increment by 1 pixel
    self.$clip[0].scrollLeft += self.o.speed;

    // scrollLeft value is unchanged

    // snip

},self.intervalDelay); // usually set to 41

我尝试用 requestAnimationFrame 替换 setInterval,并且还大幅降低速度,但没有结果;我很困惑。关于为什么缩放级别会影响计时器回调内滚动的能力有什么想法吗?

You can actually see this effect on the SimplyScroll demo page: use Safari, Chrome or Firefox, and zoom out to the point that page elements are very small, and the scroller stops moving.

I've tested and experimented, and it appears that the scrollLeft value is being incremented within a setInterval loop, but it doesn't "stick", and just stays at the previous scroll value instead. Even more strangely, altering the scroll value manually from the console works just fine.

Here is the relevant snippet:

self.interval = setInterval(function() {
    // snip

    // increment by 1 pixel
    self.$clip[0].scrollLeft += self.o.speed;

    // scrollLeft value is unchanged

    // snip

},self.intervalDelay); // usually set to 41

I've tried replacing setInterval with requestAnimationFrame, and also drastically turning down the speed, and no results; I'm stumped. Any ideas on why zoom level is affecting the ability to scroll within a timer callback?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2024-12-14 15:08:16

我想通了:看起来它与缩小时构成“像素”的内容有关;单个像素的变化计算为小于一个像素,这不会导致任何变化。我计划向开发人员提交错误报告;目前,我已经实施了以下hack解决方法:

self.interval = setInterval(function() {

    // snip

    var elem = self.$clip[0];
    var scrollLeft = elem.scrollLeft

    // increment by 1 pixel
    elem.scrollLeft += self.o.speed;

    // Zoom out hack: raise pixel values until a change actually takes place
    if(elem.scrollLeft == scrollLeft) {
        elem.scrollLeft += (self.o.speed + 1);
        if(elem.scrollLeft == scrollLeft)
            elem.scrollLeft += (self.o.speed + 2);
    }

    // snip

},self.intervalDelay);

I figured it out: it looks like it has do with what constitutes a "pixel" when zoomed out; a single-pixel change evaluates to being less than one pixel, which leads to no change. I plan to file a bug report with the developer; for now, I've implemented the following hack workaround:

self.interval = setInterval(function() {

    // snip

    var elem = self.$clip[0];
    var scrollLeft = elem.scrollLeft

    // increment by 1 pixel
    elem.scrollLeft += self.o.speed;

    // Zoom out hack: raise pixel values until a change actually takes place
    if(elem.scrollLeft == scrollLeft) {
        elem.scrollLeft += (self.o.speed + 1);
        if(elem.scrollLeft == scrollLeft)
            elem.scrollLeft += (self.o.speed + 2);
    }

    // snip

},self.intervalDelay);
最佳男配角 2024-12-14 15:08:16

第 1 步:在下面添加函数

fixScrollNotRunWhenBrowserZoomout: function(){
    var self = this;
    var speed = self.o.speed;
    var sp = self.$clip[0].scrollLeft;
    var intervalHandle = setInterval(function(){
        self.$clip[0].scrollLeft += speed;
        if (Math.floor(self.$clip[0].scrollLeft) !== sp || speed == 100){
            clearInterval(intervalHandle);
            self.o.speed = speed;
        }
        speed++;    
    }, 100);
}

第 2 步:在 init 函数上调用它

init: function() {

    this.$items = this.$list.children();
    this.$clip = this.$list.parent(); //this is the element that scrolls
    this.$container = this.$clip.parent();
    this.$btnBack = $('.simply-scroll-back',this.$container);
    this.$btnForward = $('.simply-scroll-forward',this.$container);

    this.fixScrollNotRunWhenBrowserZoomout();   

step 1: Add function below

fixScrollNotRunWhenBrowserZoomout: function(){
    var self = this;
    var speed = self.o.speed;
    var sp = self.$clip[0].scrollLeft;
    var intervalHandle = setInterval(function(){
        self.$clip[0].scrollLeft += speed;
        if (Math.floor(self.$clip[0].scrollLeft) !== sp || speed == 100){
            clearInterval(intervalHandle);
            self.o.speed = speed;
        }
        speed++;    
    }, 100);
}

Step 2: call it on init function

init: function() {

    this.$items = this.$list.children();
    this.$clip = this.$list.parent(); //this is the element that scrolls
    this.$container = this.$clip.parent();
    this.$btnBack = $('.simply-scroll-back',this.$container);
    this.$btnForward = $('.simply-scroll-forward',this.$container);

    this.fixScrollNotRunWhenBrowserZoomout();   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文