html中怎么写一个天气预报,纯CSS实现天气预报

这篇博客介绍了如何使用CSS创建一个天气预报的视觉效果,包括晴天、多云、雨天和雪天的动画。主要通过CSS的伪元素、变换、动画以及box-shadow来实现各种天气的细节,如太阳、云朵、雨滴和雪花的动态效果。
摘要由CSDN通过智能技术生成
CSS实现天气

.weather{

position: relative;

display: inline-block;

width: 180px;

height: 240px;

background: #23b7e5;

border-radius: 8px;

}

.sunny:before{

content: "";

position: absolute;

top: 50%;

left: 50%;

transform: translate(-50%,-50%); /*定位、旋转等*/

width: 60px;

height: 60px;

background: #F6D963;

border-radius: 50%;

box-shadow: 0 0 20px #ff0; /*阴影-外发光效果;水平-上下-大小-颜色*/

z-index: 2;/*设置元素的堆叠顺序。拥有更高堆叠顺序的元素总是会处于堆叠顺序较低的元素的前面;分层显示*/

}

.sunny:after{

content: "";

position: absolute;

top: 50%;

left: 50%;

margin: -45px 0 0 -45px;

width: 90px;

height: 90px;

background: #FFEB3B;

clip-path: polygon(  /*绘制图形*/

50% 0%,

65.43% 25%,

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的示例代码HTML代码: ``` <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>天气预报</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container"> <h1>天气预报</h1> <div class="weather-info"> <div class="location"> <h2>北京</h2> <p>2021年7月21日 星期三</p> </div> <div class="temperature"> <h2>28℃</h2> <p>多云转晴</p> </div> </div> </div> <script src="script.js"></script> </body> </html> ``` CSS代码: ``` body { margin: 0; padding: 0; font-family: Arial, sans-serif; } .container { max-width: 800px; margin: 0 auto; text-align: center; padding: 20px; } .weather-info { display: flex; justify-content: space-between; align-items: center; margin-top: 50px; } .location { flex-grow: 1; margin-right: 20px; } .temperature { flex-grow: 1; margin-left: 20px; } h1 { text-align: center; font-size: 48px; margin-bottom: 50px; } h2 { font-size: 36px; margin-bottom: 10px; } p { font-size: 24px; margin: 0; } ``` JavaScript代码: ``` // 用于获取天气信息的API地址 const weatherApiUrl = "https://api.openweathermap.org/data/2.5/weather?q=北京&appid=YOUR_APP_ID&units=metric"; // 获取天气信息并更新页面 fetch(weatherApiUrl) .then(response => response.json()) .then(data => { const location = document.querySelector(".location h2"); const date = new Date(data.dt * 1000); const temperature = document.querySelector(".temperature h2"); const description = document.querySelector(".temperature p"); location.textContent = data.name; dateString = date.toLocaleDateString('zh-CN', { year: 'numeric', month: 'long', day: 'numeric', weekday: 'long' }); dateElement = document.createElement("p"); dateElement.textContent = dateString; location.appendChild(dateElement); temperature.textContent = `${data.main.temp}℃`; description.textContent = data.weather[0].description; }) .catch(error => console.error(error)); ``` 请注意,这个示例代码的 `YOUR_APP_ID` 需要替换为你自己的 OpenWeatherMap API Key。此外,这个示例代码只获取了北京的天气信息,如果需要获取其他城市的天气信息,需要修改 `weatherApiUrl` 的 `q` 参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值