Loading

JS 瀑布流加载

/**
 * 页面向下滚动加载
 * @param range [可选] 预加载高度:到达底部多少像素之前预加载
 * @param callback [可选] 回调函数:如果设置的话则优先使用回调函数
 * @param proxy	[可选] 回调函数作用域
 * @param args [可选] 预设传入回调函数的其他参数
 * @author zhangnan
 * @update 2014年3月14日 14:45:31
 */
function scrollEvent() {
	var args = Array.prototype.slice.call(arguments);
	var range = args[0] || 100;	// 预设高度
	var callback = args[1] || false;// 回调函数
	var proxy = args[2] || null;	// 作用域

	$(window).scroll(function() {
		var srollPos = $(window).scrollTop();
		var totalheight = parseFloat($(window).height()) + parseFloat(srollPos);
		if(($(document).height() - range) <= totalheight) {
			if(typeof callback === 'function')
				callback.apply(proxy, args.slice(3));
		}
	});
}

例子:

var uid = "2989019";
var myV4 = {
	cur:{'id':0, 'page':1, 'isEnd':false, 'loading':false},
	source:[10, 12, 11, 22, 21, 23],
	init: function() {	// 初始化
		var self = this;
		// window.onload = self.processHash;
		self.processHash();
		if (("onhashchange" in window) && !($.browser.msie)) {
			window.onhashchange = self.processHash;
		} else {
			var prevHash = window.location.hash;
			window.setInterval(function () {
				if (window.location.hash != prevHash) {
					prevHash = window.location.hash;
					self.processHash();
				}
			}, 100);
		}
		scrollEvent(100, self.autoLoad, self);		// 页面滚动
		chat.init();	// 发表动态
	},
	processHash:function() {	// 前进后退事件
		var hashStr = window.location.hash;
		var self = window.myV4;
		if ( hashStr ) {
			if( hasStr("#mystata_news") ) {		// 最新动态
				return self.nav(0);
			} else if( hasStr("#mystata_deals") ) {	// 交易
				return self.nav(1);
			} else if( hasStr("#mystata_talks") ) {	// 讨论
				return self.nav(2);
			} else if( hasStr("#mystock_news") ) {
				if(hasStr("#mystock_news_curr")) window.location.href="#mystock_news_curr";
				return self.nav(3);
			} else if( hasStr("#mystock_perspective") ) {
				return self.nav(4);
			} else if( hasStr("#mystock_reports") ) {
				return self.nav(5);
			}
		} else {
			window.location.href='#mystata_news';
		};
		function hasStr(str) {
			return ( hashStr != hashStr.replace(str, "") ) ? true : false;
		}
	},
	nav: function( id, hold ) {
		var hold = hold || false;
		this.cur = {'id':id, 'page':1};
		if(id < 3) {
			$('#mystata_sub').show();
			$('#mystock_sub').hide();
			$('#myV4Nav li').eq(0).addClass('cur').siblings().removeClass('cur');
			$('#mystata_sub li').eq(id).addClass('cur').siblings().removeClass('cur');
		} else { // 自选股处理方式
			$('#mystock_sub').show();
			$('#mystata_sub').hide();
			$('#myV4Nav li').eq(1).addClass('cur').siblings().removeClass('cur');
			var cur = id - 3;
			$('#mystock_sub li').eq(cur).addClass('cur').siblings().removeClass('cur');
		}

		if(!hold)
			this.load(this.source[id], 1);
	},
	load: function(type, page) {
		var self = this;
		var is_append = (self.source[self.cur.id] == type && page > 1);

		// 到底提示
		if(is_append && self.cur.isEnd) {
			$('#loading').show();
			return false;
		}

		// 提示信息
		if(page < 2) {
			$('#listDeal').html('');
		}

		$('#loading').html('数据加载中...').show();

		if(self.cur.loading) return false;
		self.cur.loading = true;
		// 加载数据
		$.ajax({
			url:'/feeds/myV4',
			type:'post',
			cache:false,
			data:{'type':type, 'page':page},
			dataType:'json',
			timeout:10000,
			success:function(data) {
				if(data && data.data.length > 0) {
					if(is_append) {
						$('#listDeal').append(data.data);
					} else {
						$('#listDeal').html(data.data);
					}
					$('#loading').hide();
					var from = false;
					switch(type) {
						case 10: from = '?f=h_feed'; break;
						case 12: from = '?f=h_trade'; break;
						case 11: from = '?f=h_discuss'; break;
					}
					if(from) {
						addFromParam('#listDeal', from);
					}

					// 自选股信息添加分享
					if(type > 20)
					{
						$('#listDeal .jsShare').hover(function(){
							$(this).nextAll('.shareBox').show();
						}, function(){
							var self = this;
							var is_out = setTimeout(function(){$(self).nextAll('.shareBox').hide();}, 200);
							$(self).nextAll('.shareBox').hover(function(){
								clearTimeout(is_out);
							}, function(){
								$(this).hide();
							});
						})
					}

					// 交易信息添加交易故事
					var _ods = $('#listDeal .jygsJs');
					if(_ods.length > 0)
					{
						_ods.hover(function(){
							dealStory.h($(this));
						}, function(){
							var self = this;
							var share = $(self).closest('div').next('div');
							var is_out = setTimeout(function(){share.hide();}, 200);
							share.hover(function(){
								clearTimeout(is_out);
							}, function(){
								$(this).hide();
							});
						});
					}

				} else {
					if(page > 1) {
						self.cur.isEnd = true;
						$('#loading').html('已经是最后一页了!').show();
					} else {
						var users = '<div><p style="margin: 0px; padding: 0px; border: 0px; font-family: \'microsoft yahei\', simhei; font-size: 17px; line-height: 25.19999885559082px; vertical-align: baseline; color: rgb(180, 183, 189); background-color: rgb(255, 255, 255);">尚无最新动态,订阅炒股高手后,当其有新交易或观点时,会实时出现在这里。</p><a href="http://www.gu360.com/ranking?f=home" style="margin: 0px; padding: 0px; border: 0px; font-family: \'microsoft yahei\', simhei; font-size: 17px; line-height: 25.19999885559082px; vertical-align: baseline; color: rgb(188, 154, 108); -webkit-transition: color 200ms linear; text-decoration: none; cursor: pointer; outline: none; background-color: rgb(255, 255, 255);">立即去订阅炒股高手</a></div>';
						var news = '<div><p style="margin: 0px; padding: 0px; border: 0px; font-family: \'microsoft yahei\', simhei; font-size: 17px; line-height: 25.19999885559082px; vertical-align: baseline; color: rgb(180, 183, 189); background-color: rgb(255, 255, 255);">暂无自选股资讯,请先添加自选股</p></div>';
						var html = (type > 20) ? news : users;
						$('#loading').html(html).show();
					}
				}
				self.cur.loading = false;
			},
			error:function(XMLHttpRequest, textStatus, errorThrown) {
				self.cur.loading = false;
				$('#loading').html('数据获取失败,请重新刷新页面获取数据。').show();
			}
		});
	},
	autoLoad: function() {	// callback fn
		var type = this.source[this.cur.id];
		var page = ++this.cur.page;
		this.load(type, page);
	}
};

/**
 * 页面向下滚动加载
 * @param range [可选] 预加载高度:到达底部多少像素之前预加载
 * @param callback [可选] 回调函数:如果设置的话则优先使用回调函数
 * @param proxy	[可选] 回调函数作用域
 * @param args [可选] 预设传入回调函数的其他参数
 * @author zhangnan
 * @update 2014年3月14日 14:45:31
 */
function scrollEvent() {
	var args = Array.prototype.slice.call(arguments);
	var range = args[0] || 100;	// 预设高度
	var callback = args[1] || false;// 回调函数
	var proxy = args[2] || null;	// 作用域

	$(window).scroll(function() {
		var srollPos = $(window).scrollTop();
		var totalheight = parseFloat($(window).height()) + parseFloat(srollPos);
		if(($(document).height() - range) <= totalheight) {
			if(typeof callback === 'function')
				callback.apply(proxy, args.slice(3));
		}
	});
}
posted @ 2016-11-17 19:41  纯白、色  阅读(807)  评论(0编辑  收藏  举报