首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >简单天气js天气图标

简单天气js天气图标
EN

Stack Overflow用户
提问于 2016-05-16 12:35:21
回答 1查看 4.3K关注 0票数 1

我正在尝试使用simpleweather制作一个天气应用程序。我已经成功地获得了这个应用程序来显示我当前位置的天气,但我不知道如何让天气图标显示在温度附近。任何帮助都将不胜感激。这是我的代码:http://codepen.io/perrylivingston/pen/dMLLOP,这些是我想要使用的图标:https://github.com/erikflowers/weather-icons

html:

代码语言:javascript
运行
复制
    <div id="the_weather"></div>
    <button class="js-geolocation"> Use Location</button>
    <i class="wi wi-yahoo-31"></i>

css:

代码语言:javascript
运行
复制
body {
background-image: url("http://www.psdgraphics.com/file/blue-sky.jpg");
background-size: cover;
height: 100%;
width: 100%;
}

#the_weather {
width: 500px;
text-align: center;
margin: 0px auto;
text-transform: uppercase;
}

@font-face {
src: url('weathericons-regular-webfont.eot') format ('embedded-opentype');
src: url('weathericons-regular-webfont.svg') format ('svg');
src: url('weathericons-regular-webfont.ttf') format('truetype');
src: url('weathericons-regular-webfont.woff') format('woff');
src: url('weathericons-regular-webfont.woff2') format('woff2');
}

i {
color: #ffffff;
font-size: 160px;
font-weight: normal;
font-style: normal;
}

#the_weather li {
display: block;
font-weight: bold;
padding: 5px;
}

.icon-0:before { content: ":"; }
.icon-1:before { content: "p"; }
.icon-2:before { content: "S"; }
.icon-3:before { content: "Q"; }
.icon-4:before { content: "S"; }
.icon-5:before { content: "W"; }
.icon-6:before { content: "W"; }
.icon-7:before { content: "W"; }
.icon-8:before { content: "W"; }
.icon-9:before { content: "I"; }
.icon-10:before { content: "W"; }
.icon-11:before { content: "I"; }
.icon-12:before { content: "I"; }
.icon-13:before { content: "I"; }
.icon-14:before { content: "I"; }
.icon-15:before { content: "W"; }
.icon-16:before { content: "I"; }
.icon-17:before { content: "W"; }
.icon-18:before { content: "U"; }
.icon-19:before { content: "Z"; }
.icon-20:before { content: "Z"; }
.icon-21:before { content: "Z"; }
.icon-22:before { content: "Z"; }
.icon-23:before { content: "Z"; }
.icon-24:before { content: "E"; }
.icon-25:before { content: "E"; }
.icon-26:before { content: "3"; }
.icon-27:before { content: "a"; }
.icon-28:before { content: "A"; }
.icon-29:before { content: "a"; }
.icon-30:before { content: "A"; }
.icon-31:before { content: "6"; }
.icon-32:before { content: "1"; }
.icon-33:before { content: "6"; }
.icon-34:before { content: "1"; }
.icon-35:before { content: "W"; }
.icon-36:before { content: "1"; }
.icon-37:before { content: "S"; }
.icon-38:before { content: "S"; }
.icon-39:before { content: "S"; }
.icon-40:before { content: "M"; }
.icon-41:before { content: "W"; }
.icon-42:before { content: "I"; }
.icon-43:before { content: "W"; }
.icon-44:before { content: "a"; }
.icon-45:before { content: "S"; }
.icon-46:before { content: "U"; }
.icon-47:before { content: "S"; }

#weather h2 {
margin: 0 0 8px;
color: #fff;
font-size: 100px;
font-weight: 300;
text-align: center;
text-shadow: 0px 1px 3px rgba(0, 0, 0, 0.15);
}

#weather ul {
margin: 0;
padding: 0;
}

#weather li {
background: #fff;
background: rgba(255,255,255,0.90);
padding: 20px;
display: inline-block;
border-radius: 5px;

}

代码语言:javascript
运行
复制
#weather .currently {
margin: 0 20px;
}

button {
display: block;
margin: 15px auto;
width: 140px;
padding: 5px;
background:#efcc3a;
color: #0;
font-weight: bold;
border: 0;
border-radius: 5px;
}

jquery (导航器中的“地理定位”){$(‘..js地理定位’).show();}{${$(‘..js地理定位’).hide()};

代码语言:javascript
运行
复制
$('.js-geolocation').on('click', function() {
navigator.geolocation.getCurrentPosition(function(position) {
loadWeather(position.coords.latitude+','+position.coords.longitude);
});
});


$(document).ready(function() {
loadWeather('New York','');
});

function loadWeather(location, woeid) {
$.simpleWeather({
location: location,
woeid: woeid,
unit: 'f',
success: function(weather) {
html = '<h2><i class="icon-'+weather.code+'"></i>          '+weather.temp+'&deg;'+weather.units.temp+'</h2>';
html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
html += '<li class="currently">'+weather.currently+'</li>';
html += '<li>'+weather.alt.temp+'&deg;C</li></ul>';  

$("#the_weather").html(html);
},
error: function(error) {
$("#the_weather").html('<p>'+error+'</p>');
}
});
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-16 14:23:17

您的代码似乎工作正常。

确保按以下顺序插入库

  1. JQuery
  2. 简单天气

别忘了包括你的天气图标CSS和引导CSS文件

代码语言:javascript
运行
复制
$(document).ready(function() {
  if ("geolocation" in navigator) {
    $('.js-geolocation').show();
  } else {
    $('.js-geolocation').hide();
  }

  // button handler
  $('.js-geolocation').on('click', function() {
    navigator.geolocation.getCurrentPosition(function(position) {
      loadWeather(position.coords.latitude + ',' + position.coords.longitude);
    });
  });
  

  // loads weather
  function loadWeather(location, woeid) {
    $.simpleWeather({
      location: location,
      woeid: woeid,
      unit: 'f',
      success: function(weather) {
        
        html = '<h2><i class="wi wi-yahoo-' + weather.code + '"></i> ' + weather.temp + '&deg;' + weather.units.temp + '</h2>';
        html += '<ul><li>' + weather.city + ', ' + weather.region + '</li>';
        html += '<li class="currently">' + weather.currently + '</li>';
        html += '<li>' + weather.alt.temp + '&deg;C</li></ul>';

        $("#the_weather").html(html);
      },
      error: function(error) {
        $("#the_weather").html('<p>' + error + '</p>');
      }
    });
  }


  // init
  loadWeather('Miami', '');
});
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/3.1.0/jquery.simpleWeather.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/weather-icons/2.0.10/css/weather-icons.css" rel="stylesheet" />

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />

<div id="the_weather"></div>
<button class="js-geolocation">Use Location</button>

<i class="wi wi-night-clear"></i>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37254071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档