jScrollPane - 绑定到事件会添加到所有实例

发布于 2024-12-26 07:35:26 字数 1353 浏览 0 评论 0原文

http://jscrollpane.kelvinluck.com/events.html

在此示例中,jscrollpane 使用类选择器将功能绑定到事件。我似乎无法只绑定到一个对象:

 var pane = $('#scrollable');
pane.bind(
    'jsp-scroll-y',
    function (event, scrollPositionY, isAtTop, isAtBottom) {
        if (isAtBottom) {
            $('#navigation').addClass('hiddenAtBottom');
        } else {
            $('#navigation').removeClass('hiddenAtBottom');
        }
        if (isAtTop) {
            $('#lioverview a').addClass('focus');
        } else {
            $('#lioverview a').removeClass('focus');
        }
        $('.content').each(function (i) {
            var ob = $(this);
            var offSet = ob.offset();
            var newOffset = (offSet.top - 150);
            if (newOffset <= 0) {
                $('#navigation a').removeClass('focus');
                $('#navigation li:eq(' + i + ') a').addClass('focus');
            }

        });
    }
);
pane.jScrollPane({
    animateScroll: true,
    animateDuration: 2500,
    animateEase: '',
    hijackInternalLinks: true
});

按预期工作。如果添加另一个我不希望上面绑定的可滚动区域,我会认为我创建了另一个实例(和一个单独的对象),

就像它一样简单

$('.accScrollable').jScrollPane();

,滚动条出现,但每次我滚动到这些元素的底部,另一个绑定函数被触发!

有人知道这里发生了什么事吗?

问候

http://jscrollpane.kelvinluck.com/events.html

In this example jscrollpane uses a class selector to bind functionality to the events. I cannot seem to bind to only one object:

 var pane = $('#scrollable');
pane.bind(
    'jsp-scroll-y',
    function (event, scrollPositionY, isAtTop, isAtBottom) {
        if (isAtBottom) {
            $('#navigation').addClass('hiddenAtBottom');
        } else {
            $('#navigation').removeClass('hiddenAtBottom');
        }
        if (isAtTop) {
            $('#lioverview a').addClass('focus');
        } else {
            $('#lioverview a').removeClass('focus');
        }
        $('.content').each(function (i) {
            var ob = $(this);
            var offSet = ob.offset();
            var newOffset = (offSet.top - 150);
            if (newOffset <= 0) {
                $('#navigation a').removeClass('focus');
                $('#navigation li:eq(' + i + ') a').addClass('focus');
            }

        });
    }
);
pane.jScrollPane({
    animateScroll: true,
    animateDuration: 2500,
    animateEase: '',
    hijackInternalLinks: true
});

works as intended. If a add another scrollable area which I dont want the above to be bound to, I would think that I create another instance (and a seperate object)

something simple like

$('.accScrollable').jScrollPane();

Which, the scrollbars appear but every time I scroll to the bottom of these elements, the other bound function fires!

anyone know whats going on here?

regards

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

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

发布评论

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

评论(2

好听的两个字的网名 2025-01-02 07:35:26

听起来你所描述的是一个错误,所以我只是整理了一个简单的例子来测试。你可以在这里看到它:

http://jsfiddle.net/qSf2q/1/

这是 javascript代码:

$('.scroll-pane').jScrollPane();


$('#pane1').bind(
    'jsp-scroll-y',
    function(event, scrollPositionY, isAtTop, isAtBottom)
    {
        console.log('#pane1 Handle jsp-scroll-y', this,
                        'scrollPositionY=', scrollPositionY,
                        'isAtTop=', isAtTop,
                        'isAtBottom=', isAtBottom);
    }
);
$('#pane2').bind(
    'jsp-scroll-y',
    function(event, scrollPositionY, isAtTop, isAtBottom)
    {
        console.log('#pane2 Handle jsp-scroll-y', this,
                        'scrollPositionY=', scrollPositionY,
                        'isAtTop=', isAtTop,
                        'isAtBottom=', isAtBottom);
    }
);

如果您滚动#pane1,您将收到包含#pane1 的控制台消息,如果您滚动#pane2,您将收到以#pane2 开头的消息。

所以看起来这个错误是在你的代码中,而不是在 jScrollPane 中。由于您没有包含完整的代码或指向它的链接,我们无法真正提供帮助,但希望这个示例向您展示如何将侦听器绑定到不同的 DOM 元素...

It sounded like what you were describing was a bug so I just put together a simple example to test. You can see it here:

http://jsfiddle.net/qSf2q/1/

Here is the javascript code:

$('.scroll-pane').jScrollPane();


$('#pane1').bind(
    'jsp-scroll-y',
    function(event, scrollPositionY, isAtTop, isAtBottom)
    {
        console.log('#pane1 Handle jsp-scroll-y', this,
                        'scrollPositionY=', scrollPositionY,
                        'isAtTop=', isAtTop,
                        'isAtBottom=', isAtBottom);
    }
);
$('#pane2').bind(
    'jsp-scroll-y',
    function(event, scrollPositionY, isAtTop, isAtBottom)
    {
        console.log('#pane2 Handle jsp-scroll-y', this,
                        'scrollPositionY=', scrollPositionY,
                        'isAtTop=', isAtTop,
                        'isAtBottom=', isAtBottom);
    }
);

If you scroll #pane1 you will get the console message with #pane1 in it and if you scroll #pane2 you will get the message which starts with #pane2.

So it seems that the bug is with your code and not with jScrollPane. Since you don't include the whole code or a link to it we can't really help but hopefully this example shows you how you can bind listeners to different DOM elements...

冧九 2025-01-02 07:35:26

jsp-scroll-y 的事件处理程序会影响与绑定元素 this 没有牢固关系的元素,在本例中是 #scrollable

您的 HTML 是否包含多个 #navigation#lioverview.content

Your event handler for jsp-scroll-y affects elements without a solid relation to the bound element this, in this case #scrollable.

Does you HTML contain multiple #navigation, #lioverview or .content?

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