资讯专栏INFORMATION COLUMN

jquery移动端省市县(区)三级联动插件

Eric / 529人阅读

摘要:写省市区三级联动插件的关键是在于你如何编写自己的省市县区文件,你要是把数据格式写对了,可以很轻松的写一省市区插件出来。

最近刚做完一个版本的项目,闲暇时间,就索性把项目中需要用到的插件都自己写了一个,毕竟自己动手丰衣足食才是最重要,自己写的,可以应对各种项目需求,今天又把手机端的省市区三级联动选择功能编写了一个插件出来,代码很简单,样式也是应用的跟日期时间选择插件的一样。写省市区三级联动插件的关键是在于你如何编写自己的“省市县(区)”json文件,你要是把数据格式写对了,可以很轻松的写一省市区插件出来。在项目里,我把json文件里的数据都压缩了一遍直接放到了插件的.js文件里,自己觉得这样更好,减少插件的关联文件,就一个.js和一个.css文件,一目了然。

下面我把代码放出来,需要的小伙伴们可以粘贴复制哦。(插件没有bug,可以放心使用,喜欢的给个赞哦(o^*^o)!!!)

使用方法

</>复制代码

  1. HTML:
  2. //存放选择的省市区文本值
  3. JS:
  4. $(".city").pickArea({
  5. "speed":2
  6. });
jquery.pickArea.js

</>复制代码

  1. /*
  2. * pluginName:pickCity,
  3. * author:yangyang2010cs@163.com
  4. * Individual contact method:986372959(It"s my QQ)
  5. * date:2017/09/07 18:45:00
  6. * */
  7. ;(function($,window,document,undefined){
  8. "use strict"; //严格模式,提高效率
  9. var pluginName = "pickArea", //定义插件的名字
  10. defaults = {},//定义一个默认的参数
  11. liH,//每一个li的高度
  12. $list,//滑动列表
  13. globalThis_launchHtml,
  14. data,
  15. pluginThis;//指代指向plugin的this
  16. var global = {
  17. options:""
  18. };
  19. function Plugin(options){ //创建构造函
  20. //this -- Plugin对象
  21. pluginThis = this;
  22. this.options = options;
  23. this.init(); //初始化
  24. }
  25. Plugin.prototype = {
  26. init:function(){
  27. global.options = this.options;
  28. data = dataJson.data;//获取数据 data[i].code省code data[i].name省name
  29. console.log(data)
  30. this.render(); //渲染
  31. },
  32. render:function(){
  33. //this -- Plugin对象
  34. var str = "
    "+
  35. "
    "+
  36. "
    "+
  37. "取消"+
  38. ""+
  39. "
    设置地区
    "+
  40. "
    "+
  41. "确定"+
  42. ""+
  43. ""+
  44. "
    "+
  45. "
    "+
  46. "
    "+
  47. "
      "+
    • "
    "+
  48. "
    "+
  49. "
    "+
  50. ""+
  51. "
    "+
  52. "
    "+
  53. "
      "+
    • "
    "+
  54. "
    "+
  55. "
    "+
  56. ""+
  57. "
    "+
  58. "
    "+
  59. "
      "+
    • "
    "+
  60. "
    "+
  61. "
    "+
  62. ""+
  63. ""+
  64. ""+
  65. "
    ";
  66. $("body").append(str);
  67. $list = $(".list");
  68. $(".pick-cancel,.pick-layer").on("click",function(){
  69. $(".touches,.pick-layer").remove();
  70. $("body").unbind("touchmove"); //恢复了body的拖动事件
  71. });
  72. $(".pick-sure").on("click",function(){
  73. var val = "";
  74. $(".pick-active").each(function(){
  75. val += $(this).text()+"-";
  76. });
  77. $(globalThis_launchHtml).html(val.substring(0,val.length-1));
  78. $(".touches,.pick-layer").remove();
  79. $("body").unbind("touchmove"); //恢复了body的拖动事件
  80. });
  81. $("body").on("touchmove",function (e){
  82. e.preventDefault();
  83. });
  84. liH = 44;//li的高度
  85. this.createProvince();
  86. this.handleEvent(); //绑定事件
  87. return this;
  88. },
  89. createProvince:function(){
  90. for(var i=0;i" +data[i].name+""); //data[i].code省code data[i].name省name
  91. }
  92. $list.eq(0).find("li").eq(0).addClass("pick-active");//一开始默认第三行选中
  93. $list.eq(0).css("top",2*liH);
  94. this.createCity();
  95. },
  96. createCity:function(){
  97. $list.eq(1).html("");
  98. var selectProCode = $list.eq(0).find("li.pick-active").attr("data-provinceCode");
  99. var getCityArr = "";
  100. for(var i=0;i" +getCityArr[j].name+"");
  101. }
  102. $list.eq(1).find("li").eq(0).addClass("pick-active");//一开始默认第三行选中
  103. $list.eq(1).css("top",2*liH);
  104. this.createCounty();
  105. },
  106. createCounty:function(){
  107. $list.eq(2).html("");
  108. var selectProIndex = $list.eq(0).find("li.pick-active").attr("data-provinceIndex");
  109. var selectCityIndex = $list.eq(1).find("li.pick-active").attr("data-cityIndex");
  110. var getCountyArr = data[selectProIndex].cities[selectCityIndex].district;
  111. for(var j=0;j" +getCountyArr[j].name+"");
  112. }
  113. $list.eq(2).find("li").eq(0).addClass("pick-active");//一开始默认第三行选中
  114. $list.eq(2).css("top",2*liH);
  115. },
  116. handleEvent:function(){ //函数绑定
  117. //this -- Plugin对象
  118. $list.each(function(){
  119. var startY = null,//开始的pageY
  120. endY = null,//结束的pageY
  121. distY = null,//endY - startY
  122. cTop = null,//currentTop
  123. _top = null,//ul.list的top值
  124. timeS = null,//滚动的开始时间
  125. distT = null,//每一次滚动的时间差
  126. speed = null;//速度
  127. var SE = null;
  128. var ME = null;
  129. function startCallBack(e){
  130. //这里的this指向当前滑动的$list
  131. if(e.originalEvent.touches){
  132. SE=e.originalEvent.targetTouches[0];
  133. console.log(SE)
  134. }
  135. startY = SE.pageY;
  136. cTop = $(this).position().top;
  137. timeS = new Date();
  138. }
  139. function moveCallBack(e){
  140. //这里的this指向当前滑动的$list
  141. if(e.originalEvent.touches){
  142. ME=e.originalEvent.targetTouches[0];
  143. //console.log(ME)
  144. }
  145. var scrollSpeed = pluginThis.options.speed || 2;
  146. endY = ME.pageY;
  147. distY = scrollSpeed*(endY - startY);
  148. //console.log(distY);//往下滑动是正直,往上是负值
  149. if($(this).find("li").length==2){
  150. if(cTop+distY>88){//从顶部往下滑动
  151. _top = 88;
  152. } else if(cTop+distY<$(this).parent().height()-$(this).height()-44){//从底部往上滑动
  153. _top = $(this).parent().height() - $(this).height()-44;
  154. } else {//中间地方滑动
  155. _top = cTop+distY;
  156. }
  157. } else if($(this).find("li").length==1){
  158. return;
  159. } else {
  160. if(cTop+distY>88){//从顶部往下滑动
  161. _top = 88;
  162. } else if(cTop+distY<$(this).parent().height()-$(this).height()-88){//从底部往上滑动
  163. _top = $(this).parent().height() - $(this).height()-88;
  164. } else {//中间地方滑动
  165. _top = cTop+distY;
  166. }
  167. }
  168. _top = _top - _top % liH;//取整
  169. $(this).css("top",_top);
  170. if(_top==44){
  171. $(this).find("li").eq(1).addClass("pick-active").siblings().removeClass("pick-active");
  172. } else if(_top==88){
  173. $(this).find("li").eq(0).addClass("pick-active").siblings().removeClass("pick-active");
  174. } else {
  175. $(this).find("li").eq(Math.abs(_top/liH)+2).addClass("pick-active").siblings().removeClass("pick-active");
  176. }
  177. }
  178. function endCallBack(e){
  179. //这里的this指向当前滑动的$list
  180. var $this = $(this);
  181. console.log()
  182. var dir = distY < 0 ? 1 : -1;//方向 上移为1,下移为-1
  183. distT = new Date() - timeS;
  184. speed = Math.abs(distY / distT);//单位px/ms
  185. if($(this).find("li").length==1){
  186. return;
  187. } else {
  188. if(speed>0.6) {
  189. /*alert(1)*/
  190. if (dir == 1 && Math.abs(_top / liH) + 3 == $(this).find("li").length) { //手指向上滑动
  191. if($this.attr("class")==$list.eq(0).attr("class")){
  192. pluginThis.createCity();
  193. } else if($this.attr("class")==$list.eq(1).attr("class")){
  194. pluginThis.createCounty();
  195. }
  196. return;//到底了,不能滑了
  197. } else if(dir==-1 && _top==88){ //手指向下滑动
  198. if($this.attr("class")==$list.eq(0).attr("class")){
  199. pluginThis.createCity();
  200. } else if($this.attr("class")==$list.eq(1).attr("class")){
  201. pluginThis.createCounty();
  202. }
  203. return;//到顶了,不能滑了
  204. }
  205. }
  206. setTimeout(function(){
  207. $this.css("top",_top);
  208. if(_top==44){
  209. $(this).find("li").eq(1).addClass("pick-active").siblings().removeClass("pick-active");
  210. } else if(_top==88){
  211. $(this).find("li").eq(0).addClass("pick-active").siblings().removeClass("pick-active");
  212. } else {
  213. $(this).find("li").eq(Math.abs(_top/liH)+2).addClass("pick-active").siblings().removeClass("pick-active");
  214. }
  215. if($this.attr("class")==$list.eq(0).attr("class")){
  216. pluginThis.createCity();
  217. } else if($this.attr("class")==$list.eq(1).attr("class")){
  218. pluginThis.createCounty();
  219. }
  220. },50);
  221. }
  222. }
  223. $(this).off("touchstart").on("touchstart",startCallBack); //下滑开始 这里的this指向plugin对象
  224. $(this).off("touchmove").on("touchmove",moveCallBack); //滑动的时候 这里的this指向plugin对象
  225. $(this).off("touchend").on("touchend",endCallBack); //滑动完了 这里的this指向plugin对象
  226. })
  227. }
  228. };
  229. $.fn[pluginName] = function(options){
  230. $(this).click(function(){
  231. globalThis_launchHtml = this;
  232. new Plugin(options);
  233. });
  234. return this;//返回调用插件的对象,以便支持链式调用
  235. }
  236. })(jQuery,window,document);
jquery.pickArea.css

</>复制代码

  1. .pick-container ul{
  2. margin:0;
  3. padding:0;
  4. }
  5. .pick-container ul,.pick-container li{
  6. list-style: none;
  7. }
  8. .pick-container a{
  9. text-decoration: none;
  10. }
  11. /*materialize*/
  12. .pick-container *{-webkit-tap-highlight-color:transparent;}
  13. .pick-container {
  14. position:fixed;
  15. z-index:99999999;
  16. left:0;
  17. bottom:0;
  18. width:100vw;
  19. background:#fff;
  20. margin: 0 auto;
  21. max-width: 1280px;
  22. }
  23. *, *:before, *:after {
  24. -webkit-box-sizing: inherit;
  25. box-sizing: inherit;
  26. }
  27. .pick-m0 {
  28. margin: 0;
  29. }
  30. .row:after {
  31. content: "";
  32. display: table;
  33. clear: both;
  34. }
  35. .row .col {
  36. float: left;
  37. -webkit-box-sizing: border-box;
  38. box-sizing: border-box;
  39. padding: 0 .75rem;
  40. min-height: 1px;
  41. }
  42. .row .col.s3 {
  43. width: 25%;
  44. margin-left: auto;
  45. left: auto;
  46. right: auto;
  47. }
  48. .row .col.s4 {
  49. width: 33.33333333333%;
  50. margin-left: auto;
  51. left: auto;
  52. right: auto;
  53. }
  54. .row .col.s5 {
  55. width: 41.66666666667%;
  56. margin-left: auto;
  57. left: auto;
  58. right: auto;
  59. }
  60. .row .col.s6 {
  61. width: 50%;
  62. margin-left: auto;
  63. left: auto;
  64. right: auto;
  65. }
  66. .center, .center-align {
  67. text-align: center;
  68. }
  69. /*layer*/
  70. .pick-layer{
  71. position: fixed;
  72. top:0;
  73. bottom:0;
  74. left:0;
  75. right:0;
  76. z-index:99999998;
  77. background:rgba(0,0,0,0.4);
  78. }
  79. /*pick-timer*/
  80. .pick-timer{
  81. overflow: hidden;
  82. position:relative;
  83. border-top:1px solid #eee;
  84. height:220px;
  85. }
  86. .pick-timer .list{
  87. position:absolute;
  88. top:0;
  89. left:0;
  90. z-index:2;
  91. width:100%;
  92. transition:top .4s ease-out;
  93. }
  94. .pick-timer .list li{
  95. height:44px;
  96. line-height:44px;
  97. text-align:center;
  98. color:#666;
  99. font-size:14px;
  100. transition:all .1s ease-out;
  101. width:100%;
  102. overflow: hidden;
  103. text-overflow:ellipsis;
  104. white-space: nowrap;
  105. }
  106. .pick-timer .list li.pick-active{
  107. font-size:14px;
  108. color:#e02222;
  109. }
  110. .pick-timer .current{
  111. position:absolute;
  112. top:88px;
  113. left:0;
  114. z-index:1;
  115. border-top:1px solid #e02222;
  116. border-bottom:1px solid #e02222;
  117. width:100%;
  118. height:44px;
  119. background:#fff;
  120. }
  121. /*pick-head*/
  122. .pick-cancel{
  123. display: block;
  124. font-size:14px;
  125. color:#666;
  126. height:40px;
  127. line-height:40px;
  128. }
  129. .pick-sure{
  130. display: block;
  131. font-size:14px;
  132. color:#e02222;
  133. height:40px;
  134. line-height:40px;
  135. }
  136. .pick-title{
  137. font-size:14px;
  138. color:#666;
  139. height:40px;
  140. line-height:40px;
  141. }
  142. /*current-time*/
  143. .current-time:after{
  144. content:":";
  145. font-size: 14px;
  146. text-align: center;
  147. line-height: 44px;
  148. color: #666;
  149. position: absolute;
  150. top:-2px;
  151. right:0;
  152. }
  153. .current-date-right:after{
  154. content:".";
  155. color:transparent;
  156. width:5px;
  157. border-top:1px solid #999;
  158. position: absolute;
  159. top:20px;
  160. right:0;
  161. }
  162. .current-date-left:before{
  163. content:".";
  164. color:transparent;
  165. width:5px;
  166. border-top:1px solid #999;
  167. position: absolute;
  168. top:20px;
  169. left:0;
  170. }
  171. /**/
  172. .pick-bgTop{
  173. position:absolute;
  174. top:0;
  175. left:0;
  176. width:100%;
  177. height:88px;
  178. /*background: linear-gradient(#fff 0%, rgba(255, 255, 255, .85)45%, rgba(255, 255, 255, .6) 75%, rgba(255, 255, 255, .4) 100%);*/
  179. background: -webkit-gradient(linear, left top, left bottom, from(#efefef),to(rgba(255, 255, 255, .1)));
  180. z-index:1;
  181. }
  182. .pick-bgBottom{
  183. position:absolute;
  184. bottom:0;
  185. left:0;
  186. width:100%;
  187. height:87px;
  188. /*background: linear-gradient(rgba(255, 255, 255, .4) 0%, rgba(255, 255, 255, .6)25%, rgba(255, 255, 255, .85) 65%, #fff 100%);*/
  189. background: -webkit-gradient(linear, left top, left bottom, from(rgba(255, 255, 255, .1)),to(#efefef));
  190. z-index:1;
  191. }

效果图:

</>复制代码

  1. 特别注意:由于字数长度限制,我不能将省市区json数据放在这里,要不然segmentfault会提示字数太长,有需要的小伙伴可以加我qqqq写在.js文件里),我在qq上发给你哦。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/112574.html

相关文章

  • jquery移动省市三级联动插件

    摘要:写省市区三级联动插件的关键是在于你如何编写自己的省市县区文件,你要是把数据格式写对了,可以很轻松的写一省市区插件出来。 最近刚做完一个版本的项目,闲暇时间,就索性把项目中需要用到的插件都自己写了一个,毕竟自己动手丰衣足食才是最重要,自己写的,可以应对各种项目需求,今天又把手机端的省市区三级联动选择功能编写了一个插件出来,代码很简单,样式也是应用的跟日期时间选择插件的一样。写省市区三级联...

    Rocko 评论0 收藏0
  • jquery移动省市三级联动插件

    摘要:写省市区三级联动插件的关键是在于你如何编写自己的省市县区文件,你要是把数据格式写对了,可以很轻松的写一省市区插件出来。 最近刚做完一个版本的项目,闲暇时间,就索性把项目中需要用到的插件都自己写了一个,毕竟自己动手丰衣足食才是最重要,自己写的,可以应对各种项目需求,今天又把手机端的省市区三级联动选择功能编写了一个插件出来,代码很简单,样式也是应用的跟日期时间选择插件的一样。写省市区三级联...

    spacewander 评论0 收藏0
  • EXTJS-6.2.0示例-全国省市三级联动(传统组件方式)

    摘要:实现全国城市三级联动传统模式,监听下拉选择框的事件实现全国城市三级联动传统方式省份请选择城市请选择区县请选择三级联动用到的全国城市数据 extjs 6.2.0 实现全国城市三级联动 传统模式,监听下拉选择框的change事件实现 全国城市三级联动-传统方式 function ge...

    greatwhole 评论0 收藏0
  • 使用vue开发移动管理后台

    摘要:独立完成一个移动端项目不是很明白为何会有这样的商品管理后台,还是有些经验不足,包括对产品的全局思考,对插件的选择等,都有考虑不周的缺点,导致自己中途想换图形界面插件,浪费了点时间,这里记录下,总结下经验,理一下思路。 独立完成一个移动端项目(不是很明白为何会有这样的商品管理后台),还是有些经验不足,包括对产品的全局思考,对插件的选择等,都有考虑不周的缺点,导致自己中途想换图形界面插件,...

    simpleapples 评论0 收藏0
  • 原生js实现省市三级联动插件

    原生js实现省市区三级联动插件 前言 插件功能只满足我司业务需求,如果希望有更多功能的,可在下方留言,我尽量扩展!如果你有需要或者喜欢的话,可以给我github来个star ? 仓库地址在线预览 准备 // 页面上先引入css与js文件 页面中的容器标签不限制,只需给个id就行 var address = new Address({ wrapId: wrap, showArr...

    mumumu 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<