qiaowei | f044a74 | 2019-07-10 16:04:20 +0800 | [diff] [blame^] | 1 | /** |
| 2 | * aui-scroll.js |
| 3 | * @author 流浪男 |
| 4 | * @todo more things to abstract, e.g. Loading css etc. |
| 5 | * Licensed under the MIT license. |
| 6 | * http://www.opensource.org/licenses/mit-license.php |
| 7 | */ |
| 8 | (function(window) { |
| 9 | 'use strict'; |
| 10 | var isToBottom = false,isMoved = false; |
| 11 | var auiScroll = function (params,callback) { |
| 12 | this.extend(this.params, params); |
| 13 | this._init(params,callback); |
| 14 | } |
| 15 | auiScroll.prototype = { |
| 16 | params: { |
| 17 | listren:false, |
| 18 | distance: 100 |
| 19 | }, |
| 20 | _init : function(params,callback) { |
| 21 | var self = this; |
| 22 | if(self.params.listen){ |
| 23 | document.body.addEventListener("touchmove", function(e){ |
| 24 | self.scroll(callback); |
| 25 | }); |
| 26 | document.body.addEventListener("touchend", function(e){ |
| 27 | self.scroll(callback); |
| 28 | }); |
| 29 | } |
| 30 | window.onscroll = function(){ |
| 31 | self.scroll(callback); |
| 32 | } |
| 33 | }, |
| 34 | scroll : function (callback) { |
| 35 | var self = this; |
| 36 | var clientHeight = document.documentElement.scrollTop === 0 ? document.body.clientHeight : document.documentElement.clientHeight; |
| 37 | var scrollTop = document.documentElement.scrollTop === 0 ? document.body.scrollTop : document.documentElement.scrollTop; |
| 38 | var scrollHeight = document.documentElement.scrollTop === 0 ? document.body.scrollHeight : document.documentElement.scrollHeight; |
| 39 | |
| 40 | if (scrollHeight-scrollTop-self.params.distance <= window.innerHeight) { |
| 41 | isToBottom = true; |
| 42 | if(isToBottom){ |
| 43 | callback({ |
| 44 | "scrollTop":scrollTop, |
| 45 | "isToBottom":true |
| 46 | }) |
| 47 | } |
| 48 | }else{ |
| 49 | isToBottom = false; |
| 50 | callback({ |
| 51 | "scrollTop":scrollTop, |
| 52 | "isToBottom":false |
| 53 | }) |
| 54 | } |
| 55 | }, |
| 56 | extend: function(a, b) { |
| 57 | for (var key in b) { |
| 58 | if (b.hasOwnProperty(key)) { |
| 59 | a[key] = b[key]; |
| 60 | } |
| 61 | } |
| 62 | return a; |
| 63 | } |
| 64 | } |
| 65 | window.auiScroll = auiScroll; |
| 66 | })(window); |