2015-06-06 49 views
0

我在使用jScrollPane的reinitialise()方法时遇到问题。每当我把它叫做,东西,我在第一初始化实现停止工作......如何重新初始化jScrollPane而不会丢失第一次初始化时添加的功能?

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

$('.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); 
    }); 

到目前为止,一切都很好。什么,做的是:

  • 初始化JScrollPane中的.scrollable元素
  • 取决于内容的位置,添加或删除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类仍然可以正确添加或删除。

有谁知道什么可能导致此行为,以及如何解决它?

干杯。

回答

0

找到发生了什么事。

无论何时重新初始化,基本上所有的东西都会被重新设置,因此之前存储在arrowuparrowdw中的元素不再存在。添加

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

再次在每个reinitialise()后诀窍。