原生JS下拉加載插件分享
使用方式:
new downUpData({url:"http://192.168.1.103:8080/test/ data.json",distance:20,callback:function(resp,config){ var oUl = document.getElementById('ul'); for(var i=0;i<resp.data.length;i+=1){ oUl.innerHTML+= '<li>'+ resp.data[i].title +'</li>'; } }}).isBottom();
默認滾動到底部會去請求ajax
參數(shù)說明:
url:請求的數(shù)據(jù)地址,不支持跨域(必須)
distance:距離底部多遠加載(可選參數(shù))
callback:當滾動到指定距離后請求完ajax將會觸發(fā)這個回調(diào)函數(shù),里面有兩個參數(shù),第一個為數(shù)據(jù)(以及轉(zhuǎn)成JSON對象了,用的是JSON.parse,可能低版本瀏覽器不支持這個方法),第二個參數(shù)為傳進去的參數(shù),當你需要重新改變請求信息的時候可以用這個,比如說你想做分頁效果,就需要改變url地址。
callback(name1,name2)
name1:data
name2:配置
源代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body,ul{ margin:0; padding:0; } </style> </head> <body> <ul id="ul"> </ul> <script> function downUpData(obj){ this.config = obj; }; downUpData.prototype = { // 判斷是否到達底部 isBottom:function(){ var _this = this; var scrollH = null, clientHeight = null; scrollTop = null; distance = this.config.distance||0; h = 0; function scroll(){ scrollH = document.body.scrollHeight||document.documentElement.scrollHeight; clientHeight = window.innerHeight; scrollTop = document.body.scrollTop||document.documentElement.scrollTop; h = clientHeight + scrollTop; if(h>=scrollH-distance){ _this.ajax(); } } scroll(); window.onscroll = function(){ scroll(); }; }, // 發(fā)送AJAX請求 ajax:function(){ var _this = this; var xhr = null; if(window.XMLHttpRequest){ xhr = new XMLHttpRequest(); }else{ xhr = new ActiveXObject("Microsoft.XMLHTTP"); } xhr.open("GET",this.config.url,true); xhr.onreadystatechange = function(){ if(xhr.readyState==4&&xhr.status==200){ _this.config.callback(JSON.parse(xhr.responseText),_this.config); } } xhr.send(); } }; new downUpData({url:"http://192.168.1.103:8080/test/data.json",distance:20,callback:function(resp,config){ console.log(config) var oUl = document.getElementById('ul'); for(var i=0;i<resp.data.length;i+=1){ oUl.innerHTML+= '<li>'+ resp.data[i].title +'</li>'; } }}).isBottom(); </script> </body> </html>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
javascript中字符串替換函數(shù)replace()方法與c# 、vb 替換有一點不同
JavaScript 不像和c# vb.net 中一樣 直接就可以替換所以的要替換的字符2010-06-06JS中的public和private對象,即static修飾符
先看下面的例子,它將告訴我們在JS世界中也有C#里的public , private ,及static等2012-01-01layuiAdmin循環(huán)遍歷展示商品圖片列表的方法
今天小編就為大家分享一篇layuiAdmin循環(huán)遍歷展示商品圖片列表的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09