jQuery實(shí)現(xiàn)的上拉刷新功能組件示例
本文實(shí)例講述了jQuery實(shí)現(xiàn)的上拉刷新功能組件。分享給大家供大家參考,具體如下:
技術(shù)要點(diǎn):
1、jQuery的插件寫法
2、上拉刷新步驟分解
3、css樣式
jQuery的插件寫法:
$.fn.pluginName = function() { return this.each(function () { fn(); }) };
上拉刷新步驟分解:
上拉刷新可以分解成三個(gè)部分:一是開始(start),記錄當(dāng)前鼠標(biāo)的位置;二是移動(dòng)(move),根據(jù)下拉的位移響應(yīng)不同的視圖;三是結(jié)束(end),刷新頁(yè)面。
;!function ($) { "use strict"; var PTR = function (ele) { this.container = $(ele); this.container.addClass('pull-to-refresh'); this.distance = 60; // 設(shè)置參考的下拉位移 this.attachEvent(); }; // 判斷是否有touch事件發(fā)生 var isTouch = (function () { var isSupportTouch = !!'ontouchstart' in document || window.documentTouch; return isSupportTouch; })(); var touchEvents = { start: isTouch ? 'touchstart': 'mousedown', move: isTouch ? 'touchmove':'mousemove', end: isTouch ? 'touchend': 'mouseup' }; // 獲取事件發(fā)生時(shí)相對(duì)于文檔的距離(含滾動(dòng)距離) function getTouchPosition(e) { var e = e.orinalEvent || e; console.log(e) if(e.type === 'touchstart' || e.type === 'touchmove' || e.type === 'touchend') { return { x: e.targetTouches[0].pageX, y: e.targetTouches[0].pageY } }else { return { x: e.pageX, y: e.pageY } } }; PTR.prototype.touchStart = function (e) { var p = getTouchPosition(e); this.start = p; this.diffX = this.diffY = 0; }; PTR.prototype.touchMove = function (e) { if(this.container.hasClass('refreshing')) return; if(!this.start) return false; var p = getTouchPosition(e); this.diffX = p.x - this.start.x; this.diffY = p.y - this.start.y; if(this.diffY < 0) return; this.container.addClass('touching'); e.preventDefault(); e.stopPropagation(); // 設(shè)置container的位移小于頁(yè)面滾動(dòng)的距離,給人一種用力下拉的錯(cuò)覺(jué),提升用戶體驗(yàn) this.diffY = Math.pow(this.diffY, .8); this.container.css('transform', 'translate3d(0,'+ this.diffY +'px, 0)'); if(this.diffY < this.distance) { this.container.removeClass('pull-up').addClass('pull-down') }else { this.container.removeClass('pull-down').addClass('pull-up') } }; PTR.prototype.touchEnd = function (e) { var _this = this; this.start = false; this.container.removeClass('pull-down'); this.container.removeClass('pull-up'); this.container.removeClass('touching'); this.container.css('transform',''); if(this.diffY >= this.distance) { this.container.addClass('refreshing'); this.container.trigger('pull-to-refresh') } }; // 事件處理程序,通過(guò)$.proxy(fn, content)綁定執(zhí)行函數(shù)的上下文。 PTR.prototype.attachEvent = function () { var ele = this.container; ele.on(touchEvents.start, $.proxy(this.touchStart, this)); ele.on(touchEvents.move, $.proxy(this.touchMove, this)); ele.on(touchEvents.end, $.proxy(this.touchEnd, this)); }; // 實(shí)例化構(gòu)造函數(shù) var pullToRefresh = function (ele) { new PTR(ele) }; var pullToRefreshDone = function (ele) { $(ele).removeClass('refreshing'); }; // jQuery 插件編寫的一般模式 $.fn.pullToRefresh = function () { // return 是插件可鏈?zhǔn)秸{(diào)用 // this 在這里是一個(gè)jQuery對(duì)象,相當(dāng)于$(ele)。因?yàn)樵诩磿r(shí)執(zhí)行函數(shù)作用域中,沒(méi)必要用“$(this)”的方式來(lái)把this包裹到一個(gè)jQuery對(duì)象中,因?yàn)閠his本身已經(jīng)是被包裝好的jQuery對(duì)象。 // this.each()使插件代碼為多元素集合中的每個(gè)元素單獨(dú)起作用 return this.each(function () { pullToRefresh(this); }) }; $.fn.pullToRefreshDone = function () { return this.each(function () { pullToRefreshDone(this); }) } }(window.jQuery);
HTML代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="pull-to-refresh.css" rel="external nofollow" > <style> p { margin-top: 0; } </style> </head> <body> <div class="pull-to-refresh_layer"> <div class="pull-to-refresh-arrow">↓</div> <div class="pull-to-refresh-preloader"></div> <div class="down">下拉刷新</div> <div class="up">釋放刷新</div> <div class="refresh">正在刷新</div> </div> <div> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus amet aperiam, architecto at aut beatae dignissimos eaque est ex fugi at incidunt inventore natus nemo nostru m omnis quos repellat ut voluptas! </p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus amet aperiam, architecto at aut beatae dignissimos eaque est ex fugi at incidunt inventore natus nemo nostru m omnis quos repellat ut voluptas! </p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus amet aperiam, architecto at aut beatae dignissimos eaque est ex fugi at incidunt inventore natus nemo nostru m omnis quos repellat ut voluptas! </p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus amet aperiam, architecto at aut beatae dignissimos eaque est ex fugi at incidunt inventore natus nemo nostru m omnis quos repellat ut voluptas! </p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus amet aperiam, architecto at aut beatae dignissimos eaque est ex fugi at incidunt inventore natus nemo nostru m omnis quos repellat ut voluptas! </p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus amet aperiam, architecto at aut beatae dignissimos eaque est ex fugi at incidunt inventore natus nemo nostru m omnis quos repellat ut voluptas! </p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus amet aperiam, architecto at aut beatae dignissimos eaque est ex fugi at incidunt inventore natus nemo nostru m omnis quos repellat ut voluptas! </p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus amet aperiam, architecto at aut beatae dignissimos eaque est ex fugi at incidunt inventore natus nemo nostru m omnis quos repellat ut voluptas! </p> </div> <script src="../jquery-1.8.3.min.js"></script> <script src="pull-to-refresh.js"></script> <script> $(function () { $(document.body).pullToRefresh().on('pull-to-refresh', function () { setTimeout(function () { $(document.body).pullToRefreshDone(); }, 2000) }); }) </script> </body> </html>
CSS代碼如下:
.pull-to-refresh { margin-top: -50px; transition: transform .4s; } .pull-to-refresh .pull-to-refresh-preloader, .pull-to-refresh .up, .pull-to-refresh .refresh { display: none; } .pull-to-refresh.refreshing { transform: translate3d(0,50px,0); } .refreshing .pull-to-refresh-arrow, .refreshing .down, .refreshing .up { display: none; } .refreshing .refresh, .refreshing .pull-to-refresh-preloader { display: inline-block; } .pull-to-refresh_layer { height: 30px; line-height: 30px; padding-bottom: 10px; } .pull-down .pull-to-refresh_layer .up, .pull-down .pull-to-refresh_layer .refresh { display: none; } .pull-down .pull-to-refresh_layer .down{ display: inline-block; } .pull-up .pull-to-refresh_layer .up{ display: inline-block; } .pull-up .pull-to-refresh_layer .down, .pull-up .pull-to-refresh_layer .refresh { display: none; } .pull-up .pull-to-refresh-arrow { transform: rotate(180deg) translate3d(0, 0, 0); } .pull-to-refresh-arrow { display: inline-block; z-index: 10; margin-right: 4px; transition-duration: 300ms; transform: rotate(0deg) translate3d(0, 0, 0); } .pull-to-refresh_layer { display: inline-block; } .pull-to-refresh-preloader { display: inline-block; } .pull-down { } .pull-up { } .down { display: inline-block; } .up { display: inline-block; } .refresh { display: inline-block; }
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具 http://tools.jb51.net/code/HtmlJsRun 測(cè)試上述代碼運(yùn)行效果。
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery頁(yè)面元素操作技巧匯總》、《jQuery常見(jiàn)事件用法與技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- jQuery模擬原生態(tài)App上拉刷新下拉加載更多頁(yè)面及原理
- jquery使用iscorll實(shí)現(xiàn)上拉、下拉加載刷新
- JQuery插件iScroll實(shí)現(xiàn)下拉刷新,滾動(dòng)翻頁(yè)特效
- jQuery+AJAX實(shí)現(xiàn)無(wú)刷新下拉加載更多
- jquery ajax實(shí)現(xiàn)下拉框三級(jí)無(wú)刷新聯(lián)動(dòng),且保存保持選中值狀態(tài)
- 用Jquery實(shí)現(xiàn)多級(jí)下拉框無(wú)刷新的聯(lián)動(dòng)
- 基于jQuery Ajax實(shí)現(xiàn)下拉框無(wú)刷新聯(lián)動(dòng)
- jQuery 翻頁(yè)組件yunm.pager.js實(shí)現(xiàn)div局部刷新的思路
- jquery刷新頁(yè)面的實(shí)現(xiàn)代碼(局部及全頁(yè)面刷新)
- 用Jquery.load載入頁(yè)面實(shí)現(xiàn)局部刷新
- jQuery實(shí)現(xiàn)AJAX定時(shí)刷新局部頁(yè)面實(shí)例
- JQuery+Ajax無(wú)刷新分頁(yè)的實(shí)例代碼
相關(guān)文章
jQuery.datatables.js插件用法及api實(shí)例詳解
這篇文章主要介紹了jquery插件之jQuery.datatables.js用法及api實(shí)例詳解,本文給大家介紹的非常詳細(xì)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10jquery分頁(yè)插件jquery.pagination.js使用方法解析
這篇文章主要針對(duì)js分頁(yè)插件jquery.pagination.js使用方法進(jìn)行解析,很實(shí)用的分頁(yè)插件,感興趣的小伙伴們可以參考一下2016-04-04jQuery.ajax 跨域請(qǐng)求webapi設(shè)置headers的解決方案
需要通過(guò)服務(wù)器端設(shè)置響應(yīng)頭、正確響應(yīng)options請(qǐng)求,正確設(shè)置 JavaScript端需要設(shè)置的headers信息方能實(shí)現(xiàn),本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08jquery css 設(shè)置table的奇偶行背景色示例
jquery css 設(shè)置table的奇偶行背景色2014-06-06有關(guān)easyui-layout中的收縮層無(wú)法顯示標(biāo)題的解決辦法
本文由腳本之家小編給大家介紹有關(guān)easyui-layout中的收縮層無(wú)法顯示標(biāo)題的原因分析及解決辦法,感興趣的朋友可以參考下2016-05-05jQuery height()、innerHeight()、outerHeight()函數(shù)的區(qū)別詳解
這篇文章主要介紹了jQuery height()、innerHeight()、outerHeight()函數(shù)的區(qū)別詳解,需要的朋友可以參考下2016-05-05vue登錄頁(yè)面cookie的使用及頁(yè)面跳轉(zhuǎn)代碼
這篇文章主要介紹了vue登錄頁(yè)面cookie的使用及頁(yè)面跳轉(zhuǎn)代碼功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07