微信小程序实现天气预报功能


Posted in Javascript onJuly 18, 2018

本文实例为大家分享了微信小程序实现天气预报功能的具体代码,供大家参考,具体内容如下

微信小程序实现天气预报功能

这个案例是仿UC中天气界面做的中间也有点出入,预留了显示当前城市名字和刷新图标的位置,自己可以写下,也可以添加搜索城市。值得注意的是100%这个设置好像已经不好使了,可以通过获取设备的高度通过数据绑定设置高度。地址:weather

界面主要分为四部分:

微信小程序实现天气预报功能

第一部分

微信小程序实现天气预报功能

这里是预留的一块可以自行添加补充下

<view class="newTopView">
<!--左边添加当前城市名字,点击跳转选择城市 右边添加刷新当前天气-->
</view>

第二部分:

微信小程序实现天气预报功能

<view class="topView">
 <view class="degreeView">
 <!--当前温度-->
 <text class="degree">{{currentTemperature}}</text>
 <!--度数图标-->
 <image class="circle" src="../../image/circle.png"></image>
 </view>
 <view class="detailInfo">
 <view class="degreeView">
 <!--夜间天气情况-->
 <text class="detailInfoDegree">{{nightAirTemperature}}</text>
 <image class="detailInfoCircle" src="../../image/circle.png" />
 <text class="detailInfoLine">/</text>
 <!--白天天气-->
 <text class="detailInfoDegree">{{dayAirTemperature}}</text>
 <!-- style优先级比class高会覆盖class中相同属性 -->
 <image class="detailInfoCircle" style="margin-left: 57rpx; margin-right: 40rpx" src="../../image/circle.png" />
 <!-- 当前天气名字 -->
 <text class="detailInfoName">{{weather}}</text>
 </view>
 </view>
 </view>

第三部分:

微信小程序实现天气预报功能

<!-- 中间部分 -->
 <view class="centerView">
 <view class="centerItem" style="margin-right: 25rpx;">
 <image class="centerItemImage" src="../../image/leaf.png" />
 <!-- 相同属性抽出来! -->
 <!--污染指数-->
 <text class="centerItemText" style="margin-left: 10rpx; margin-right: 10rpx">{{aqi}}</text>
 <!--污染指数对应name-->
 <text class="centerItemText">{{quality}}</text>
 </view>
 <view class="centerItem" style="margin-left: 25rpx">
 <image class="centerItemImage" src="../../image/wind.png" />
 <text class="centerItemText" style="margin-left: 10rpx; margin-right: 10rpx">{{windPower}}</text>
 <!--风-->
 <text class="centerItemText">{{windDirection}}</text>
 </view>
 </view>

第四部分:

微信小程序实现天气预报功能

<!-- 底部view -->
 <view class="bottomView">
 <!--数据返回的不是数组 在js中拼接的数组-->
 <block wx:for-items="{{list}}">
 <view class="bottomItemView">
 <image class="bottomImage" src="{{item.day_weather_pic}}" style="margin-bottom: 15rpx;" />
 <text wx:if="{{item.weekday == 1}}" class="bottomText">星期一</text>
 <text wx:elif="{{item.weekday == 2}}" class="bottomText">星期二</text>
 <text wx:elif="{{item.weekday == 3}}" class="bottomText">星期三</text>
 <text wx:elif="{{item.weekday == 4}}" class="bottomText">星期四</text>
 <text wx:elif="{{item.weekday == 5}}" class="bottomText">星期五</text>
 <text wx:elif="{{item.weekday == 6}}" class="bottomText">星期六</text>
 <text wx:else="{{item.weekday == 7}}" class="bottomText">星期日</text>
 <view class="degreeView" style="margin-top: 20rpx;">
  <text class="detailInfoDegree">{{item.night_air_temperature}}</text>
  <image class="detailInfoCircle" src="../../image/circle.png" />
  <text class="detailInfoLine">/</text> 
  <text class="detailInfoDegree">{{item.day_air_temperature}}</text>
  <!-- style优先级比class高会覆盖class中相同属性 -->
  <image class="detailInfoCircle" style="margin-left: 57rpx; margin-right: 40rpx" src="../../image/circle.png" />
 </view>
</view>

js

//index.js
//获取应用实例
var app = getApp()
Page({
 data: {
 //加载状态
 loadingHidden: false,
 //当前温度
 currentTemperature: '',
 //夜间温度
 nightAirTemperature: '',
 //白天温度
 dayAirTemperature: '',
 //当前天气
 weather: '',
 //污染指数
 aqi: '',
 //污染程度
 quality: '',
 //风力
 windPower: '',
 //风向
 windDirection: '',
 //因为数据返回不是数组所以要自己封装一个数组
 list: [],
 height: 0,


 },

 onLoad: function () {
 console.log('onLoad')
 var that = this

 //100%好像不好使 可以获取设备高度
 wx.getSystemInfo({
 success: function (res) {
 that.data.height = res.windowHeight;
 }
 })

 wx.getLocation({
 success: function (res) {
 //通过经纬度请求数据
 wx.request({
  //这个网站有免费API赶紧收藏
  url: 'http://route.showapi.com/9-5',
  data: {
  showapi_appid: '11697',
  showapi_sign: '6c0c15c5ec61454dac5288cea2d32881',
  //
  from: '5',
  lng: res.longitude,
  lat: res.latitude,
  //获取一周情况 0是不获取
  needMoreDay: '1',
  needIndex: '1'
  },
  success: function (res) {
  console.log(res)
  console.log(res.data.showapi_res_body.now.api)
  that.setData({
  //改变加载状态
  loadingHidden: true,

  currentTemperature: res.data.showapi_res_body.now.temperature,
  nightAirTemperature: res.data.showapi_res_body.f1.night_air_temperature,
  dayAirTemperature: res.data.showapi_res_body.f1.day_air_temperature,
  weather: res.data.showapi_res_body.now.weather,
  aqi: res.data.showapi_res_body.now.aqi,
  quality: res.data.showapi_res_body.now.aqiDetail.quality,
  windPower: res.data.showapi_res_body.now.wind_power,
  windDirection: res.data.showapi_res_body.now.wind_direction,
  //拼接数组
  list: [
  {
   'day_weather_pic': res.data.showapi_res_body.f1.day_weather_pic,
   'weekday': res.data.showapi_res_body.f1.weekday,
   'day_air_temperature': res.data.showapi_res_body.f1.day_air_temperature,
   'night_air_temperature': res.data.showapi_res_body.f1.night_air_temperature
  },
  {
   'day_weather_pic': res.data.showapi_res_body.f2.day_weather_pic,
   'weekday': res.data.showapi_res_body.f2.weekday,
   'day_air_temperature': res.data.showapi_res_body.f2.day_air_temperature,
   'night_air_temperature': res.data.showapi_res_body.f2.night_air_temperature
  },
  {
   'day_weather_pic': res.data.showapi_res_body.f3.day_weather_pic,
   'weekday': res.data.showapi_res_body.f3.weekday,
   'day_air_temperature': res.data.showapi_res_body.f3.day_air_temperature,
   'night_air_temperature': res.data.showapi_res_body.f3.night_air_temperature
  },
  {
   'day_weather_pic': res.data.showapi_res_body.f4.day_weather_pic,
   'weekday': res.data.showapi_res_body.f4.weekday,
   'day_air_temperature': res.data.showapi_res_body.f4.day_air_temperature,
   'night_air_temperature': res.data.showapi_res_body.f4.night_air_temperature
  },
  {
   'day_weather_pic': res.data.showapi_res_body.f5.day_weather_pic,
   'weekday': res.data.showapi_res_body.f5.weekday,
   'day_air_temperature': res.data.showapi_res_body.f5.day_air_temperature,
   'night_air_temperature': res.data.showapi_res_body.f5.night_air_temperature
  },
  {
   'day_weather_pic': res.data.showapi_res_body.f6.day_weather_pic,
   'weekday': res.data.showapi_res_body.f6.weekday,
   'day_air_temperature': res.data.showapi_res_body.f6.day_air_temperature,
   'night_air_temperature': res.data.showapi_res_body.f6.night_air_temperature
  },
  {
   'day_weather_pic': res.data.showapi_res_body.f7.day_weather_pic,
   'weekday': res.data.showapi_res_body.f7.weekday,
   'day_air_temperature': res.data.showapi_res_body.f7.day_air_temperature,
   'night_air_temperature': res.data.showapi_res_body.f7.night_air_temperature
  }

  ]
  })
  }
 })

 }
 })

 }
})

wxss

.container {
 display: flex;
 flex-direction: column;
 justify-content: space-between;

}

.newTopView {
 display: flex;
 flex-direction: row;
 justify-content: space-between;
}

/* 头部样式上半部分*/
.topView {
 flex-direction: column;
 align-self: center;
 margin-top: -450rpx;
}
/*当前度数样式*/
.degreeView {
 flex-direction: row;
 position: relative;
}
/*当前温度度数图标*/
.circle {
 width: 35rpx;
 height: 35rpx; 
 position: absolute;
 left: 130rpx;
} 
/*当前度数数组*/
.degree {
 color: white;
 font-size: 130rpx
}

/*详细View样式*/
.detailInfoView {
 flex-direction: row;
}
/*当前最高和最低温度度数图标*/
.detailInfoCircle {
 width: 20rpx;
 height: 20rpx; 
 position: absolute;
 left: 30rpx;
} 

/*当前度数数组*/
.detailInfoDegree {
 color: white;
 font-size: 30rpx
}

/*斜线*/
.detailInfoLine {
 color: white;
 margin-left: 15rpx;
 font-size: 30rpx;
}
/*比如阴天 晴天,暴雨*/
.detailInfoName {
 font-size: 30rpx;
 color: white;
 margin-left: 20rpx;
 align-self: center
}

/*中间view样式*/
.centerView {
 display: flex;
 flex-direction: row;
 margin-top: -550rpx;
 justify-content: center;
 align-items: center;
}

/*中间view分为两个view*/
.centerItem {
 display: flex;
 flex-direction: row;
 align-items: center;
 justify-content: center;
}
/*Item中图片样式*/
.centerItemImage {
 width: 25rpx;
 height: 25rpx;
}
/*文字样式*/
.centerItemText {
 font-size: 28rpx;
 color: white;
}

/*底部view样式*/
.bottomView {
 margin-top: -200rpx;
 display: flex;
 flex-direction: row;
 justify-content: space-around;
 align-items: center;
}


.bottomItemView {
 display: flex;
 flex-direction: column;
 align-items: center;
 margin-bottom: 200rpx;
}

/*最近七天样式*/
.bottomImage {
 width: 70rpx;
 height: 70rpx;
}

.bottomText {
 font-size: 28rpx;
 color: white;
}

PS:

开发者工具无法显示问题:是因为View没有获得高度,默认个高度或者直接修改wxml中height高度即可。

另外,本站在线工具小程序上有一款天气查询工具,还添加了城市选择的功能,感兴趣的朋友可以扫描如下小程序码查看:

微信小程序实现天气预报功能

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

Javascript 相关文章推荐
JavaScript中两个感叹号的作用说明
Dec 28 Javascript
使用js检测浏览器的实现代码
May 14 Javascript
Node.js中的事件驱动编程详解
Aug 16 Javascript
JQuery包裹DOM节点的方法
Jun 11 Javascript
js判断手机访问或者PC的几个例子(常用于手机跳转)
Dec 15 Javascript
JS实现alert中显示换行的方法
Dec 17 Javascript
jQuery实现本地预览上传图片功能
Jan 08 Javascript
jQueryUI DatePicker 添加时分秒
Jun 04 Javascript
深入理解Javascript中的观察者模式
Feb 20 Javascript
AngularJS页面传参的5种方式
Apr 01 Javascript
jQuery仿移动端支付宝键盘的实现代码
Aug 15 jQuery
微信小程序仿通讯录功能
Apr 09 Javascript
vue代理和跨域问题的解决
Jul 18 #Javascript
小程序自定义组件实现城市选择功能
Jul 18 #Javascript
微信小程序实践之动态控制组件的显示/隐藏功能
Jul 18 #Javascript
微信小程序项目实践之主页tab选项实现
Jul 18 #Javascript
详解性能更优越的小程序图片懒加载方式
Jul 18 #Javascript
微信小程序项目实践之验证码倒计时功能
Jul 18 #Javascript
微信小程序日期选择器实例代码
Jul 18 #Javascript
You might like
松下Panasonic RF-B65电路分析
2021/03/02 无线电
php中的登陆login
2007/01/18 PHP
thinkPHP模型初始化实例分析
2015/12/03 PHP
Yii中的relations数据关联查询及统计功能用法详解
2016/07/14 PHP
jquery ajax 调用失败的原因示例介绍
2013/09/27 Javascript
javascript:window.open弹出窗口的位置问题
2014/03/18 Javascript
Jquery解析json字符串及json数组的方法
2015/05/29 Javascript
程序员必知35个jQuery 代码片段
2015/11/05 Javascript
浅谈JS中的bind方法与函数柯里化
2016/08/10 Javascript
微信小程序之ES6与事项助手的功能实现
2016/11/30 Javascript
Bootstrap3 datetimepicker控件使用实例
2016/12/13 Javascript
Angular.js实现注册系统的实例详解
2016/12/18 Javascript
vue2实现移动端上传、预览、压缩图片解决拍照旋转问题
2017/04/13 Javascript
Ionic项目中Native Camera的使用方法
2017/06/07 Javascript
基于jquery实现多级菜单效果
2017/07/25 jQuery
Node.js中使用mongoose操作mongodb数据库的方法
2017/09/12 Javascript
微信小程序使用modal组件弹出对话框功能示例
2017/11/29 Javascript
Vue-Quill-Editor富文本编辑器的使用教程
2018/09/21 Javascript
react native基于FlatList下拉刷新上拉加载实现代码示例
2018/09/30 Javascript
深入了解js原型模式
2019/05/30 Javascript
JS获取动态添加元素的方法详解
2019/07/31 Javascript
JQuery省市联动效果实现过程详解
2020/05/08 jQuery
Vue使用自定义指令实现拖拽行为实例分析
2020/06/06 Javascript
Python单链表简单实现代码
2016/04/27 Python
浅谈Pycharm调用同级目录下的py脚本bug
2018/12/03 Python
Python中base64与xml取值结合问题
2019/12/22 Python
pytorch forward两个参数实例
2020/01/17 Python
opencv+python实现均值滤波
2020/02/19 Python
Michael Kors美国官网:美式奢侈生活风格的代表
2016/11/25 全球购物
十佳大学生事迹材料
2014/01/29 职场文书
12月小学生校园广播稿
2014/02/04 职场文书
《永远的白衣战士》教学反思
2014/04/25 职场文书
竞选班干部演讲稿600字
2014/08/20 职场文书
干部作风整顿自我剖析材料和整改措施
2014/09/18 职场文书
法定代表人授权委托书格式
2014/10/14 职场文书
Python 装饰器(decorator)常用的创建方式及解析
2022/04/24 Python