mobileSelect.js 35 KB

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