当前位置:   article > 正文

HTML、CSS和JavaScript实现简单天气预报_html5代码编写天气预报

html5代码编写天气预报

使用HTML、CSS和JavaScript实现简单天气预报的步骤:

  1. 首先需要获取天气API的数据,可以通过向第三方天气数据服务商发送HTTP请求来获取数据。例如,可以使用Yahoo Weather API或OpenWeatherMap API等。这里以OpenWeatherMap API为例,获取当前城市的天气情况。

  2. 接着,将获取到的天气数据动态地展示在HTML页面上。可以使用JavaScript的DOM操作方法,将获取到的数据渲染到页面上指定的位置。

  3. 最后,为了美化界面,可以使用CSS对整个天气预报页面进行样式设置。


 

下面是具体的代码实现:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>天气预报</title>
  6. <link rel="stylesheet" href="style.css">
  7. </head>
  8. <body>
  9. <div class="weather">
  10. <span class="city"></span>
  11. <span class="temp"></span>
  12. <span class="description"></span>
  13. <img class="icon"/>
  14. </div>
  15. <script src="main.js"></script>
  16. </body>
  17. </html>
  1. .weather {
  2. width: 300px;
  3. height: 150px;
  4. background-color: #eee;
  5. border-radius: 10px;
  6. text-align: center;
  7. margin: 50px auto;
  8. padding: 20px;
  9. }
  10. .city {
  11. font-size: 25px;
  12. font-weight: bold;
  13. display: block;
  14. margin-bottom: 15px;
  15. }
  16. .temp {
  17. font-size: 18px;
  18. font-weight: bold;
  19. display: block;
  20. margin-bottom: 10px;
  21. }
  22. .description {
  23. font-size: 16px;
  24. display: block;
  25. margin-bottom: 15px;
  26. }
  27. .icon {
  28. width: 50px;
  29. height: auto;
  30. margin-top: 10px;
  31. }
  1. let city = "北京"; // 获取天气的城市
  2. let apiKey = "your_api_key"; // 替换为你自己的API Key
  3. let url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&lang=zh_cn`;
  4. fetch(url)
  5. .then(response => response.json())
  6. .then(data => {
  7. console.log(data);
  8. let cityName = data.name;
  9. let temperature = Math.round(data.main.temp - 273.15);
  10. let description = data.weather[0].description;
  11. let iconCode = data.weather[0].icon;
  12. document.querySelector(".city").textContent = cityName;
  13. document.querySelector(".temp").textContent = `${temperature}°C`;
  14. document.querySelector(".description").textContent = description;
  15. document.querySelector(".icon").setAttribute("src", `http://openweathermap.org/img/w/${iconCode}.png`);
  16. })
  17. .catch(err => {
  18. console.log(err);
  19. });

 


上面的代码中,先定义了要获取天气数据的城市和API Key,在JavaScript中使用fetch方法发送HTTP请求,获取数据后再使用DOM操作将数据渲染到HTML页面上对应的元素中。

如果使用Vue.js框架开发,则可以更加简便地实现天气预报功能,具体步骤如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>天气预报</title>
  6. <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
  7. <link rel="stylesheet" href="style.css">
  8. </head>
  9. <body>
  10. <div id="app">
  11. <div class="weather">
  12. <span class="city">{{ city }}</span>
  13. <span class="temp">{{ temperature }}°C</span>
  14. <span class="description">{{ description }}</span>
  15. <img :src="iconUrl"/>
  16. </div>
  1. var app = new Vue({
  2. el: '#app',
  3. data: {
  4. city: "北京",
  5. apiKey: "your_api_key",
  6. temperature: "",
  7. description: "",
  8. iconCode: ""
  9. },
  10. methods: {
  11. getWeatherData: function() {
  12. let url = `https://api.openweathermap.org/data/2.5/weather?q=${this.city}&appid=${this.apiKey}&lang=zh_cn`;
  13. fetch(url)
  14. .then(response => response.json())
  15. .then(data => {
  16. console.log(data);
  17. this.temperature = Math.round(data.main.temp - 273.15);
  18. this.description = data.weather[0].description;
  19. this.iconCode = data.weather[0].icon;
  20. })
  21. .catch(err => {
  22. console.log(err);
  23. });
  24. }
  25. },
  26. computed: {
  27. iconUrl: function() {
  28. return `http://openweathermap.org/img/w/${this.iconCode}.png`;
  29. }
  30. },
  31. mounted: function() {
  32. this.getWeatherData();
  33. }
  34. });

在上面的代码中,我们使用了Vue.js的data属性来存储需要展示的数据和API Key信息。通过Vue.js的methods属性定义一个获取天气数据的方法,并通过computed属性计算图片地址,最后使用mounted属性在页面加载时自动调用获取天气数据的方法。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/906559
推荐阅读
相关标签
  

闽ICP备14008679号