原生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延迟执行实现方法(setTimeout)
Dec 30 Javascript
DB.ASP 用Javascript写ASP很灵活很好用很easy
Jul 31 Javascript
javascript 构造函数强制调用经验总结
Dec 02 Javascript
jQuery避免$符和其他JS库冲突的方法对比
Feb 20 Javascript
使用CDN和AJAX加速WordPress中jQuery的加载
Dec 05 Javascript
node网页分段渲染详解
Sep 05 Javascript
JS对象创建的几种方式整理
Feb 28 Javascript
详解HTML5 使用video标签实现选择摄像头功能
Oct 25 Javascript
浅析Vue 生命周期
Jun 21 Javascript
浅谈webpack4.x 入门(一篇足矣)
Sep 05 Javascript
一篇文章看懂JavaScript中的回调
Jan 05 Javascript
vue本地构建热更新卡顿的问题“75 advanced module optimization”完美解决方案
Aug 05 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
PHP5.2下chunk_split()函数整数溢出漏洞 分析
2007/06/06 PHP
PHP XML error parsing SOAP payload on line 1
2010/06/17 PHP
php 错误处理经验分享
2011/10/11 PHP
php设计模式之单例模式使用示例
2014/01/20 PHP
php中hashtable实现示例分享
2014/02/13 PHP
PHP实现数组转JSon和JSon转数组的方法示例
2018/06/14 PHP
JavaScript随机排序(随即出牌)
2010/09/17 Javascript
框架页面高度自动刷新的Javascript脚本
2013/11/01 Javascript
jQuery可见性过滤器:hidden和:visibility用法实例
2015/06/24 Javascript
JavaScript驾驭网页-获取网页元素
2016/03/24 Javascript
Jquery跨域获得Json的简单实例
2016/05/18 Javascript
把json格式的字符串转换成javascript对象或数组的方法总结
2016/11/03 Javascript
vue动态生成dom并且自动绑定事件
2017/04/19 Javascript
Javascript中this关键字指向问题的测试与详解
2017/08/11 Javascript
javascript将list转换成树状结构的实例
2017/09/08 Javascript
mpvue+vuex搭建小程序详细教程(完整步骤)
2018/09/30 Javascript
vue-cli3 配置开发与测试环境详解
2019/05/17 Javascript
vue将data恢复到初始状态 &amp;&amp; 重新渲染组件实例
2020/09/04 Javascript
[48:41]VP vs VG Supermajor小组赛 B组胜者组决赛 BO3 第二场 6.2
2018/06/03 DOTA
详解Python中最难理解的点-装饰器
2017/04/03 Python
django 修改server端口号的方法
2018/05/14 Python
TensorFlow自定义损失函数来预测商品销售量
2020/02/05 Python
Python安装whl文件过程图解
2020/02/18 Python
利用pyecharts读取csv并进行数据统计可视化的实现
2020/04/17 Python
Python使用多进程运行含有任意个参数的函数
2020/05/02 Python
整理HTML5中支持的URL编码与字符编码
2016/02/23 HTML / CSS
Notino意大利:购买香水和化妆品
2018/11/14 全球购物
奥地利时尚、美容、玩具和家居之家:Kastner & Öhler
2020/04/26 全球购物
游戏商店:Eneba
2020/04/25 全球购物
为什么如下的代码int a=100,b=100;long int c=a * b;不能工作
2013/11/29 面试题
制定岗位职责的原则
2013/11/08 职场文书
入党思想汇报怎么写
2014/04/03 职场文书
地质灾害防治方案
2014/05/14 职场文书
村庄环境整治方案
2014/05/15 职场文书
2014年志愿者工作总结
2014/11/20 职场文书
祝福语集锦:给满月宝宝的祝福语
2019/11/20 职场文书