making iscroll to work when loading a div dynamically

ぐ巨炮叔叔 提交于 2019-12-04 03:22:29

It's better to use the refresh() method in iScroll, which will recalculate the height.

myScroll.refresh();

I had a similar problem, and just refresh() didn't help, so didn't help destroying and recreating iScroll. In my case I was loading a lot of elements into iScroll div. What did solve the problem is setTimeout(). As MASTERING THE REFRESH() METHOD said adding even 0ms to a setTimeout() would solve a lot of problems. In my case it was 500ms. =

here is code sample:

    var myScroll;

    function createIScroll(){
    myScroll = new iScroll('wrapper');
    }

    function iScrollRefresh(){
      setTimeout(function(){
         myScroll.refresh(); 
      }, 500);
    }
$ajax(){ 
//receiving data
}
function someFunction(){
  //dynamic content is created
  iScrollRefresh();
}

My problem was that refresh() function was executed before content was inserted into DOM, so increasing timeout helped. I hope it helps to beginners like me.

ryuutatsuo

try this, when you insert your new data into iScroll do these steps

//myScroll is a global variable you initialize iScroll on
myScroll.destroy();
myScroll = null;
loaded();//this is a functions where you have have your iScroll initializer 

To watch height changes:

setInterval(function () {
    var newScrollerHeight = $scroller.innerHeight();

    if (newScrollerHeight !== prevScrollerHeight) {
         prevScrollerHeight = newScrollerHeight;
         myScroll.refresh();
    }
}, 500);

Take a look at iScroll4
In iscroll.js, I see experimental option: checkDOMChanges: false,// Experimental
You can enable and use it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!