scrolllistview_学习笔记

本文是关于ScrollListView JavaScript第三方库的学习笔记,详细介绍了tpl.js模板引擎的语法和使用方法,包括如何进行字符串转换。同时,解析了scrollViewList.js中的onscroll事件处理函数,并在example.js中展示了如何初始化、赋予数据以及实现Cell的循环利用赋值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ScrollListView(JS第三方库) 学习笔记

github地址 https://github.com/ryanseddon/ScrollListView

tpl.js (JS的模板引擎)

就是帮我们把带有JavaScript代码的伪html语句翻译为html的东西

(function(){ })();

当一个匿名函数被括起来,然后再在后面加一个括号,这个匿名函数就能立即运行起来!

模板的语法

  • 用正常的方式书写html
  • 用<% %>嵌套JavaScript语句
  • 用<%= %>嵌套JavaScript 变量值

字符串转换

想得到预期html字符串,我们必须设法让模板内部的javascript变量置换、javaScript语句执行,也就是把JavaScript代码剥离出来执行,把其它html语句拼接为一个字符串

  • 把<%=xxx%> 替换为 ‘);p.push(xxx);p.push(‘
html=html.replace(/<%=(.*?)%>/g,"');p.push(xxx);p.push('");
  • 把<%替换为 ‘);
html=html.replace(/<%/g,"');");
  • 把%> 替换为 p.push(‘
html=html.replace(/%>/g,"p.push('");

scrollViewList.js

onscroll:function onScroll(){}

  • 滚动方向通过比较滚动头部位置的变化
onScroll: function onScroll() {
    scrollTop = body.scrollTop;
    this.direction = scrollTop - lastScrollTop;
    this.checkCells();
}
  • 得到可用的cell,取滚出屏幕后的cell除以cell的总和获得的模
getCurrentCell: function getCurrentCell(count) {
    return this.cells[count % this.cells.length];
}
  • 当我们能看到的最上面cell滚动出屏幕的时候进行的操作
this.cellsOutOfViewportCount++;
this.cellIndex = this.cellsOutOfViewportCount;
this.elementStyle.paddingTop = parseInt(this.elementStyle.paddingTop || 0, 10) + this.CELLHEIGHT + 'px';
this.currentCell.style[orderProp] = this.cellIndex;
this.renderCell(this.currentCell, this.cellIndex-1);

example.js (如何使用)

使用方法 初始化

var ScrollListView = require('ScrollListView');

var scrollListView = new ScrollListView({
  element: document.querySelector('.list'),
  data: [... array of objects to render],
  cellHeight: 150,
  renderFn: render,
  renderCellFn: renderCellFn
});
First HeaderSecond Header
elementThe element that the re-usable cells will live inside. You don’t add children to this the renderFn takes care of that.
dataThe array of data the list will read from work scroll offset and heights
cellHeightThe abslute height in px that the cell will be.
renderFnThis function gets called on initialisation to render the initial list.
renderCellFnWhen a cell is determined to be far enough out of the viewport this will be called with the element you can safely overwrite with the new data and the index in your data array passed in earlier.

给予数据

function render(count) {
    var cellsFrag = document.createDocumentFragment();

    for(var i = 0; i < count; i++) {
        var cell = document.createElement('li'),
        tweet = this.data[i];

        cell.className = 'scrolllist__cell gpuarise';
        cell.innerHTML = tmpl('tweet_tpl', tweet);
        cell.style.order = i+1;
        cellsFrag.appendChild(cell);
    }

    listContainer.appendChild(cellsFrag);
    listContainer.style.minHeight = this.data.length * this.CELLHEIGHT + 'px';
}

Cell循环利用赋值

//tweet_tpl是id
function renderCell(cell, index) {
    cell.innerHTML = tmpl('tweet_tpl', this.data[index]);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值