先搞个DIV
< div class = " book_update" > </ div>
然后就是下拉刷新的代码:
< script>
$ ( document) . ready ( function ( ) {
var y = 0 ;
var h = 0 ;
if ( $ ( document) . scrollTop ( ) == 0 ) {
document. addEventListener ( "touchstart" , function ( e ) {
y = e. changedTouches[ 0 ] . screenY;
} )
document. addEventListener ( "touchmove" , function ( e ) {
if ( $ ( document) . scrollTop ( ) == 0 ) {
if ( e. changedTouches[ 0 ] . screenY > y) {
$ ( ".book_update" ) . css ( "height" , h++ ) . show ( ) . text ( "开始刷新.." ) ;
if ( h > 30 & h < 50 ) {
$ ( ".book_update" ) . text ( "释放可以刷新..." ) ;
} else if ( h == 50 ) {
alert ( "开始刷新" ) ;
$. ajax ( {
url: "{% url 'book_city' 1 %}" ,
type: "post" ,
data: {
"max_book_len" : '{{ len_book_num }}' ,
} ,
success : function ( res ) {
if ( res == 0 ) {
console. log ( res) ;
mui. toast ( '未查询到新的数据哦' , { duration: 'short' , type: 'div' } ) ;
} else {
for ( var i= 0 ; i< res. length; i++ ) {
console. log ( res) ;
mui. toast ( '已更新' + res. length + '条数据' , { duration: 'short' , type: 'div' } ) ;
$ ( "#txt_name" ) . text ( res[ i] [ 0 ] ) ;
$ ( "#author" ) . text ( res[ i] [ 1 ] ) ;
$ ( "#details" ) . text ( res[ i] [ 2 ] ) ;
$ ( "#times" ) . text ( res[ i] [ 3 ] ) ;
$ ( "#upload_name" ) . text ( "上传者:" , res[ i] [ 4 ] ) ;
}
}
}
} )
$ ( ".book_update" ) . hide ( ) ;
h = 0 ;
} else {
document. addEventListener ( "touchend" , function ( e ) {
$ ( ".book_update" ) . hide ( ) ;
h = 0 ;
} )
}
}
}
} )
}
} )
< / script>
基本功能已经解决,但还是有些小问题,后续再慢慢完善,暂时先这样了
最新更新:判断左右滑动超过15px则不刷新,只有下拉才会执行
< script>
$ ( document) . ready ( function ( ) {
var x = 0 ;
var y = 0 ;
var h = 0 ;
if ( $ ( document) . scrollTop ( ) == 0 ) {
document. addEventListener ( "touchstart" , function ( e ) {
y = e. changedTouches[ 0 ] . screenY;
x = e. changedTouches[ 0 ] . screenX;
} )
document. addEventListener ( "touchmove" , function ( e ) {
if ( $ ( document) . scrollTop ( ) == 0 ) {
if ( e. changedTouches[ 0 ] . screenY > y ) {
if ( e. changedTouches[ 0 ] . screenX - x > 15 | x - e. changedTouches[ 0 ] . screenX > 15 ) {
$ ( ".book_update" ) . hide ( ) ;
h = 0 ;
} else {
$ ( ".book_update" ) . css ( "height" , h++ ) . show ( ) . text ( "开始刷新.." ) ;
if ( h > 30 ) {
$ ( ".book_update" ) . text ( "释放可以刷新..." ) ;
} else {
document. addEventListener ( "touchend" , function ( e ) {
$ ( ".book_update" ) . hide ( ) ;
h = 0 ;
} )
}
}
}
}
} )
}
document. addEventListener ( "touchend" , function ( ) {
if ( h > 30 ) {
$ ( ".book_update" ) . text ( "正在刷新..." ) ;
setTimeout ( function ( ) {
$ ( ".book_update" ) . hide ( ) ;
h = 0 ;
} , 2000 ) ;
}
} )
} )
< / script>
js完全是半桶水,代码可能看起来不伦不类的,setTimeout不知道为什么没有生效,不过问题不大,已经能正常下拉刷新了