在基础实时天气的基础上,展示未来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;
}