iscroll 上拉下拉刷新AJAX后,样式有问题

尤天翔 发布于 2013/04/10 15:18
阅读 12K+
收藏 5
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<title>iScroll demo: simple</title>
<script type="text/javascript" src="js/iscroll.js"></script>
<script type="text/javascript" charset="utf-8" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" charset="utf-8" src="js/jquery-1.9.1.js"></script>
<script type="text/javascript">

var myScroll,
	pullDownEl, pullDownOffset,
	pullUpEl, pullUpOffset,
	generatedCount = 0;
	n=0;

function pullDownAction () {// 这个函数是下拉刷新,但是刷新后样式有问题,高度不变,滚动条拉到底部了,内容却没有达到底部。
	setTimeout(function () {//如果去掉$.ajax后换成其他的循环函数,正常。
	var el, li, i;
	el = document.getElementById('thelist');
	$.ajax({ url:'http://202.198.133.181/android/andrew2/jsonp.php?callback=?&num='+(++n)+'',//从该处获取数据
	     dataType:'jsonp',
	    success:function(data){
	        for (i=0;i<5;i++)
	         {
		var time = getLocalTime(data[i]['pub_time']);
		li = document.createElement('li');
                  li.innerHTML = "<a href='news.html?news_id="+data[i]['news_id']+"'>"+data[i]['title']+"</a>"+time;
		el.insertBefore(li, el.childNodes[0]);						}
	}//;success不能跟分号
});myScroll.refresh();}, 1000);
}

function pullUpAction () {
	setTimeout(function () {
		var el, li, i;
		el = document.getElementById('thelist');
			for (i=0;i<5;i++ )
			{li = document.createElement('li');
			li.innerHTML = 'Generated row ' + (++generatedCount);
			el.appendChild(li, el.childNodes[0]);
			};myScroll.refresh();}, 1000);
}

function loaded() {
	pullDownEl = document.getElementById('pullDown');
	pullDownOffset = pullDownEl.offsetHeight;
	pullUpEl = document.getElementById('pullUp');
	pullUpOffset = pullUpEl.offsetHeight;

	myScroll = new iScroll('wrapper', {
		useTransition: true,
		topOffset: pullDownOffset,
		onRefresh: function () {
			if (pullDownEl.className.match('loading')) {
				pullDownEl.className = '';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Pull down to refresh...';
			} else if (pullUpEl.className.match('loading')) {
				pullUpEl.className = '';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';
			}
		},
		onScrollMove: function () {
			if (this.y > 5 && !pullDownEl.className.match('flip')) {
				pullDownEl.className = 'flip';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Release to refresh...';
				this.minScrollY = 0;
			} else if (this.y < 5 && pullDownEl.className.match('flip')) {
				pullDownEl.className = '';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Pull down to refresh...';
				this.minScrollY = -pullDownOffset;
			} else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
				pullUpEl.className = 'flip';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Release to refresh...';
				this.maxScrollY = this.maxScrollY;
			} else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
				pullUpEl.className = '';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';
				this.maxScrollY = pullUpOffset;
			}
		},
		onScrollEnd: function () {
			if (pullDownEl.className.match('flip')) {
				pullDownEl.className = 'loading';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Loading...';
				pullDownAction();	// Execute custom function (ajax call?)
			} else if (pullUpEl.className.match('flip')) {
				pullUpEl.className = 'loading';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Loading...';
				pullUpAction();	// Execute custom function (ajax call?)
			}
		}
	});

	setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);
}

document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 200); }, false);
</script>
	<script type="text/javascript" charset="utf-8">
//函数:将时间戳变成日期形式
function getLocalTime(nS) {
   return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
}

//获取数据
function xunlei(){
	var el, li, i,option,ell;
	el = document.getElementById('thelist');
	$.ajax({					url:'http://202.198.133.181/android/andrew2/jsonp2.php?callback=?',//从该处获取数据
	dataType:'json',
	success:function(data){
	for (i=0;i<20;i++)
	{
	li = document.createElement('li');
	var time = getLocalTime(data[i]['pub_time']);  li.innerHTML = "<a href='news.html?news_id="+data[i]['news_id']+"'>"+data[i]['title']+"</a>"+time;
	el.appendChild(li, el.childNodes[0]);
	}
}//;success不能跟分号
});
}
</script>
<script type="text/javascript">
window.onload = xunlei;
</script>

<style type="text/css" media="all">
body,ul{
	padding:0;
	margin:0;
	border:0;
}

li{
	padding:0;
	margin:0;
	border:0;
    overflow:hidden;
	width:100%;
    text-overflow:ellipsis;
    white-space:nowrap;
}

body {
	font-size:12px;
	-webkit-user-select:none;
    -webkit-text-size-adjust:none;
	font-family:helvetica;
}

#header {
	position:absolute; z-index:2;
	top:0; left:0;
	width:100%;
	height:45px;
	line-height:45px;
	background-color:#d51875;
	background-image:-webkit-gradient(linear, 0 0, 0 100%, color-stop(0, #fe96c9), color-stop(0.05, #d51875), color-stop(1, #7b0a2e));
	background-image:-moz-linear-gradient(top, #fe96c9, #d51875 5%, #7b0a2e);
	background-image:-o-linear-gradient(top, #fe96c9, #d51875 5%, #7b0a2e);
	padding:0;
	color:#eee;
	font-size:20px;
	text-align:center;
}

#header a {
	color:#f3f3f3;
	text-decoration:none;
	font-weight:bold;
	text-shadow:0 -1px 0 rgba(0,0,0,0.5);
}

#footer {
	position:absolute; z-index:2;
	bottom:0; left:0;
	width:100%;
	height:48px;
	background-color:#222;
	background-image:-webkit-gradient(linear, 0 0, 0 100%, color-stop(0, #999), color-stop(0.02, #666), color-stop(1, #222));
	background-image:-moz-linear-gradient(top, #999, #666 2%, #222);
	background-image:-o-linear-gradient(top, #999, #666 2%, #222);
	padding:0;
	border-top:1px solid #444;
}

#wrapper {
	position:absolute; z-index:1;
	top:45px; bottom:48px; left:-9999px;
	width:100%;
	background:#aaa;
	overflow:auto;
}

#scroller {
	position:absolute; z-index:1;
/*	-webkit-touch-callout:none;*/
	-webkit-tap-highlight-color:rgba(0,0,0,0);
	width:100%;
	padding:0;
}

#scroller ul {
	list-style:none;
	padding:0;
	margin:0;
	width:100%;
	text-align:left;
}

#scroller li {
	padding:0 10px;
	height:40px;
	line-height:40px;
	border-bottom:1px solid #ccc;
	border-top:1px solid #fff;
	background-color:#fafafa;
	font-size:14px;
}

#myFrame {
	position:absolute;
	top:0; left:0;
}



/**
 *
 * Pull down styles
 *
 */
#pullDown, #pullUp {
	background:#fff;
	height:40px;
	line-height:40px;
	padding:5px 10px;
	border-bottom:1px solid #ccc;
	font-weight:bold;
	font-size:14px;
	color:#888;
}
#pullDown .pullDownIcon, #pullUp .pullUpIcon  {
	display:block; float:left;
	width:40px; height:40px;
	background:url(pull-icon@2x.png) 0 0 no-repeat;
	-webkit-background-size:40px 80px; background-size:40px 80px;
	-webkit-transition-property:-webkit-transform;
	-webkit-transition-duration:250ms;
}
#pullDown .pullDownIcon {
	-webkit-transform:rotate(0deg) translateZ(0);
}
#pullUp .pullUpIcon  {
	-webkit-transform:rotate(-180deg) translateZ(0);
}

#pullDown.flip .pullDownIcon {
	-webkit-transform:rotate(-180deg) translateZ(0);
}

#pullUp.flip .pullUpIcon {
	-webkit-transform:rotate(0deg) translateZ(0);
}

#pullDown.loading .pullDownIcon, #pullUp.loading .pullUpIcon {
	background-position:0 100%;
	-webkit-transform:rotate(0deg) translateZ(0);
	-webkit-transition-duration:0ms;

	-webkit-animation-name:loading;
	-webkit-animation-duration:2s;
	-webkit-animation-iteration-count:infinite;
	-webkit-animation-timing-function:linear;
}

@-webkit-keyframes loading {
	from { -webkit-transform:rotate(0deg) translateZ(0); }
	to { -webkit-transform:rotate(360deg) translateZ(0); }
}

</style>
</head>
<body>

<div id="header"><a href="http://cubiq.org/iscroll">iScroll</a></div>
<div id="wrapper">
	<div id="scroller">
		<div id="pullDown">
			<span class="pullDownIcon"></span><span class="pullDownLabel">Pull down to refresh...</span>
		</div>
		<ul id="thelist">
		</ul>
		<div id="pullUp">
			<span class="pullUpIcon"></span><span class="pullUpLabel">Pull up to refresh...</span>
		</div>
	</div>
</div>
<div id="footer">
</div>
</body>
</html>

这个例子是网上比较常见的,下过来的时候,上拉下拉刷新都是添加的静态元素,我添加ajax后,就出现了样式问题,刷新后滚动条长度不变,但是内容长度变了,达不到底部。得改变下浏览器高度才能使滚动条正常。。不知道什么原因。

急需大神求解!谢谢!给个思路也行

加载中
0
ducp123
ducp123

在myScroll.refresh();}, 1000);后面

添加$("#add").listview("refresh");

0
ducp123
ducp123
li.innerHTML = "<a href='news.html?news_id="+data[i]['news_id']+"'>"+data[i]['title']+"</a>"+time; 这个怎么添加<a>标签后刷新不出数据来???
0
曹医生
我也遇到过,把ajax设置成同步就行了。$.ajax({async : false,)
海农
海农
此答案是对的。谢谢啊!
Mr_zhaoy
Mr_zhaoy
此方法可以.
0
海农
海农
@曹医生 的方法可以,这是什么原因导致的呢?求解。
0
王逍遥77
王逍遥77

你设置iscroll 的外框高度 #wrapper 为window.innerHeight(当前手机端的设备高度值);就解决问题了

林雉
林雉
不口以啊
返回顶部
顶部