原生js实现html手机端城市列表索引选择城市


Posted in Javascript onJune 24, 2020

本文实例为大家分享了js实现手机端城市列表索引选择城市的具体代码,供大家参考,具体内容如下

html部分:

<div class="cityPage">
 <div class="cityContent">
 <div class="inputBox">
 <input type="text" placeholder="中文 / 拼音首字母" id="findcityInp">
 </div>
 <div class="localCity">定位城市</div>
 <div class="cityname">上海市</div>
 </div>
 <div id='list'>
 <section id="sectionBox"></section>
 <nav id="navBar"></nav>
 </div>
 <div class="letterBox"></div>
</div>

css部分:

*{
 margin: 0;
 padding: 0;
 list-style: none;
}
html{
 font-size: 12px;
}
body {
 background-color: #f5f5f5;
 font-family: 'PingFang SC Regular', 'PingFang SC';
 width: 100%;
 height: 100%;
 min-width: 320px;
 max-width: 480px;
 position: relative;
}

.cityPage {
 width: 100%;
 height: 100%;
 /* border: 1px solid black; */
 position: relative;
 top: 0;
 display: flex;
 flex-direction: column;
 /* justify-content: center; */
}

.cityContent {
 width: 100%;
 height: 140px;
 /* border: 1px solid black; */
 background: #f7f7f9;
 position: fixed;
 z-index: 9999;
 top: 0;

}

.inputBox input {
 width: 90%;
 height: 30px;
 border: 1px solid rgb(215, 215, 215);
 outline: none;
 background: #fff;
 margin-left: 4%;
 border-radius: 4px;
 padding-left: 4px;
 color: #9e9e9e;
 font-size: 14px;
 margin-bottom: 16px;
 margin-top: 14px;

}

.localCity {
 color: #333;
 font-size: 13px;
 font-weight: bold;
 margin-left: 4.5%;
 margin-bottom: 16px;
}

.cityname {
 font-size: 13px;
 margin-left: 4.5%;
 margin-bottom: 16px;
}

#list {
 font-size: 13px;
 position: fixed;
 height: 100%;
 top: 140px;
 width: 100%;
 overflow: scroll;
 font-size: 15px;
 /* margin-bottom: 140px; */
 /* bottom: 200px; */
}

#list>section {
 overflow-y: auto;
 height: 100%;
 margin-bottom: 140px;
}

#list>section>dl>dt {
 background: #f7f7f9;
 color: #999;
 height: 40px;
 line-height: 40px;
 padding-left: 15px;
}

#list>section>dl>dd {
 color: #333;
 line-height: 40px;
 padding-left: 15px;
 position: relative;
 background-color: #fff;
}

#list>section>dl>dd:after {
 content: '';
 position: absolute;
 left: 0;
 bottom: 1px;
 width: 100%;
 height: 1px;
 background-color: #c8c7cc;
 transform: scaleY(.5);
 -webkit-transform: scaleY(.5);
}

#list>section>dl>dd:last-of-type:after {
 display: none;
}

#navBar {
 position: fixed;
 width: 26px;
 height: 50%;
 right: 0;
 z-index: 30;
 top: 50%;
 display: flex;
 flex-direction: column;
 margin-top: -25%;
 /* text-align: center; */
}

#navBar.active {
 background: rgba(211, 211, 211, .6);
}

#navBar>div {
 text-align: center;
 display: block;
 text-decoration: none;
 /* height: 4.166%;
 line-height: 100%; */
 color: #333;
 font-size: 13px;
 flex: 1;
}
.letterBox{
 width: 40px;
 height: 40px;
 background:#9f9f9f;
 opacity: .5;
 position: fixed;
 top: 50%;
 left: 50%;
 margin-top: -25px;
 margin-left: -25px;
 text-align: center;
 line-height: 40px;
 color: #fff;
 display: none;
}

js部分:

$(function () { 
 initCities(cityData);
 clickAction()

 //输入城市查询
 var key = false;
 $('#findcityInp').on('compositionstart', function () {
 key = true;
 console.log('不搜索')
 });
 $('#findcityInp').on('compositionend', function (e) {
 var keyWord = $.trim(e.target.value);
 if(keyWord.length>0){
 var result = findCity(keyWord, cityData);
 initCities(result);
 bindEvent();

 }else{
 initCities(cityData);
 bindEvent();
 
 }
 });

 $('#findcityInp').on('change', function (e) {
 var keyWord = $.trim(e.target.value);
 console.log(keyWord)
 var result = findCity(keyWord, cityData);
 // console.log(result)
 initCities(result)

 });
 //城市查询
 function findCity(keyWord, data) {
 if (!(data instanceof Array)) return;

 var reg = new RegExp(keyWord);
 var arr = [];
 var obj ={
  name:'',
  cities:[]
 }
 if(keyWord.length>0 && checkCh(keyWord)==false){
  data.forEach((item, index) => {
  item.cities.forEach((childItem, childIndex) => {
   if (childItem.tags.match(reg)) {
   obj.name = childItem.tags[0];
   obj.cities.push(childItem)
   arr=[obj]
   }
  })
  })
 }else if(keyWord.length ==1 && checkCh(keyWord)==true){
  data.forEach((item,index)=>{
  if(item.name == keyWord){
   // console.log(item)
   arr.push(item)
  }
  })
 }
 else{
  arr = data
 }
 return arr;
 }
 

function checkCh(str){
 var RegExp = /^[a-zA-Z]{1}$/;
 return RegExp.test(str);  
}
//点击右边描点
function toTarget(tag){
 var text = $(tag).text();
 location.href = "#"+text;
 $('.letterBox').html(text);
$('.letterBox').show()
setTimeout(function(){
 $('.letterBox').hide()
},1000)

}
//初始化城市列表
function initCities(cityData) {
 var g = "";
 $('section').html('');
 $('nav').html('')
 cityData.forEach((item, index) => {
  g += "<dl id=" + item.name + "><dt>" + item.name + "</dt>";
  item.cities.forEach((citiesItem, citiesIndex) => {
   g += "<dd data-id=" + citiesItem.cityid + " data-name=" + citiesItem.name + " class='list' οnclick='clickAction()'>" + citiesItem.name + "</dd>"
  })
  g += "</dl>"
 })
 $('section').append(g);

 var g = $('nav').height() / 26;
 var f = '';

 cityData.forEach((item, index) => {
  // f += '<a href="#' + item.name + '" rel="external nofollow" style="height:' + g + "px;line-height:" + g + 'px">' + item.name + "</a>"
  f+=`<div οnclick="toTarget(this)" style="height:${g}px;line-height:${g}px">${item.name}</div>`
 })
 $('nav').append(f);
}



//点击城市列表某城市
function clickAction(){
 $('.list').click(function (e) {
 console.log(e.target.getAttribute('data-name'))
 })
}

原生js实现html手机端城市列表索引选择城市

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
Javascript 中 null、NaN和undefined的区别总结
Apr 10 Javascript
JQuery对id中含有特殊字符的转义处理示例
Sep 06 Javascript
Js使用WScript.Shell对象执行.bat文件和cmd命令
Dec 18 Javascript
JavaScript事件 &quot;事件对象&quot;的注意要点
Jan 14 Javascript
Node.js五大应用性能技巧小结(必须收藏)
Aug 09 Javascript
JS控制鼠标拒绝点击某一按钮的实例
Dec 29 Javascript
解析Angular 2+ 样式绑定方式
Jan 15 Javascript
微信小程序实现YDUI的ScrollNav组件
Feb 02 Javascript
Array数组对象中的forEach、map、filter及reduce详析
Aug 02 Javascript
jQuery实现获取当前鼠标位置并输出功能示例
Jan 05 jQuery
vue + typescript + 极验登录验证的实现方法
Jun 27 Javascript
vue图片裁剪插件vue-cropper使用方法详解
Dec 16 Vue.js
jQuery实时统计输入框字数及限制
Jun 24 #jQuery
jQuery实现移动端下拉展现新的内容回弹动画
Jun 24 #jQuery
JS forEach跳出循环2种实现方法
Jun 24 #Javascript
js判断鼠标移入移出方向的方法
Jun 24 #Javascript
JS判断数组是否包含某元素实现方法汇总
Jun 24 #Javascript
JS script脚本中async和defer区别详解
Jun 24 #Javascript
javascript实现前端分页效果
Jun 24 #Javascript
You might like
php array_filter除去数组中的空字符元素
2020/06/21 PHP
简单实现php上传文件功能
2017/09/21 PHP
使用 laravel sms 构建短信验证码发送校验功能
2017/11/06 PHP
PHP文件后缀不强制为.php方法
2019/03/31 PHP
用于判断用户注册时,密码强度的JS代码
2009/01/01 Javascript
jquery代码实现简单的随机图片瀑布流效果
2015/04/20 Javascript
JavaScript函数使用的基本教程
2015/06/04 Javascript
jQuery+HTML5美女瀑布流布局实现方法
2015/09/21 Javascript
jQuery Html控件基本操作(日常收集整理)
2016/03/11 Javascript
浅谈javascript中关于日期和时间的基础知识
2016/07/13 Javascript
three.js快速入门【推荐】
2017/01/21 Javascript
js清除浏览器缓存的几种方法
2017/03/15 Javascript
Angular.js 4.x中表单Template-Driven Forms详解
2017/04/25 Javascript
nodejs制作爬虫实现批量下载图片
2017/05/19 NodeJs
解决微信二次分享不显示摘要和图片的问题
2017/08/18 Javascript
vue.js删除列表中的一行
2018/06/30 Javascript
IE9 elementUI文件上传的问题解决
2018/10/17 Javascript
layer.open 获取不到表单信息的解决方法
2019/09/26 Javascript
layui的数据表格+springmvc实现搜索功能的例子
2019/09/28 Javascript
ant design实现圈选功能
2019/12/17 Javascript
html2canvas属性和使用方法以及如何使用html2canvas将HTML内容写入Canvas生成图片
2020/01/12 Javascript
python中星号变量的几种特殊用法
2016/09/07 Python
Python 多维List创建的问题小结
2019/01/18 Python
pyqt5移动鼠标显示坐标的方法
2019/06/21 Python
Django Python 获取请求头信息Content-Range的方法
2019/08/06 Python
Jupyter Notebook打开任意文件夹操作
2020/04/14 Python
Django模板报TemplateDoesNotExist异常(亲测可行)
2020/12/18 Python
民族学专业求职信
2014/07/28 职场文书
预备党员学习十八届三中全会精神思想汇报
2014/09/13 职场文书
2014年仓库管理工作总结
2014/12/17 职场文书
个人党性分析材料
2014/12/19 职场文书
委托公证书格式
2015/01/26 职场文书
企业百日安全活动总结
2015/05/07 职场文书
Python 中数组和数字相乘时的注意事项说明
2021/05/10 Python
Nginx stream 配置代理(Nginx TCP/UDP 负载均衡)
2021/11/17 Servers
阿里云日志过滤器配置日志服务
2022/04/09 Servers