mobileSelect.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. app.directive('placeArea', function() {
  2. return {
  3. restrict: 'E',
  4. // scope: {
  5. // MobileSelect: '=',
  6. // placelable: '='
  7. // },
  8. template:
  9. // '<div class="contain">' +
  10. // '<div class="fixWidth">' +
  11. // '<div class="input-group col-xs-12 demo">' +
  12. // // '<input id="trigger" ng-model=data disalbed>' +
  13. // '<i class="icon ion-ios-arrow-right font-color-gray" style="font-size: 2rem;"></i>' +
  14. // '<a class="" id="trigger">' +
  15. // ' </a>' +
  16. // '<span class="">{{searchstate.label}}</span>' +
  17. // '</div>' +
  18. // '</div>' +
  19. // '</div>',
  20. '<div class="contain" style="background:white" class="tabs-top fz-16">' +
  21. '<div class="fixWidth">' +
  22. '<div class="item item-icon-right bordo height_3" id="trigger">' +
  23. '<a class="" >' +
  24. '<i class="icon ion-ios-arrow-right font-color-gray" style="font-size: 2rem;"></i>' +
  25. '</a>' +
  26. // '<span class="">{{searchstate.label}}</span>' +
  27. '</div>' +
  28. '</div>' +
  29. '</div>',
  30. // controller: function($scope, element, attrs) {
  31. controller: ['$scope', '$element', '$attrs', function($scope, $element, $attrs) {
  32. function getClass(dom, string) {
  33. return dom.getElementsByClassName(string);
  34. }
  35. //构造器
  36. var MobileSelect = function(config) {
  37. MobileSelect.prototype.mobileSelect;
  38. MobileSelect.prototype.wheelsData = config.wheels;
  39. MobileSelect.prototype.jsonType = false;
  40. MobileSelect.prototype.cascadeJsonData = [];
  41. MobileSelect.prototype.displayJson = [];
  42. MobileSelect.prototype.cascade = false;
  43. MobileSelect.prototype.startY;
  44. MobileSelect.prototype.moveEndY;
  45. MobileSelect.prototype.moveY;
  46. MobileSelect.prototype.oldMoveY;
  47. MobileSelect.prototype.offset = 0;
  48. MobileSelect.prototype.offsetSum = 0;
  49. MobileSelect.prototype.oversizeBorder;
  50. MobileSelect.prototype.curDistance = [];
  51. MobileSelect.prototype.clickStatus = false;
  52. MobileSelect.prototype.isPC = true;
  53. MobileSelect.prototype.init(config);
  54. }
  55. MobileSelect.prototype = {
  56. constructor: MobileSelect,
  57. init: function(config) {
  58. var _this = this;
  59. _this.keyMap = config.keyMap ? config.keyMap : { id: 'id', value: 'value', childs: 'childs' };
  60. _this.checkDataType();
  61. _this.renderWheels(_this.wheelsData, config.cancelBtnText, config.ensureBtnText);
  62. _this.trigger = document.querySelector(config.trigger);
  63. if (!_this.trigger) {
  64. console.error('mobileSelect has been successfully installed, but no trigger found on your page.');
  65. return false;
  66. }
  67. _this.wheel = getClass(_this.mobileSelect, 'wheel');
  68. _this.slider = getClass(_this.mobileSelect, 'selectContainer');
  69. _this.wheels = _this.mobileSelect.querySelector('.wheels');
  70. _this.liHeight = _this.mobileSelect.querySelector('li').offsetHeight;
  71. _this.ensureBtn = _this.mobileSelect.querySelector('.ensure');
  72. _this.cancelBtn = _this.mobileSelect.querySelector('.cancel');
  73. _this.grayLayer = _this.mobileSelect.querySelector('.grayLayer');
  74. _this.popUp = _this.mobileSelect.querySelector('.content');
  75. _this.callback = config.callback ? config.callback : function() {};
  76. _this.transitionEnd = config.transitionEnd ? config.transitionEnd : function() {};
  77. _this.initPosition = config.position ? config.position : [];
  78. _this.titleText = config.title ? config.title : '';
  79. _this.connector = config.connector ? config.connector : ' ';
  80. _this.trigger.style.cursor = 'pointer';
  81. _this.setStyle(config);
  82. _this.setTitle(_this.titleText);
  83. _this.checkIsPC();
  84. _this.checkCascade();
  85. if (_this.cascade) {
  86. _this.initCascade();
  87. }
  88. //定位 初始位置
  89. if (_this.initPosition.length < _this.slider.length) {
  90. var diff = _this.slider.length - _this.initPosition.length;
  91. for (var i = 0; i < diff; i++) {
  92. _this.initPosition.push(0);
  93. }
  94. }
  95. _this.setCurDistance(_this.initPosition);
  96. _this.addListenerAll();
  97. //按钮监听
  98. _this.cancelBtn.addEventListener('click', function() {
  99. _this.mobileSelect.classList.remove('mobileSelect-show');
  100. });
  101. _this.ensureBtn.addEventListener('click', function(event) {
  102. _this.mobileSelect.classList.remove('mobileSelect-show');
  103. var tempValue = '';
  104. for (var i = 0; i < _this.wheel.length; i++) {
  105. i == _this.wheel.length - 1 ? tempValue += _this.getInnerHtml(i) : tempValue += _this.getInnerHtml(i) + _this.connector;
  106. }
  107. _this.trigger.innerHTML = tempValue;
  108. _this.callback(_this.getIndexArr(), _this.getValue());
  109. $scope.getselect(_this.getValue());
  110. });
  111. _this.trigger.addEventListener('click', function() {
  112. _this.mobileSelect.classList.add('mobileSelect-show');
  113. });
  114. _this.grayLayer.addEventListener('click', function() {
  115. _this.mobileSelect.classList.remove('mobileSelect-show');
  116. });
  117. _this.popUp.addEventListener('click', function() {
  118. event.stopPropagation();
  119. });
  120. _this.fixRowStyle(); //修正列数
  121. },
  122. setTitle: function(string) {
  123. var _this = this;
  124. _this.titleText = string;
  125. _this.mobileSelect.querySelector('.title').innerHTML = _this.titleText;
  126. },
  127. setStyle: function(config) {
  128. var _this = this;
  129. if (config.ensureBtnColor) {
  130. _this.ensureBtn.style.color = config.ensureBtnColor;
  131. }
  132. if (config.cancelBtnColor) {
  133. _this.cancelBtn.style.color = config.cancelBtnColor;
  134. }
  135. if (config.titleColor) {
  136. _this.title = _this.mobileSelect.querySelector('.title');
  137. _this.title.style.color = config.titleColor;
  138. }
  139. if (config.textColor) {
  140. _this.panel = _this.mobileSelect.querySelector('.panel');
  141. _this.panel.style.color = config.textColor;
  142. }
  143. if (config.titleBgColor) {
  144. _this.btnBar = _this.mobileSelect.querySelector('.btnBar');
  145. _this.btnBar.style.backgroundColor = config.titleBgColor;
  146. }
  147. if (config.bgColor) {
  148. _this.panel = _this.mobileSelect.querySelector('.panel');
  149. _this.shadowMask = _this.mobileSelect.querySelector('.shadowMask');
  150. _this.panel.style.backgroundColor = config.bgColor;
  151. _this.shadowMask.style.background = 'linear-gradient(to bottom, ' + config.bgColor + ', rgba(255, 255, 255, 0), ' + config.bgColor + ')';
  152. }
  153. },
  154. checkIsPC: function() {
  155. var _this = this;
  156. var sUserAgent = navigator.userAgent.toLowerCase();
  157. var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
  158. var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
  159. var bIsMidp = sUserAgent.match(/midp/i) == "midp";
  160. var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
  161. var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
  162. var bIsAndroid = sUserAgent.match(/android/i) == "android";
  163. var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
  164. var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
  165. if ((bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM)) {
  166. _this.isPC = false;
  167. }
  168. },
  169. show: function() {
  170. this.mobileSelect.classList.add('mobileSelect-show');
  171. },
  172. renderWheels: function(wheelsData, cancelBtnText, ensureBtnText) {
  173. var _this = this;
  174. var cancelText = cancelBtnText ? cancelBtnText : '取消';
  175. var ensureText = ensureBtnText ? ensureBtnText : '确认';
  176. _this.mobileSelect = document.createElement("div");
  177. _this.mobileSelect.className = "mobileSelect";
  178. _this.mobileSelect.innerHTML =
  179. '<div class="grayLayer"></div>' +
  180. '<div class="content">' +
  181. '<div class="btnBar">' +
  182. '<div class="fixWidth">' +
  183. '<div class="cancel">' + cancelText + '</div>' +
  184. '<div class="title"></div>' +
  185. '<div class="ensure" ng-click="upselect()">' + ensureText + '</div>' +
  186. '</div>' +
  187. '</div>' +
  188. '<div class="panel">' +
  189. '<div class="fixWidth">' +
  190. '<div class="wheels">' +
  191. '</div>' +
  192. '<div class="selectLine"></div>' +
  193. '<div class="shadowMask"></div>' +
  194. '</div>' +
  195. '</div>' +
  196. '</div>';
  197. document.body.appendChild(_this.mobileSelect);
  198. //根据数据长度来渲染
  199. var tempHTML = '';
  200. for (var i = 0; i < wheelsData.length; i++) {
  201. //列
  202. tempHTML += '<div class="wheel"><ul class="selectContainer">';
  203. if (_this.jsonType) {
  204. for (var j = 0; j < wheelsData[i].data.length; j++) {
  205. //行
  206. tempHTML += '<li data-id="' + wheelsData[i].data[j][_this.keyMap.id] + '">' + wheelsData[i].data[j][_this.keyMap.value] + '</li>';
  207. }
  208. } else {
  209. for (var j = 0; j < wheelsData[i].data.length; j++) {
  210. //行
  211. tempHTML += '<li>' + wheelsData[i].data[j] + '</li>';
  212. }
  213. }
  214. tempHTML += '</ul></div>';
  215. }
  216. _this.mobileSelect.querySelector('.wheels').innerHTML = tempHTML;
  217. },
  218. addListenerAll: function() {
  219. var _this = this;
  220. for (var i = 0; i < _this.slider.length; i++) {
  221. //手势监听
  222. (function(i) {
  223. _this.addListenerWheel(_this.wheel[i], i);
  224. _this.addListenerLi(i);
  225. })(i);
  226. }
  227. },
  228. addListenerWheel: function(theWheel, index) {
  229. var _this = this;
  230. theWheel.addEventListener('touchstart', function() {
  231. _this.touch(event, this.firstChild, index);
  232. }, false);
  233. theWheel.addEventListener('touchend', function() {
  234. _this.touch(event, this.firstChild, index);
  235. }, false);
  236. theWheel.addEventListener('touchmove', function() {
  237. _this.touch(event, this.firstChild, index);
  238. }, false);
  239. if (_this.isPC) {
  240. //如果是PC端则再增加拖拽监听 方便调试
  241. theWheel.addEventListener('mousedown', function() {
  242. _this.dragClick(event, this.firstChild, index);
  243. }, false);
  244. theWheel.addEventListener('mousemove', function() {
  245. _this.dragClick(event, this.firstChild, index);
  246. }, false);
  247. theWheel.addEventListener('mouseup', function() {
  248. _this.dragClick(event, this.firstChild, index);
  249. }, true);
  250. }
  251. },
  252. addListenerLi: function(sliderIndex) {
  253. var _this = this;
  254. var curWheelLi = _this.slider[sliderIndex].getElementsByTagName('li');
  255. for (var j = 0; j < curWheelLi.length; j++) {
  256. (function(j) {
  257. curWheelLi[j].addEventListener('click', function() {
  258. _this.singleClick(this, j, sliderIndex);
  259. }, false);
  260. })(j);
  261. }
  262. },
  263. checkDataType: function() {
  264. var _this = this;
  265. if (typeof(_this.wheelsData[0].data[0]) == 'object') {
  266. _this.jsonType = true;
  267. }
  268. },
  269. checkCascade: function() {
  270. var _this = this;
  271. if (_this.jsonType) {
  272. var node = _this.wheelsData[0].data;
  273. for (var i = 0; i < node.length; i++) {
  274. if (_this.keyMap.childs in node[i] && node[i][_this.keyMap.childs].length > 0) {
  275. _this.cascade = true;
  276. _this.cascadeJsonData = _this.wheelsData[0].data;
  277. break;
  278. }
  279. }
  280. } else {
  281. _this.cascade = false;
  282. }
  283. },
  284. generateArrData: function(targetArr) {
  285. var tempArr = [];
  286. var keyMap_id = this.keyMap.id;
  287. var keyMap_value = this.keyMap.value;
  288. for (var i = 0; i < targetArr.length; i++) {
  289. var tempObj = {};
  290. tempObj[keyMap_id] = targetArr[i][this.keyMap.id];
  291. tempObj[keyMap_value] = targetArr[i][this.keyMap.value];
  292. tempArr.push(tempObj);
  293. }
  294. return tempArr;
  295. },
  296. initCascade: function() {
  297. var _this = this;
  298. _this.displayJson.push(_this.generateArrData(_this.cascadeJsonData));
  299. if (_this.initPosition.length > 0) {
  300. _this.initDeepCount = 0;
  301. _this.initCheckArrDeep(_this.cascadeJsonData[_this.initPosition[0]]);
  302. } else {
  303. _this.checkArrDeep(_this.cascadeJsonData[0]);
  304. }
  305. _this.reRenderWheels();
  306. },
  307. initCheckArrDeep: function(parent) {
  308. var _this = this;
  309. if (parent) {
  310. if (_this.keyMap.childs in parent && parent[_this.keyMap.childs].length > 0) {
  311. _this.displayJson.push(_this.generateArrData(parent[_this.keyMap.childs]));
  312. _this.initDeepCount++;
  313. var nextNode = parent[_this.keyMap.childs][_this.initPosition[_this.initDeepCount]];
  314. if (nextNode) {
  315. _this.initCheckArrDeep(nextNode);
  316. } else {
  317. _this.checkArrDeep(parent[_this.keyMap.childs][0]);
  318. }
  319. }
  320. }
  321. },
  322. checkArrDeep: function(parent) {
  323. //检测子节点深度 修改 displayJson
  324. var _this = this;
  325. if (parent) {
  326. if (_this.keyMap.childs in parent && parent[_this.keyMap.childs].length > 0) {
  327. _this.displayJson.push(_this.generateArrData(parent[_this.keyMap.childs])); //生成子节点数组
  328. _this.checkArrDeep(parent[_this.keyMap.childs][0]); //检测下一个子节点
  329. }
  330. }
  331. },
  332. checkRange: function(index, posIndexArr) {
  333. var _this = this;
  334. // if (_this.getValue()[0]) {
  335. // $scope.getplacedata(_this.getValue()[0].id);
  336. // }
  337. var deleteNum = _this.displayJson.length - 1 - index;
  338. for (var i = 0; i < deleteNum; i++) {
  339. _this.displayJson.pop(); //修改 displayJson
  340. }
  341. var resultNode;
  342. for (var i = 0; i <= index; i++) {
  343. if (i == 0)
  344. resultNode = _this.cascadeJsonData[posIndexArr[0]];
  345. else {
  346. resultNode = resultNode[_this.keyMap.childs][posIndexArr[i]];
  347. }
  348. }
  349. _this.checkArrDeep(resultNode);
  350. //console.log(_this.displayJson);
  351. _this.reRenderWheels();
  352. _this.fixRowStyle();
  353. _this.setCurDistance(_this.resetPostion(index, posIndexArr));
  354. },
  355. resetPostion: function(index, posIndexArr) {
  356. var _this = this;
  357. var tempPosArr = posIndexArr;
  358. var tempCount;
  359. if (_this.slider.length > posIndexArr.length) {
  360. tempCount = _this.slider.length - posIndexArr.length;
  361. for (var i = 0; i < tempCount; i++) {
  362. tempPosArr.push(0);
  363. }
  364. } else if (_this.slider.length < posIndexArr.length) {
  365. tempCount = posIndexArr.length - _this.slider.length;
  366. for (var i = 0; i < tempCount; i++) {
  367. tempPosArr.pop();
  368. }
  369. }
  370. for (var i = index + 1; i < tempPosArr.length; i++) {
  371. tempPosArr[i] = 0;
  372. }
  373. return tempPosArr;
  374. },
  375. reRenderWheels: function() {
  376. var _this = this;
  377. // $scope.getplacedata(_this.getValue()[0].id);
  378. //删除多余的wheel
  379. if (_this.wheel.length > _this.displayJson.length) {
  380. var count = _this.wheel.length - _this.displayJson.length;
  381. for (var i = 0; i < count; i++) {
  382. _this.wheels.removeChild(_this.wheel[_this.wheel.length - 1]);
  383. }
  384. }
  385. for (var i = 0; i < _this.displayJson.length; i++) {
  386. //列
  387. (function(i) {
  388. var tempHTML = '';
  389. if (_this.wheel[i]) {
  390. //console.log('插入Li');
  391. for (var j = 0; j < _this.displayJson[i].length; j++) {
  392. //行
  393. tempHTML += '<li data-id="' + _this.displayJson[i][j][_this.keyMap.id] + '">' + _this.displayJson[i][j][_this.keyMap.value] + '</li>';
  394. }
  395. _this.slider[i].innerHTML = tempHTML;
  396. } else {
  397. var tempWheel = document.createElement("div");
  398. tempWheel.className = "wheel";
  399. tempHTML = '<ul class="selectContainer">';
  400. for (var j = 0; j < _this.displayJson[i].length; j++) {
  401. //行
  402. tempHTML += '<li data-id="' + _this.displayJson[i][j][_this.keyMap.id] + '">' + _this.displayJson[i][j][_this.keyMap.value] + '</li>';
  403. }
  404. tempHTML += '</ul>';
  405. tempWheel.innerHTML = tempHTML;
  406. _this.addListenerWheel(tempWheel, i);
  407. _this.wheels.appendChild(tempWheel);
  408. }
  409. _this.addListenerLi(i);
  410. })(i);
  411. }
  412. },
  413. updateWheels: function(data) {
  414. var _this = this;
  415. if (_this.cascade) {
  416. _this.cascadeJsonData = data;
  417. _this.displayJson = [];
  418. _this.initCascade();
  419. if (_this.initPosition.length < _this.slider.length) {
  420. var diff = _this.slider.length - _this.initPosition.length;
  421. for (var i = 0; i < diff; i++) {
  422. _this.initPosition.push(0);
  423. }
  424. }
  425. _this.setCurDistance(_this.initPosition);
  426. _this.fixRowStyle();
  427. }
  428. },
  429. updateWheel: function(sliderIndex, data) {
  430. var _this = this;
  431. var tempHTML = '';
  432. if (_this.cascade) {
  433. console.error('级联格式不支持updateWheel(),请使用updateWheels()更新整个数据源');
  434. return false;
  435. } else if (_this.jsonType) {
  436. for (var j = 0; j < data.length; j++) {
  437. tempHTML += '<li data-id="' + data[j][_this.keyMap.id] + '">' + data[j][_this.keyMap.value] + '</li>';
  438. }
  439. _this.wheelsData[sliderIndex] = { data: data };
  440. } else {
  441. for (var j = 0; j < data.length; j++) {
  442. tempHTML += '<li>' + data[j] + '</li>';
  443. }
  444. _this.wheelsData[sliderIndex] = data;
  445. }
  446. _this.slider[sliderIndex].innerHTML = tempHTML;
  447. _this.addListenerLi(sliderIndex);
  448. },
  449. fixRowStyle: function() {
  450. var _this = this;
  451. var width = (100 / _this.wheel.length).toFixed(2);
  452. for (var i = 0; i < _this.wheel.length; i++) {
  453. _this.wheel[i].style.width = width + '%';
  454. }
  455. },
  456. getIndex: function(distance) {
  457. return Math.round((2 * this.liHeight - distance) / this.liHeight);
  458. },
  459. getIndexArr: function() {
  460. var _this = this;
  461. var temp = [];
  462. for (var i = 0; i < _this.curDistance.length; i++) {
  463. temp.push(_this.getIndex(_this.curDistance[i]));
  464. }
  465. return temp;
  466. },
  467. getValue: function() {
  468. var _this = this;
  469. var temp = [];
  470. var positionArr = _this.getIndexArr();
  471. if (_this.cascade) {
  472. for (var i = 0; i < _this.wheel.length; i++) {
  473. temp.push(_this.displayJson[i][positionArr[i]]);
  474. }
  475. } else if (_this.jsonType) {
  476. for (var i = 0; i < _this.curDistance.length; i++) {
  477. temp.push(_this.wheelsData[i].data[_this.getIndex(_this.curDistance[i])]);
  478. }
  479. } else {
  480. for (var i = 0; i < _this.curDistance.length; i++) {
  481. temp.push(_this.getInnerHtml(i));
  482. }
  483. }
  484. return temp;
  485. },
  486. calcDistance: function(index) {
  487. return 2 * this.liHeight - index * this.liHeight;
  488. },
  489. setCurDistance: function(indexArr) {
  490. var _this = this;
  491. var temp = [];
  492. for (var i = 0; i < _this.slider.length; i++) {
  493. temp.push(_this.calcDistance(indexArr[i]));
  494. _this.movePosition(_this.slider[i], temp[i]);
  495. }
  496. _this.curDistance = temp;
  497. },
  498. fixPosition: function(distance) {
  499. return -(this.getIndex(distance) - 2) * this.liHeight;
  500. },
  501. movePosition: function(theSlider, distance) {
  502. theSlider.style.webkitTransform = 'translate3d(0,' + distance + 'px, 0)';
  503. theSlider.style.transform = 'translate3d(0,' + distance + 'px, 0)';
  504. },
  505. locatePostion: function(index, posIndex) {
  506. this.curDistance[index] = this.calcDistance(posIndex);
  507. this.movePosition(this.slider[index], this.curDistance[index]);
  508. },
  509. updateCurDistance: function(theSlider, index) {
  510. this.curDistance[index] = parseInt(theSlider.style.transform.split(',')[1]);
  511. },
  512. getDistance: function(theSlider) {
  513. return parseInt(theSlider.style.transform.split(',')[1]);
  514. },
  515. getInnerHtml: function(sliderIndex) {
  516. var _this = this;
  517. var index = _this.getIndex(_this.curDistance[sliderIndex]);
  518. return _this.slider[sliderIndex].getElementsByTagName('li')[index].innerHTML;
  519. },
  520. touch: function(event, theSlider, index) {
  521. var _this = this;
  522. event = event || window.event;
  523. switch (event.type) {
  524. case "touchstart":
  525. _this.startY = event.touches[0].clientY;
  526. _this.oldMoveY = _this.startY;
  527. break;
  528. case "touchend":
  529. _this.moveEndY = event.changedTouches[0].clientY;
  530. _this.offsetSum = _this.moveEndY - _this.startY;
  531. //修正位置
  532. _this.updateCurDistance(theSlider, index);
  533. _this.curDistance[index] = _this.fixPosition(_this.curDistance[index]);
  534. _this.movePosition(theSlider, _this.curDistance[index]);
  535. _this.oversizeBorder = -(theSlider.getElementsByTagName('li').length - 3) * _this.liHeight;
  536. //反弹
  537. if (_this.curDistance[index] + _this.offsetSum > 2 * _this.liHeight) {
  538. _this.curDistance[index] = 2 * _this.liHeight;
  539. setTimeout(function() {
  540. _this.movePosition(theSlider, _this.curDistance[index]);
  541. }, 100);
  542. } else if (_this.curDistance[index] + _this.offsetSum < _this.oversizeBorder) {
  543. _this.curDistance[index] = _this.oversizeBorder;
  544. setTimeout(function() {
  545. _this.movePosition(theSlider, _this.curDistance[index]);
  546. }, 100);
  547. }
  548. _this.transitionEnd(_this.getIndexArr(), _this.getValue());
  549. if (_this.cascade) {
  550. var tempPosArr = _this.getIndexArr();
  551. tempPosArr[index] = _this.getIndex(_this.curDistance[index]);
  552. _this.checkRange(index, tempPosArr);
  553. }
  554. break;
  555. case "touchmove":
  556. event.preventDefault();
  557. _this.moveY = event.touches[0].clientY;
  558. _this.offset = _this.moveY - _this.oldMoveY;
  559. _this.updateCurDistance(theSlider, index);
  560. _this.curDistance[index] = _this.curDistance[index] + _this.offset;
  561. _this.movePosition(theSlider, _this.curDistance[index]);
  562. _this.oldMoveY = _this.moveY;
  563. break;
  564. }
  565. },
  566. dragClick: function(event, theSlider, index) {
  567. var _this = this;
  568. event = event || window.event;
  569. switch (event.type) {
  570. case "mousedown":
  571. _this.startY = event.clientY;
  572. _this.oldMoveY = _this.startY;
  573. _this.clickStatus = true;
  574. break;
  575. case "mouseup":
  576. _this.moveEndY = event.clientY;
  577. _this.offsetSum = _this.moveEndY - _this.startY;
  578. //修正位置
  579. _this.updateCurDistance(theSlider, index);
  580. _this.curDistance[index] = _this.fixPosition(_this.curDistance[index]);
  581. _this.movePosition(theSlider, _this.curDistance[index]);
  582. _this.oversizeBorder = -(theSlider.getElementsByTagName('li').length - 3) * _this.liHeight;
  583. //反弹
  584. if (_this.curDistance[index] + _this.offsetSum > 2 * _this.liHeight) {
  585. _this.curDistance[index] = 2 * _this.liHeight;
  586. setTimeout(function() {
  587. _this.movePosition(theSlider, _this.curDistance[index]);
  588. }, 100);
  589. } else if (_this.curDistance[index] + _this.offsetSum < _this.oversizeBorder) {
  590. _this.curDistance[index] = _this.oversizeBorder;
  591. setTimeout(function() {
  592. _this.movePosition(theSlider, _this.curDistance[index]);
  593. }, 100);
  594. }
  595. _this.clickStatus = false;
  596. _this.transitionEnd(_this.getIndexArr(), _this.getValue());
  597. if (_this.cascade) {
  598. var tempPosArr = _this.getIndexArr();
  599. tempPosArr[index] = _this.getIndex(_this.curDistance[index]);
  600. _this.checkRange(index, tempPosArr);
  601. }
  602. break;
  603. case "mousemove":
  604. event.preventDefault();
  605. if (_this.clickStatus) {
  606. _this.moveY = event.clientY;
  607. _this.offset = _this.moveY - _this.oldMoveY;
  608. _this.updateCurDistance(theSlider, index);
  609. _this.curDistance[index] = _this.curDistance[index] + _this.offset;
  610. _this.movePosition(theSlider, _this.curDistance[index]);
  611. _this.oldMoveY = _this.moveY;
  612. }
  613. break;
  614. }
  615. },
  616. singleClick: function(theLi, index, sliderIndex) {
  617. var _this = this;
  618. if (_this.cascade) {
  619. var tempPosArr = _this.getIndexArr();
  620. tempPosArr[sliderIndex] = index;
  621. _this.checkRange(sliderIndex, tempPosArr);
  622. } else {
  623. _this.curDistance[sliderIndex] = (2 - index) * _this.liHeight;
  624. _this.movePosition(theLi.parentNode, _this.curDistance[sliderIndex]);
  625. }
  626. }
  627. };
  628. $scope.getcum = function(sliderIndex, data) {
  629. MobileSelect.prototype.updateWheel(sliderIndex, data)
  630. }
  631. $scope.gerdirect = function(MobileSelectdata) {
  632. if (typeof exports == "object") {
  633. module.exports = MobileSelect;
  634. } else if (typeof define == "function" && define.amd) {
  635. define([], function() {
  636. // $scope.getareadata();
  637. // $scope.getplacedata(1);
  638. return MobileSelect(MobileSelectdata);
  639. })
  640. } else {
  641. // $scope.getareadata();
  642. // $scope.getplacedata(1);
  643. window.MobileSelect = MobileSelect(MobileSelectdata);
  644. }
  645. }
  646. }]
  647. }
  648. })