jquery-weui滚动加载问题解决
2017年12月25日
jquery-weui滚动加载报错问题解决
监听事件 infinite(),下面是源码,infinite(distance)接收distance参数,默认的 distance 值是 50,表示滚动到距离底部 50 px 的时候会触发加载。所以页面初始化需要把请求的数据渲染html元素填满页面才会触发。
引入js
+function ($) {
"use strict";
var Infinite = function(el, distance) {
this.container = $(el);
this.container.data("infinite", this);
this.distance = distance || 50;
this.attachEvents();
}
Infinite.prototype.scroll = function() {
var container = this.container;
var offset = container.scrollHeight() - ($(window).height() + container.scrollTop());
if(offset <= this.distance) {
container.trigger("infinite");
}
}
Infinite.prototype.attachEvents = function(off) {
var el = this.container;
var scrollContainer = (el[0].tagName.toUpperCase() === "BODY" ? $(document) : el);
scrollContainer[off ? "off" : "on"]("scroll", $.proxy(this.scroll, this));
};
Infinite.prototype.detachEvents = function(off) {
this.attachEvents(true);
}
var infinite = function(el) {
attachEvents(el);
}
$.fn.infinite = function(distance) {
return this.each(function() {
new Infinite(this, distance);
});
}
$.fn.destroyInfinite = function() {
return this.each(function() {
var infinite = $(this).data("infinite");
if(infinite && infinite.detachEvents) infinite.detachEvents();
});
}
}($);
下面是页面代码
<div id="parent" class="list">
<!--<div class="weui-cells">
<a class="weui-cell weui-cell_access" href="javascript:;">
<div class="weui-cell__bd">
<p>AA</p>
</div>
<div class="weui-cell__ft update">2018-01-01 <span class="state">A</span></div>
</a>
</div>-->
</div>
<div id="loadmore" class="weui-loadmore">
<i class="weui-loading"></i>
<span class="weui-loadmore__tips">上拉加载</span>
</div>
//上拉加载
var access = localStorage.getItem('login');
var loading = false;
var count = 0;
pullup ();
pullup ();
pullup ();
$(document.body).infinite(100).on("infinite", function() {
if(loading) return;
loading = true;
setTimeout(function(){
pullup ();
},1500);
});
function pullup (){
count++;
var request_data = {
'access_token': access,
'page': count
};
$.ajax({
type: 'post',
url: api.domain + list,
data: request_data,
success: function(res){
if(res.status == 1) {
if(res.data.data.length > 0) {
var n = res.data.data.length;
var $content = $('<div class="weui-cells"></div>');
for(var i = 0; i < n; i++) {
var health = 'v';
var $list = $('<a class="weui-cell weui-cell_access" href="/home/report/' + res.data.data[i].id + '">' +
'<div class="weui-cell__bd">' +
'<p>' + res.data.data[i].username + '</p>' +
'</div>' +
'<div class="weui-cell__ft update">' + res.data.data[i].created_at + ' <span class="state">' + health + '</span></div>' +
'</a>');
$list.appendTo($content);
}
$content.appendTo($('#parent'));
//$(document.body).destroyInfinite();
loading = false;
} else {
//$(document.body).destroyInfinite();
loading = false;
$.toast('没有更多');
$('#loadmore').addClass('hide');
}
} else {
$.toast(res.msg, 'cancel');
}
},
});
}

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
10个Python练手小程序
【程序1】 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。 2.程序源代码: for i in range(1,5): for j in range(1,5): for k in range(1,5): if( i != k ) and (i != j) and (j != k): print (i,j,k) 阿里云代金券1000元免费领取地址:https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=2a7uv47d 新老阿里云账户均可领取!可用于购买阿里云服务器ECS、云数据库RDS、虚拟主机、安骑士、DDoS高防IP等100多云计算产品。 代金券自领取之日起,有效期是30天,请及时使用,过30天后还可以重新领取。 【程序2】 题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高 于10万元,低于20万元时,低于10万元的部分按10%...
-
下一篇
早上起床后不想动,让 Python 来帮你朗读网页吧
是不是有的时候懒得自己看新闻?那么不妨试试用 Python 来朗读给你听吧。 网页转换成语音,步骤无外乎: 网页正文识别,获取到正文的文本内容; 文本转语音,通过接口将文本转换成语音文件; 语音文件的发声,即将语音文件读出; 1 网页正文识别 之所以用 Python,就是因为 Python 有着丰富的库,网页正文识别也不在话下。这里我尝试了 readability、goose3。 1.1 readability readability 支持 Python3,使用 pip install readability-lxml 安装即可。 readability 使用起来也很方便: import requestsfrom readability import Document response = requests.get('http://news.china.com/socialgd/10000169/20180616/32537640_all.html') doc = Document(response.text) print(doc.title()) 但是 readability 提取到...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8编译安装MySQL8.0.19
- MySQL数据库在高并发下的优化方案
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Docker快速安装Oracle11G,搭建oracle11g学习环境