文章目录

  • 前情提要
    • wx:if 和 hidden
    • 包装元素block
  • 小程序项目
    • pages/search.json
    • pages/search.wxml
    • pages/search.wxss
    • pages/search.js
  • 相关链接

前情提要

wx:if 和 hidden

wx:if 类似于Vue中的v-if条件渲染hidden 类似于Vue中的v-show,简单地控制显示与隐藏

包装元素block

wx:if可以添加到一个标签上,可以添加到一组标签上。如果想用wx:if来控制一组标签,用<block/>将该组标签包装起来即可。

小程序项目

代码涉及的主要文件有:

  1. pages/search/search.json
  2. pages/search/search.wxml
  3. pages/search/search.wxss
  4. pages/search/search.js

pages/search.json

{"usingComponents": {},"navigationBarTitleText": "搜一搜","navigationBarBackgroundColor": "#fff","navigationBarTextStyle": "black"
}

pages/search.wxml

<view class="search-container"><view class="header"><view class="input-box"><image src="/static/images/search.png"></image><input type="text" placeholder="你想听的 这里都有~" placeholder-style="font-swxize:27rpx"bindinput="handleInput" value="{{keyword}}"/><image src="/static/images/cross.png" bindtap="removeKeyword" hidden="{{!keyword}}"></image></view></view><block wx:if="{{keyword}}"><view class="search-container"><view class="search-title">搜索"{{keyword}}"</view><view class="search-list"><view class="search-item" wx:for="{{searchList}}" wx:key="id"><image src="/static/images/search.png"></image><view class="content">{{item.content}}</view></view></view></view></block><block wx:else><view class="history-container" wx:if="{{historyList.length}}"><view class="history-header"><view class="history-title">搜索历史</view><image src="/static/images/delete.png" bindtap="deleteHistory"></image></view><view class="history-list"><text class="history-item" wx:for="{{historyList}}" wx:key="*this">{{item}}</text></view></view><view class="hot-container"><view class="hot-title">热搜榜</view><view class="hot-list"><view class="hot-item" wx:for="{{hotList}}" wx:key="id"><text class="order" style="{{(index===0 || index ===1 || index==2) && 'color:#d81e06' }}">{{index+1}}</text><text class="name">{{item.keyword}}</text><image wx:if="{{item.iconUrl}}" src="{{item.iconUrl}}"></image></view></view></view></block>
</view>

pages/search.wxss

.search-container{padding: 20rpx;
}
.input-box{background: #eee;border-radius: 28rpx;display: flex;align-items: center;
}
.input-box input{height: 60rpx;line-height: 60rpx;flex: 1;font-size: 27rpx;
}
.input-box image{width: 36rpx;height: 36rpx;padding: 0 20rpx;
}
.hot-container{margin: 20rpx 0;
}
.hot-container .hot-title{font-size: 26rpx;font-weight:550;
}
.hot-list{padding: 10rpx 0 ;
}
.hot-item{height: 60rpx;line-height: 60rpx;display: flex;align-items: center;margin-bottom: 20rpx;
}
.hot-item .order{display: inline-block;width: 40rpx;height: 60rpx;line-height: 60rpx;text-align: center;margin-right: 30rpx;color: #888;
}
.hot-item .name{font-weight: 550;
}
.hot-item image{width: 48rpx;height: 48rpx;margin-left: 20rpx;
}
.search-container .search-title{color: #d81e06;height: 80rpx;line-height: 80rpx;font-size: 28rpx;
}
.search-item{display: flex;align-items: center;height: 80rpx;line-height: 80rpx;
}
.search-item image{width: 28rpx;height: 28rpx;margin-right: 20rpx;
}
.search-item .content{flex:1;color: #666;font-size: 28rpx;
}
.history-container{margin-top: 20rpx;
}
.history-header {display: flex;align-items: center;justify-content: space-between;
}
.history-header .history-title{font-size: 26rpx;font-weight:550;
}
.history-header image{width: 36rpx;height: 36rpx;
}
.history-list{display: flex;flex-wrap: wrap;padding: 20rpx 0;
}
.history-item{font-size: 26rpx;height: 36rpx;line-height: 36rpx;text-align: center;padding: 6rpx 16rpx;background: #eee;border-radius: 16rpx;margin: 0 20rpx 20rpx 0;
}

pages/search.js

const host = "http://localhost:3000"
let timer = null;Page({data:{hotList:[],keyword:'',searchList:[],historyList:[]},onLoad(){this.getHotList();const historyList = wx.getStorageSync('historyList');if(historyList){this.setData({historyList})}},getHotList(){const result = [{id:"001",keyword:"周杰伦",iconUrl:host+"/images/hot-fill.png"},{id:"002",keyword:"最伟大的作品"},{id:"003",keyword:"林俊杰"},{id:"004",keyword:"孤勇者"},{id:"005",keyword:"再见莫妮卡"},{id:"006",keyword:"陈奕迅"},{id:"007",keyword:"李荣浩"},{id:"008",keyword:"毛不易"}]this.setData({hotList:result})},handleInput(event){const keyword = event.detail.value.trim();if(!keyword) {this.setData({keyword:''});return;}this.throttle(this.getSearchList,500);const {historyList} = this.data;const index = historyList.indexOf(keyword);if(index > -1){historyList.splice(index,1);}const newHistoryList = [keyword,...historyList].slice(0,10) //最多显示10条搜索历史,且后来者居上wx.setStorageSync("historyList",newHistoryList)this.setData({keyword,historyList:newHistoryList});},throttle(fn,delay){if(timer != null) return;timer = setTimeout(() => {timer = nullfn();},delay) },getSearchList(){const result = [{id:"001",content:"周杰伦"},{id:"002",content:"周杰伦 最伟大的作品"},{id:"003",content:"周杰伦 爷爷泡的茶"},{id:"004",content:"周杰伦 珊瑚海"},{id:"005",content:"周杰伦 夜的第七章"},{id:"006",content:"周杰伦 说好不哭"},{id:"007",content:"周杰伦 等你下课"},{id:"008",content:"周杰伦 我是如此相信"}]this.setData({searchList:result})},removeKeyword(){this.setData({keyword:'',searchList:[]})},deleteHistory(){this.setData({historyList:[]});wx.removeStorageSync('historyList');}
})

相关链接

条件渲染

微信小程序实现音乐搜索页面相关推荐

  1. html5音乐播放器设计论文,基于微信小程序的音乐播放器设计和毕业论文

    摘  要 随着通信技术的发展和智能设备的普及,移动互联网在近两年发展迅猛,新兴的移动社交软件"微信"逐渐走进了手机用户的生活,深受全国数亿用户的欢迎.随着微信版本的不断更新,微信也 ...

  2. 后退一步 小程序_微信小程序返回上一级页面的实现代码

    微信小程序返回上一级页面 wx.navigateBack({ delta: 1 //返回上一级页面}) html: 保存 js: //提交 submit(){ let data = this.data ...

  3. 微信小程序云音乐项目

    微信小程序云音乐项目 微信小程序云音乐项目 1. 项目介绍 技术栈 项目使用库 项目目标 2. 页面效果和功能展示 3. 项目源码与 API 接口 4. 项目说明 5. 致谢 微信小程序云音乐项目 1 ...

  4. 第7课 微信小程序实现图片搜索器案例:

    第7课 微信小程序实现图片搜索器案例: 效果图如下: 手机运行效果: 来我们来看看代码的全局样式: 首先在app.json内的pages把需要的index1.index2页面注册 之后我们只需写ind ...

  5. 微信小程序实现音乐播放器(2)

    文章目录 前情提要 BackgroundAudioManager API:wx.setNavigationBarTitle() 搭建静态资源服务器 小程序项目 app.json app.wxss pa ...

  6. 微信小程序之音乐播放

    微信小程序之音乐播放 这次用的是API中的wx.createInnerAudioContext()接口 (创建内部 audio 上下文 InnerAudioContext 对象.) InnerAudi ...

  7. 微信小程序最全搜索功能

    微信小程序最全搜索功能 主要功能: 搜索 点击x号清空输入框内容 点击取消返回上一页面 搜索数据为空提示 历史记录 清空历史记录 点击历史记录搜索 点击搜索列表带参数返回上一页面 代码如下: < ...

  8. 微信小程序实现音乐播放器(3)(使用全局数据实现数据共享)

    文章目录 前情提要 搭建静态资源服务器 小程序项目 app.json app.js app.wxss pages/index/index.json pages/index/index.wxml pag ...

  9. 微信小程序实现音乐播放器(5)

    文章目录 前情提要 pubsubjs实现页面通信 moment.js实现时间格式化 BackgroundAudioManager 小程序项目 app.json app.wxss app.js page ...

  10. 微信小程序—模拟豆瓣搜索电影(图文)

    微信小程序-模拟豆瓣搜索电影 先新建search目录和page 1.在search.wxml页面 给input添加bindinput事件 给input写一个value值,用来清空 给button绑定b ...

最新文章

  1. desc mysql 连表查询_Mysql连表查询
  2. Centos6.7安装docker1.7.1
  3. Spring与Struts1的集成
  4. 树莓派GPIO的两种模式区别
  5. python2和python3中encoding的差别
  6. 总感觉这个访问量过于均匀……
  7. python数字转中文大写_阿拉伯数字转换成大写汉字的Python代码
  8. 【荐】卓有成效的管理者
  9. 什么是数据库分组查询(详解)
  10. 基于不同策略的英文单词的词频统计和检索系统(C++)
  11. 5000字 大数据时代读书笔记_大数据时代读书笔记
  12. 贝叶斯例题(一)先验分布与后验分布
  13. 计算机自定义桌面,电脑桌面图标自定义摆放,如何自定义桌面图标
  14. Android使用CameraX打开相机拍照简单使用
  15. 数据结构:一元多项式(线性表)
  16. linux 触摸屏驱动课程设计,linux驱动编写(触摸屏驱动)
  17. [课业] 18 | 软工 | 软件体系结构基础
  18. WPF水滴马赛克效果
  19. Unity——获取本机MAC地址
  20. DS1302时钟(显示时间,不可调)

热门文章

  1. 0704最简单的驱动hello
  2. 社会新现象:“蜗婚”离婚不分家
  3. 超市进销存管理系统(SSM,MYSQL,VUE,JSP)
  4. 智能暖风机——FAQ
  5. 陆面生态水文模拟与多源遥感数据同化技术
  6. 网络拓扑结构的优缺点分析
  7. 安装ADOBE READER时无法将数值写入键_php_sir_新浪博客
  8. pandas daraframe 写入读取excel文件,并简单计算
  9. 全自动加药装置详细介绍
  10. 基于百度指数的股票收益率多因子模型研究