熱門文章

循環列表製作方法

Q:如何設計循環列表?
const list = new eui.List;
const data = new eui.ArrayCollection;
for(let i = 1; i<=20; i++) data.addItem(`news${i}`);
list.dataProvider = data;

const scrl = new eui.Scroller;
scrl.viewport = list;
scrl.width = 300;
scrl.height = 300;
this.addChild(scrl);

const updateFunc: Function = function () {
    const startV = (scrl.viewport.contentHeight - scrl.viewport.height) >> 1;
    const dist = scrl.viewport.scrollV - startV;
    const itemHeight = scrl.viewport.contentHeight / data.length;
    const moveCount = ~~(Math.abs(dist) / itemHeight);
    if (moveCount)
    if (dist > 0) {
        for (let i = 0; i < moveCount; i++)data.addItem(data.removeItemAt(0));
        scrl.viewport.scrollV -= moveCount * itemHeight;
    } else if (dist < 0) {
        for (let i = 0; i < moveCount; i++)data.addItemAt(data.removeItemAt(data.length - 1), 0);
        scrl.viewport.scrollV += moveCount * itemHeight;
    }
}

scrl.once(eui.UIEvent.CHANGE_START, updateFunc, this);
scrl.addEventListener(eui.UIEvent.CHANGE_END, updateFunc, this);

沒有留言:

張貼留言