前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >开发一个微信小程序(5):查询天气-添加未来24小时逐小时天气

开发一个微信小程序(5):查询天气-添加未来24小时逐小时天气

作者头像
冰霜
发布2023-02-24 11:07:55
7381
发布2023-02-24 11:07:55
举报

在基础实时天气的基础上,展示未来24小时的逐小时天气情况,效果如下

这个功能比较简单,只需要调一下和风天气的24小时预报拿到数据,然后在小程序中使用 <scroll-view> 标签滚动显示数据即可

耗时最长的是调整样式,需要把时间、天气图标、温度、天气描述纵向排列,整了半天才弄好

打开pages/weather/weather.js,在 queryWeather()方法中添加如下代码,调用24小时天气接口

代码语言:javascript
复制
// 获取locationid后,查询未来24小时天气,在success中发起请求
                  wx.request({
                    url: 'https://devapi.qweather.com/v7/weather/24h', 
                    method: 'GET',
                    data: {
                      key: this.data.key,
                      location: location_id
                    },
                    success: (res) => {
                      console.log(res);
                      this.setData({
                        twenty_four: res.data.hourly,
                        flag: true
                      })
                    },
         
                  });

打开 pages/weather/weather.wxml,添加滚动显示数据相关代码

代码语言:javascript
复制
<view>
      <!-- 未来24小时逐小时天气 -->
      <scroll-view scroll-x="true" class="next">
          <view wx:for="{{twenty_four}}" wx:key="index" class="next-son">
            <view><text>{{tools.splitdaily(item.fxTime)}}</text></view>
            <view><image wx:if="{{item.icon}}" src="../../QWeather-Icons-1.1.1/icons/{{item.icon}}.svg"></image></view>
            <view><text space="nbsp">{{item.temp}} ℃</text></view>
            <view style="margin-top: 15rpx;"><text space="nbsp">{{item.text}}</text></view>
          </view>
      </scroll-view>
  </view>
</view>

完整的样式文件 pages/weather/weather.wxss

代码语言:javascript
复制

/* pages/weather/weather.wxss */


.title {
  display: flex;
  justify-content: center;
  margin-top: 40rpx;
  
}
.title text {
  font-family: sans-serif;
  font-size: 36rpx;
  font-weight: bold;  /*加粗*/
  letter-spacing: 4rpx; /*字符之间的间距*/

}

.search {
  display: flex;
  justify-content: center;
  
}
/* .search-container {
  height: 88rpx;
  background-color: #ada5a5;
  display: flex;
  align-items: center;
  padding: 0 10rpx;
} */

.search-container  {
  font-size: 28rpx;
  margin-top: 20rpx;
  margin-left: 20rpx;
  height: 60rpx;
  width: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;  /*光标居中,在input标签下设置这个即可*/
  border-radius: 8px;
  background: #ffffff;
  box-shadow: inset 5px 5px 10px #cccccc,
              inset -5px -5px 10px #ffffff;
}


.placeho {
  font-size: 22rpx;
  color: rgb(152, 153, 155);
  text-align: center;
}

.search-button {
  margin-left: 60rpx;
  margin-top: 20rpx;
  height: 55rpx;
  width: 120rpx;
  border-radius: 8px;
  background: linear-gradient(145deg, #e6e6e6, #ffffff);
  box-shadow:  18px 18px 24px #e2e0e0,
              -18px -18px 24px #ffffff;
  display: flex;
  /* flex-wrap: wrap; */
  justify-content: center;
}

.search-button view {
  font-size: 28rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.search-button image{
  height: 34rpx;
  width: 34rpx;
  margin-left: 5rpx;
}

.search-button text{
  font-size: 26rpx;
}

/* 热门城市 样式*/
.cards {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}
.cards__item {
  display: flex;
  flex-basis: 25%;
  /* padding-left: 8px;
  padding-right: 8px; */
  margin: 20rpx 0 0 20rpx;
  justify-content: center;
  align-items: center;
  height: 60rpx;
  /* width: 1000rpx; */
  border: 1rpx solid rgb(211, 210, 210);
  font-size: 24rpx;
  border-radius: 8px;
  background: linear-gradient(145deg, #d8d8d8, #ffffff);
  box-shadow:  18px 18px 24px #e2e0e0,
              -18px -18px 24px #ffffff;
}
.choose {
  display: flex;
  flex-basis: 25%;
  /* padding-left: 8px;
  padding-right: 8px; */
  margin: 20rpx 0 0 20rpx;
  justify-content: center;
  align-items: center;
  height: 60rpx;
  /* width: 1000rpx; */
  border: 1rpx solid rgb(211, 210, 210);
  font-size: 24rpx;
  color: white;
  border-radius: 8px;
  background: #0cb5df;
box-shadow:  14px 14px 59px #fcfdfd,
             -14px -14px 59px #e6f0f5;
}

/* 实时天气样式 */
.top-box {
  border-radius: 5px;
  background: #ace3fb;
  box-shadow: inset 6px 6px 12px #92c1d5,
            inset -6px -6px 12px #c6ffff;
  height: 100%;
  margin-left: 20rpx;
  margin-right: 20rpx;
}
.weather-image {
  margin-top: 20rpx;
  
  padding-top: 40rpx;
  /* width: 100%; */
  
  display: flex;
  justify-content: center;
  
}

/* 调整天气图标大小 */
.weather-image image {
  width: 150rpx;
  height: 150rpx;
  filter: drop-shadow(red);
}
/* 调整文本位置 */
.weather-text {
  margin-left: 50rpx;
  display: flex;
  flex-direction: column; 
}

.weather-text text {
   margin: 4rpx;
}
.indices {
  display: flex;
  justify-content: center;
  font-size: 26rpx;
  margin: 50rpx 30rpx 0 30rpx;
}
.next {
  margin-top: 60rpx;
  white-space: nowrap;
  /* display: flex;
  justify-content: center; */
}

.next-son {
  /* align-items: center;
  justify-content: center; */
  width: 20%;
  display: inline-block;
  margin-bottom: 20rpx;
  
}
.next-son text {
  font-size: 26rpx;
}
.next-son image {
  width: 40rpx;
  height: 40rpx;
  margin: 15rpx 0 15rpx 0;
}

.next-son view {
  display: flex;
  flex-direction: column; 
  align-items: center;
  justify-content: center; 
}

/* 未来3天天气的样式 */
.future-3d-father {
  margin-top: 20rpx;
  display: flex;
  /* flex-wrap: wrap; */
  justify-content: center;
  height: 100%;
  margin-left: 20rpx;
  margin-right: 20rpx;
  padding-bottom: 20rpx;  /*里面填充*/
  margin-bottom: 40rpx;  /*外壳距底部*/
  border-radius: 5px;
  background: #ace3fb;
  box-shadow: inset 6px 6px 12px #92c1d5,
            inset -6px -6px 12px #c6ffff;
}
.future-3d-father view {
  
}
.future-3d-son1  {
  margin-top: 40rpx;
  width: 33.33%;
  
  /* height: 100rpx; */
  /* display: flex; */
  
  align-items: center;
  justify-content: center;
}
.future-3d-son2 {
  margin-top: 40rpx;
  width: 33.33%;
  /* height: 220rpx; */
  /* display: flex; */
  align-items: center;
  justify-content: center;
  border-left: 1rpx dashed black;
  border-right: 1rpx dashed black;
}

.future-3d-son3 {
  margin-top: 40rpx;
  width: 33.33%;
  /* height: 100rpx; */
  /* display: flex; */
  align-items: center;
  justify-content: center; 
}

.future-3d-son1 view {
  display: flex;
  flex-direction: column;  /*使元素纵向布局(默认为横向)*/
  align-items: center;
  justify-content: center; 
}

.future-3d-son2 view {
  display: flex;
  flex-direction: column;  /*使元素纵向布局(默认为横向)*/
  align-items: center;
  justify-content: center; 
}

.future-3d-son3 view {
  display: flex;
  flex-direction: column;  /*使元素纵向布局(默认为横向)*/
  align-items: center;
  justify-content: center; 
}

.future-3d-father image {
  width: 50rpx;
  height: 50rpx;
  margin: 15rpx 0 15rpx 0;
}
.banquan {
  height: 20rpx;
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-06-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 冰霜的软测基地 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档