mui.picker.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * 选择列表插件
  3. * varstion 2.0.0
  4. * by Houfeng
  5. * Houfeng@DCloud.io
  6. */
  7. (function($, window, document, undefined) {
  8. var MAX_EXCEED = 30;
  9. var VISIBLE_RANGE = 90;
  10. var DEFAULT_ITEM_HEIGHT = 40;
  11. var BLUR_WIDTH = 10;
  12. var rad2deg = $.rad2deg = function(rad) {
  13. return rad / (Math.PI / 180);
  14. };
  15. var deg2rad = $.deg2rad = function(deg) {
  16. return deg * (Math.PI / 180);
  17. };
  18. var platform = navigator.platform.toLowerCase();
  19. var userAgent = navigator.userAgent.toLowerCase();
  20. var isIos = (userAgent.indexOf('iphone') > -1 ||
  21. userAgent.indexOf('ipad') > -1 ||
  22. userAgent.indexOf('ipod') > -1) &&
  23. (platform.indexOf('iphone') > -1 ||
  24. platform.indexOf('ipad') > -1 ||
  25. platform.indexOf('ipod') > -1);
  26. //alert(isIos);
  27. var Picker = $.Picker = function(holder, options) {
  28. var self = this;
  29. self.holder = holder;
  30. self.options = options || {};
  31. self.init();
  32. self.initInertiaParams();
  33. self.calcElementItemPostion(true);
  34. self.bindEvent();
  35. };
  36. Picker.prototype.findElementItems = function() {
  37. var self = this;
  38. self.elementItems = [].slice.call(self.holder.querySelectorAll('li'));
  39. return self.elementItems;
  40. };
  41. Picker.prototype.init = function() {
  42. var self = this;
  43. self.list = self.holder.querySelector('ul');
  44. self.findElementItems();
  45. self.height = self.holder.offsetHeight;
  46. self.r = self.height / 2 - BLUR_WIDTH;
  47. self.d = self.r * 2;
  48. self.itemHeight = self.elementItems.length > 0 ? self.elementItems[0].offsetHeight : DEFAULT_ITEM_HEIGHT;
  49. self.itemAngle = parseInt(self.calcAngle(self.itemHeight * 0.8));
  50. self.hightlightRange = self.itemAngle / 2;
  51. self.visibleRange = VISIBLE_RANGE;
  52. self.beginAngle = 0;
  53. self.beginExceed = self.beginAngle - MAX_EXCEED;
  54. self.list.angle = self.beginAngle;
  55. if (isIos) {
  56. self.list.style.webkitTransformOrigin = "center center " + self.r + "px";
  57. }
  58. };
  59. Picker.prototype.calcElementItemPostion = function(andGenerateItms) {
  60. var self = this;
  61. if (andGenerateItms) {
  62. self.items = [];
  63. }
  64. self.elementItems.forEach(function(item) {
  65. var index = self.elementItems.indexOf(item);
  66. self.endAngle = self.itemAngle * index;
  67. item.angle = self.endAngle;
  68. item.style.webkitTransformOrigin = "center center -" + self.r + "px";
  69. item.style.webkitTransform = "translateZ(" + self.r + "px) rotateX(" + (-self.endAngle) + "deg)";
  70. if (andGenerateItms) {
  71. var dataItem = {};
  72. dataItem.text = item.innerHTML || '';
  73. dataItem.value = item.getAttribute('data-value') || dataItem.text;
  74. self.items.push(dataItem);
  75. }
  76. });
  77. self.endExceed = self.endAngle + MAX_EXCEED;
  78. self.calcElementItemVisibility(self.beginAngle);
  79. };
  80. Picker.prototype.calcAngle = function(c) {
  81. var self = this;
  82. var a = b = parseFloat(self.r);
  83. //直径的整倍数部分直接乘以 180
  84. c = Math.abs(c); //只算角度不关心正否值
  85. var intDeg = parseInt(c / self.d) * 180;
  86. c = c % self.d;
  87. //余弦
  88. var cosC = (a * a + b * b - c * c) / (2 * a * b);
  89. var angleC = intDeg + rad2deg(Math.acos(cosC));
  90. return angleC;
  91. };
  92. Picker.prototype.calcElementItemVisibility = function(angle) {
  93. var self = this;
  94. self.elementItems.forEach(function(item) {
  95. var difference = Math.abs(item.angle - angle);
  96. if (difference < self.hightlightRange) {
  97. item.classList.add('highlight');
  98. } else if (difference < self.visibleRange) {
  99. item.classList.add('visible');
  100. item.classList.remove('highlight');
  101. } else {
  102. item.classList.remove('highlight');
  103. item.classList.remove('visible');
  104. }
  105. });
  106. };
  107. Picker.prototype.setAngle = function(angle) {
  108. var self = this;
  109. self.list.angle = angle;
  110. self.list.style.webkitTransform = "perspective(1000px) rotateY(0deg) rotateX(" + angle + "deg)";
  111. self.calcElementItemVisibility(angle);
  112. };
  113. Picker.prototype.bindEvent = function() {
  114. var self = this;
  115. var lastAngle = 0;
  116. var startY = null;
  117. var isPicking = false;
  118. self.holder.addEventListener($.EVENT_START, function(event) {
  119. isPicking = true;
  120. event.preventDefault();
  121. self.list.style.webkitTransition = '';
  122. startY = (event.changedTouches ? event.changedTouches[0] : event).pageY;
  123. lastAngle = self.list.angle;
  124. self.updateInertiaParams(event, true);
  125. }, false);
  126. self.holder.addEventListener($.EVENT_END, function(event) {
  127. isPicking = false;
  128. event.preventDefault();
  129. self.startInertiaScroll(event);
  130. }, false);
  131. self.holder.addEventListener($.EVENT_CANCEL, function(event) {
  132. isPicking = false;
  133. event.preventDefault();
  134. self.startInertiaScroll(event);
  135. }, false);
  136. self.holder.addEventListener($.EVENT_MOVE, function(event) {
  137. if (!isPicking) {
  138. return;
  139. }
  140. event.preventDefault();
  141. var endY = (event.changedTouches ? event.changedTouches[0] : event).pageY;
  142. var dragRange = endY - startY;
  143. var dragAngle = self.calcAngle(dragRange);
  144. var newAngle = dragRange > 0 ? lastAngle - dragAngle : lastAngle + dragAngle;
  145. if (newAngle > self.endExceed) {
  146. newAngle = self.endExceed
  147. }
  148. if (newAngle < self.beginExceed) {
  149. newAngle = self.beginExceed
  150. }
  151. self.setAngle(newAngle);
  152. self.updateInertiaParams(event);
  153. }, false);
  154. //--
  155. self.list.addEventListener('tap', function(event) {
  156. elementItem = event.target;
  157. if (elementItem.tagName == 'LI') {
  158. self.setSelectedIndex(self.elementItems.indexOf(elementItem), 200);
  159. }
  160. }, false);
  161. };
  162. Picker.prototype.initInertiaParams = function() {
  163. var self = this;
  164. self.lastMoveTime = 0;
  165. self.lastMoveStart = 0;
  166. self.stopInertiaMove = false;
  167. };
  168. Picker.prototype.updateInertiaParams = function(event, isStart) {
  169. var self = this;
  170. var point = event.changedTouches ? event.changedTouches[0] : event;
  171. if (isStart) {
  172. self.lastMoveStart = point.pageY;
  173. self.lastMoveTime = event.timeStamp || Date.now();
  174. self.startAngle = self.list.angle;
  175. } else {
  176. var nowTime = event.timeStamp || Date.now();
  177. if (nowTime - self.lastMoveTime > 300) {
  178. self.lastMoveTime = nowTime;
  179. self.lastMoveStart = point.pageY;
  180. }
  181. }
  182. self.stopInertiaMove = true;
  183. };
  184. Picker.prototype.startInertiaScroll = function(event) {
  185. var self = this;
  186. var point = event.changedTouches ? event.changedTouches[0] : event;
  187. /**
  188. * 缓动代码
  189. */
  190. var nowTime = event.timeStamp || Date.now();
  191. var v = (point.pageY - self.lastMoveStart) / (nowTime - self.lastMoveTime); //最后一段时间手指划动速度
  192. var dir = v > 0 ? -1 : 1; //加速度方向
  193. var deceleration = dir * 0.0006 * -1;
  194. var duration = Math.abs(v / deceleration); // 速度消减至0所需时间
  195. var dist = v * duration / 2; //最终移动多少
  196. var startAngle = self.list.angle;
  197. var distAngle = self.calcAngle(dist) * dir;
  198. //----
  199. var srcDistAngle = distAngle;
  200. if (startAngle + distAngle < self.beginExceed) {
  201. distAngle = self.beginExceed - startAngle;
  202. duration = duration * (distAngle / srcDistAngle) * 0.6;
  203. }
  204. if (startAngle + distAngle > self.endExceed) {
  205. distAngle = self.endExceed - startAngle;
  206. duration = duration * (distAngle / srcDistAngle) * 0.6;
  207. }
  208. //----
  209. if (distAngle == 0) {
  210. self.endScroll();
  211. return;
  212. }
  213. self.scrollDistAngle(nowTime, startAngle, distAngle, duration);
  214. };
  215. Picker.prototype.scrollDistAngle = function(nowTime, startAngle, distAngle, duration) {
  216. var self = this;
  217. self.stopInertiaMove = false;
  218. (function(nowTime, startAngle, distAngle, duration) {
  219. var frameInterval = 13;
  220. var stepCount = duration / frameInterval;
  221. var stepIndex = 0;
  222. (function inertiaMove() {
  223. if (self.stopInertiaMove) return;
  224. var newAngle = self.quartEaseOut(stepIndex, startAngle, distAngle, stepCount);
  225. self.setAngle(newAngle);
  226. stepIndex++;
  227. if (stepIndex > stepCount - 1 || newAngle < self.beginExceed || newAngle > self.endExceed) {
  228. self.endScroll();
  229. return;
  230. }
  231. setTimeout(inertiaMove, frameInterval);
  232. })();
  233. })(nowTime, startAngle, distAngle, duration);
  234. };
  235. Picker.prototype.quartEaseOut = function(t, b, c, d) {
  236. return -c * ((t = t / d - 1) * t * t * t - 1) + b;
  237. };
  238. Picker.prototype.endScroll = function() {
  239. var self = this;
  240. if (self.list.angle < self.beginAngle) {
  241. self.list.style.webkitTransition = "150ms ease-out";
  242. self.setAngle(self.beginAngle);
  243. } else if (self.list.angle > self.endAngle) {
  244. self.list.style.webkitTransition = "150ms ease-out";
  245. self.setAngle(self.endAngle);
  246. } else {
  247. var index = parseInt((self.list.angle / self.itemAngle).toFixed(0));
  248. self.list.style.webkitTransition = "100ms ease-out";
  249. self.setAngle(self.itemAngle * index);
  250. }
  251. self.triggerChange();
  252. };
  253. Picker.prototype.triggerChange = function(force) {
  254. var self = this;
  255. setTimeout(function() {
  256. var index = self.getSelectedIndex();
  257. var item = self.items[index];
  258. if ($.trigger && (index != self.lastIndex || force === true)) {
  259. $.trigger(self.holder, 'change', {
  260. "index": index,
  261. "item": item
  262. });
  263. //console.log('change:' + index);
  264. }
  265. self.lastIndex = index;
  266. typeof force === 'function' && force();
  267. }, 0);
  268. };
  269. Picker.prototype.correctAngle = function(angle) {
  270. var self = this;
  271. if (angle < self.beginAngle) {
  272. return self.beginAngle;
  273. } else if (angle > self.endAngle) {
  274. return self.endAngle;
  275. } else {
  276. return angle;
  277. }
  278. };
  279. Picker.prototype.setItems = function(items) {
  280. var self = this;
  281. self.items = items || [];
  282. var buffer = [];
  283. self.items.forEach(function(item) {
  284. if (item !== null && item !== undefined) {
  285. buffer.push('<li>' + (item.text || item) + '</li>');
  286. }
  287. });
  288. self.list.innerHTML = buffer.join('');
  289. self.findElementItems();
  290. self.calcElementItemPostion();
  291. self.setAngle(self.correctAngle(self.list.angle));
  292. self.triggerChange(true);
  293. };
  294. Picker.prototype.getItems = function() {
  295. var self = this;
  296. return self.items;
  297. };
  298. Picker.prototype.getSelectedIndex = function() {
  299. var self = this;
  300. return parseInt((self.list.angle / self.itemAngle).toFixed(0));
  301. };
  302. Picker.prototype.setSelectedIndex = function(index, duration, callback) {
  303. var self = this;
  304. self.list.style.webkitTransition = '';
  305. var angle = self.correctAngle(self.itemAngle * index);
  306. if (duration && duration > 0) {
  307. var distAngle = angle - self.list.angle;
  308. self.scrollDistAngle(Date.now(), self.list.angle, distAngle, duration);
  309. } else {
  310. self.setAngle(angle);
  311. }
  312. self.triggerChange(callback);
  313. };
  314. Picker.prototype.getSelectedItem = function() {
  315. var self = this;
  316. return self.items[self.getSelectedIndex()];
  317. };
  318. Picker.prototype.getSelectedValue = function() {
  319. var self = this;
  320. return (self.items[self.getSelectedIndex()] || {}).value;
  321. };
  322. Picker.prototype.getSelectedText = function() {
  323. var self = this;
  324. return (self.items[self.getSelectedIndex()] || {}).text;
  325. };
  326. Picker.prototype.setSelectedValue = function(value, duration, callback) {
  327. var self = this;
  328. for (var index in self.items) {
  329. var item = self.items[index];
  330. if (item.value == value) {
  331. self.setSelectedIndex(index, duration, callback);
  332. return;
  333. }
  334. }
  335. };
  336. if ($.fn) {
  337. $.fn.picker = function(options) {
  338. //遍历选择的元素
  339. this.each(function(i, element) {
  340. if (element.picker) return;
  341. if (options) {
  342. element.picker = new Picker(element, options);
  343. } else {
  344. var optionsText = element.getAttribute('data-picker-options');
  345. var _options = optionsText ? JSON.parse(optionsText) : {};
  346. element.picker = new Picker(element, _options);
  347. }
  348. });
  349. return this[0] ? this[0].picker : null;
  350. };
  351. //自动初始化
  352. $.ready(function() {
  353. $('.mui-picker').picker();
  354. });
  355. }
  356. })(window.mui || window, window, document, undefined);
  357. //end