如何在不丢失第一次初始化中添加的功能的情况下重新初始化jScrollPane

How to reinitialise jScrollPane without losing features added in the first initialisation?

本文关键字:初始化 功能 情况下 jScrollPane 添加 第一次      更新时间:2023-09-26

我在使用jScrollPane的reinitialise()方法时遇到问题。每当我调用它时,我在第一次初始化中实现的东西就会停止工作。。。

在我当前的代码中,我正在做这样的事情:

$('.scrollable').each(function(){
    var e = $(this),
        parent = e.parent();
    e.jScrollPane({
        // parameters
    });
    var api = e.data('jsp'),
        arrowup = e.find('.jspArrowUp'),
        arrowdw = e.find('.jspArrowDown');
    if ( api.getIsScrollableV() ) {
        e.addClass('scrolled-top');
        parent.addClass('scroll-parent');
    }
    e.scroll(function(){
        var scrbef = api.getContentPositionY(),
            scrmax = api.getContentHeight() - this.clientHeight,
            scraft = scrmax - scrbef,
            dlayup = (scrbef - 220)/100,
            dlaydw = (scraft - 220)/100,
            opacup = dlayup > 1 ? 1 : dlayup < 0 ? 0 : dlayup,
            opacdw = dlaydw > 1 ? 1 : dlaydw < 0 ? 0 : dlaydw;
        if ( scrbef === 0 ) {
            e.addClass('scrolled-top').removeClass('scrolled-bot');
        } else if ( scraft === 0 ) {
            e.addClass('scrolled-bot').removeClass('scrolled-top');
        } else {
            e.removeClass('scrolled-top scrolled-bot');
        }
        arrowup.css('opacity', opacup);
        arrowdw.css('opacity', opacdw);
    });

到目前为止,一切都很好。它的作用是:

  • 初始化.scrollable元素上的jScrollPane
  • 根据内容位置添加或删除scrolled-topscrolled-bot
  • 根据内容的位置控制箭头的不透明度(顶部和底部有很多填充,所以我只希望箭头在实际内容到达屏幕边界时出现(

紧接着,我有了这个:

    var throttleTimeout;
    $(window).resize(function(){
        if ( !throttleTimeout ) {
            throttleTimeout = setTimeout( function(){
                    api.reinitialise();
                    throttleTimeout = null;
                }, 500
            );
        }
    });
    $('.deployer').click(function(e){
        api.reinitialise();
    });
});

现在,这很简单;调整窗口大小时重新初始化的代码直接来自文档。

然而,一旦调用了reinitialise(),那么在调整窗口大小或单击.deployer元素后,控制箭头不透明度的前一个代码就会停止工作——尽管奇怪的是,scrolled-topscrolled-bot类仍然可以正确添加或删除。

有人知道是什么导致了这种行为,以及如何解决吗?

干杯。

发现发生了什么。

每当重新初始化时,基本上所有内容都会被重新设置,因此以前存储在arrowuparrowdw中的元素不再存在。添加

var arrowup = e.find('.jspArrowUp'),
    arrowdw = e.find('.jspArrowDown');

再次在每个CCD_ 11完成特技之后。