- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
screen shot of app images and code
我在使用 CSS 水平对齐映射列表时遇到问题。我想让每天的天气看起来像左边的那个。这些列表是在 DOM、Vanilla JS 中创建的。 将所有映射列表包装在一起。我怎样才能像下面所附的图片那样每天包装它们?
This is the result code I expected to have
请帮帮我。提前感谢您的帮助。
(function () {
const GOOGLE_MAPS_API_URL = 'https://maps.googleapis.com/maps/api/geocode/json';
const CORS_PROXY = 'https://cors-anywhere.herokuapp.com/';
const DARKSKY_API_URL = 'https://api.darksky.net/forecast/';
const WEATHER_MAP_API_URL = 'http://api.openweathermap.org/data/2.5/';
const GOOGLE_MAPS_API_KEY = 'GOOGLE_MAPS_API_KEY';
const DARKSKY_API_KEY = 'DARKSKY_API_KEY';
const WEATHER_MAP_API_KEY = 'WEATHER_MAP_API_KEY';
/* --- Get Coodrdinates For City --- */
function getCoordinatesForCity(cityName) {
const url = `${GOOGLE_MAPS_API_URL}?address=${cityName}&key=${GOOGLE_MAPS_API_KEY}`;
return (
fetch(url)
.then(response => response.json())
.then(data => {
const {
geometry,
address_components
} = data.results[0]
return {
cityName: address_components[0].long_name,
latitude: geometry.location.lat,
longitude: geometry.location.lng
};
})
);
}
/* --- Get Weather Data --- */
function getWeather(cityInfo) {
const {
cityName,
latitude,
longitude
} = cityInfo
const url = `${CORS_PROXY}${DARKSKY_API_URL}${DARKSKY_API_KEY}/${latitude},${longitude}?units=si&exclude=minutely,hourly,alerts,flags`;
return (
fetch(url)
.then(response => response.json())
.then(data => {
return {
cityName: cityName,
weather: data.currently,
daily: data.daily.data
};
})
);
}
const app = document.querySelector('#app');
const cityForm = app.querySelector('.city-form');
const cityInput = cityForm.querySelector('.city-input');
const conHeader = app.querySelector('.contents-header');
const currentSummary = app.querySelector('.current-summary');
const currentRight = app.querySelector('.current-right');
const currentLeft = app.querySelector('.current-left');
const dailyList = app.querySelector('.daily-list');
/* --- Display Current Weather Icon --- */
function getCurrentIcon(result) {
const { icon } = result.weather;
const wIcon = document.createElement('li');
wIcon.innerHTML = '<i></i>';
wIcon.setAttribute('id', 'c-icon');
currentSummary.appendChild(wIcon);
if(icon ==='clear-day') {
return wIcon.setAttribute('class', 'wi wi-day-sunny');
}
else if(icon === 'clear-night') {
return wIcon.setAttribute('class', 'wi wi-night-clear');
}
else if(icon === 'rain') {
return wIcon.setAttribute('class', 'wi wi-rain');
}
else if(icon === 'snow') {
return wIcon.setAttribute('class', 'wi wi-snow');
}
else if(icon === 'sleet') {
return wIcon.setAttribute('class', 'wi wi-sleet');
}
else if(icon === 'wind') {
return wIcon.setAttribute('class', 'wi wi-windy');
}
else if(icon === 'fog') {
return wIcon.setAttribute('class', 'wi wi-fog');
}
else if(icon === 'cloudy') {
return wIcon.setAttribute('class', 'wi wi-cloudy');
}
else if(icon === 'partly-cloudy-day') {
return wIcon.setAttribute('class', 'wi wi-day-cloudy');
}
else if(icon === 'partly-cloudy-night') {
return wIcon.setAttribute('class', 'wi wi-night-partly-cloudy');
}
else if(icon === 'hail') {
return wIcon.setAttribute('class', 'wi wi-hail');
}
else if(icon === 'thunderstorm') {
return wIcon.setAttribute('class', 'wi wi-thunderstorm');
}
else if(icon === 'tornado') {
return wIcon.setAttribute('class', 'wi wi-tornado');
}
}
/* --- Display Current Weather Data --- */
function displayCurrentWeather(result) {
const {
apparentTemperature,
pressure,
humidity,
summary,
temperature,
uvIndex,
windSpeed,
visibility
} = result.weather;
//convert wind speed unit, m/s -> km/h
const convertedWindSpeed = Math.round(windSpeed * 3.6);
// <div class="contents-header"></div>
const wCity = document.createElement('H2');
wCity.appendChild(document.createTextNode(result.cityName));
const wSummary = document.createElement('li');
wSummary.appendChild(document.createTextNode(summary));
conHeader.appendChild(wCity);
conHeader.appendChild(wSummary);
// <div class="current-summary"></div>
const wTemp = document.createElement('li');
wTemp.appendChild(document.createTextNode(temperature + ' °C'));
wTemp.setAttribute('class', 'w-temperature');
currentSummary.appendChild(wTemp);
// <div class="current-list"></div>
const wApparentTemp = document.createElement('li');
wApparentTemp.innerHTML = '<span class="current-span">FEELS LIKE</span> <br>' + apparentTemperature + ' °';
const wHumidity = document.createElement('li');
wHumidity.innerHTML = '<span class="current-span">HUMIDITY</span> <br>' + humidity + '%';
const wWind = document.createElement('li');
wWind.innerHTML = '<span class="current-span">WIND</span> <br>' + convertedWindSpeed + ' Km/h';
const wUv = document.createElement('li');
wUv.innerHTML = '<span class="current-span">UV INDEX</span> <br>' + uvIndex + ' of 10';
const wPressure = document.createElement('li');
wPressure.innerHTML = '<span class="current-span">PRESSURE</span> <br>' + pressure + ' hPa';
const wVisibility = document.createElement('li');
wVisibility.innerHTML = '<span class="current-span">VISIBILITY</span> <br>' + visibility + ' Km';
currentLeft.appendChild(wApparentTemp);
currentLeft.appendChild(wHumidity);
currentLeft.appendChild(wWind);
currentRight.appendChild(wUv);
currentRight.appendChild(wPressure);
currentRight.appendChild(wVisibility);
// weather list border line
document.getElementById('current-list').style.borderTop = "1px solid #333";
document.getElementById('current-list').style.borderBottom = "1px solid #333";
}
/* --- Display Dates --- */
function getDayNames(unixTime) {
const timeStamp = unixTime
const d = new Date(timeStamp * 1000)
const days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saterday', 'Sunday']
const dayName = days[d.getDay()]
// To get 'Mon', 'Tue'..
//const dayName = d.toString().split(' ')[0];
const date = document.createElement('li');
date.appendChild(document.createTextNode(dayName));
dailyList.appendChild(date);
}
/* --- Display Daily Weather Icon --- */
function getDailyIcon(icon) {
const dIcon = document.createElement('li');
dIcon.innerHTML = '<i></i>';
dIcon.setAttribute('id', 'd-icon');
dailyList.appendChild(dIcon);
if(icon ==='clear-day') {
return dIcon.setAttribute('class', 'wi wi-day-sunny');
}
else if(icon === 'clear-night') {
return dIcon.setAttribute('class', 'wi wi-night-clear');
}
else if(icon === 'rain') {
return dIcon.setAttribute('class', 'wi wi-rain');
}
else if(icon === 'snow') {
return dIcon.setAttribute('class', 'wi wi-snow');
}
else if(icon === 'sleet') {
return dIcon.setAttribute('class', 'wi wi-sleet');
}
else if(icon === 'wind') {
return dIcon.setAttribute('class', 'wi wi-windy');
}
else if(icon === 'fog') {
return dIcon.setAttribute('class', 'wi wi-fog');
}
else if(icon === 'cloudy') {
return dIcon.setAttribute('class', 'wi wi-cloudy');
}
else if(icon === 'partly-cloudy-day') {
return dIcon.setAttribute('class', 'wi wi-day-cloudy');
}
else if(icon === 'partly-cloudy-night') {
return dIcon.setAttribute('class', 'wi wi-day-cloudy')
}
else if(icon === 'hail') {
return dIcon.setAttribute('class', 'wi wi-hail');
}
else if(icon === 'thunderstorm') {
return dIcon.setAttribute('class', 'wi wi-thunderstorm');
}
else if(icon === 'tornado') {
return dIcon.setAttribute('class', 'wi wi-tornado');
}
}
/* --- Display Daily Weather Data --- */
function displayDailySummary(result) {
const dailyWeather = result.daily
.map( data => {
return {
time: data.time,
icon: data.icon,
tempMax: data.apparentTemperatureMax,
tempMin: data.apparentTemperatureMin
}
})
.forEach( data => {
getDayNames(data.time);
getDailyIcon(data.icon);
// <div class="daily-list"></div>
const maxTemp = document.createElement('li');
maxTemp.appendChild(document.createTextNode( data.tempMax + ' °'));
const minTemp = document.createElement('li');
minTemp.appendChild(document.createTextNode(data.tempMin + ' °'));
dailyList.appendChild(maxTemp);
dailyList.appendChild(minTemp);
})
}
/* --- Get Background Image by Temperature --- */
function getBackground(result) {
const { temperature } = result.weather;
const backgroundImg = document.getElementById('section-weather');
if (temperature > 20) {
return backgroundImg.style.backgroundImage = "url('resources/css/img/clear-sky.png')";
}
if (temperature > 0) {
return backgroundImg.style.backgroundImage = "url('resources/css/img/cloud.png')";
}
if (temperature <= 0) {
return backgroundImg.style.backgroundImage = "url('resources/css/img/snow.png')";
}
}
/* --- Get Users Position --- */
function getUserPosition() {
return new Promise(function(resolve, reject) {
navigator.geolocation.getCurrentPosition(resolve, reject);
});
}
/* --- Get Users City Name --- */
function getUserCityName(position) {
const lat = position.coords.latitude;
const lng = position.coords.longitude;
//Used another weather open source API to get the proper cityName
const url = `${WEATHER_MAP_API_URL}weather?lat=`+lat+`&lon=`+lng+`&appid=${WEATHER_MAP_API_KEY}`
return (
fetch(url)
.then(response => response.json())
.then(data => {
return {
cityName: data.name,
latitude: lat,
longitude: lng
};
})
.catch(error => console.log("Something went wrong!"))
);
}
/* --- Confirm Geo Location Service With Users --- */
function geolocationService() {
const answer = confirm("You would like to share your location?")
if(answer) {
getUserPosition()
.then(getUserCityName)
.then(getWeather)
.then(displayWeather)
.catch(error => console.log("Something went wrong!"))
}
else {
// do nothing
}
}
/* --- Call Alert Box on Page Load to Confirm Geo Location Service --- */
geolocationService();
/* --- Call Display Weather Functions --- */
function displayWeather(result) {
cityInput.value = ''; // clear the input box
conHeader.innerHTML = ''; // clear the weather contents for new search
currentSummary.innerHTML = '';
currentRight.innerHTML = '';
currentLeft.innerHTML = '';
dailyList.innerHTML = '';
const promises = [
getCurrentIcon(result),
displayCurrentWeather(result),
displayDailySummary(result),
getBackground(result)
]
Promise.all(promises);
}
/* --- Search EventListener --- */
cityForm.addEventListener('submit', function (e) {
e.preventDefault(); // prevent the form from submitting
const city = cityInput.value;
if(!city) {
alert("Please enter a city name")
}
else {
getCoordinatesForCity(city)
.then(getWeather)
.then(displayWeather)
.catch(error => console.log("Something went wrong!"))
}
});
})();
// wrapping the code in an IIFE to prevent it from polluting the global scope
/* ---------------------------------------- */
/* BASIC SETUP */
/* ---------------------------------------- */
html {
min-height: 100%;
position: relative;
}
* {
margin: 0;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
body {
font-size: 16px;
font-weight: 300;
font-family: 'Montserrat', sans-serif;
text-rendering: optimizeLegibility;
background: #fff;
height: 100%;
color: #252839;
}
/* ---------------------------------------- */
/* HEADER */
/* ---------------------------------------- */
header {
background: #333;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
}
h1 {
color: #fff;
font-size: 2.0em;
padding: 5px;
background: #f2b632;
}
.h1-span {
font-size: .6em;
}
/*- SOCIAL ICONS -*/
.social-links {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
padding-top: 9px;
padding-bottom: 7px;
list-style: none;
}
.social-links li {
margin-right: 10px;
}
.icon-portfolio,
.icon-linkedin,
.icon-github {
font-size: 1.7em;
color: #fff;
text-align: center;
-webkit-transition: color .2s;
transition: color .2s;
}
.icon-portfolio {
margin-right: .1em;
}
.icon-portfolio:hover {
color: #dd4b39;
}
.icon-linkedin:hover {
color: #007bb5;
}
.icon-github:hover {
color: #fae596;
}
/* ---------------------------------------- */
/* SECTION */
/* ---------------------------------------- */
#section-weather {
background-size: cover;
background-repeat: no-repeat;
display: block;
}
/* --- SEARCH BOX --- */
.city-form {
padding: 40px;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
.city-input {
font-size: 1em;
font-family: 'Montserrat', sans-serif;
height: 1.6em;
width: auto;
color: #6d6a6a;
-webkit-box-flex: 2;
-ms-flex-positive: 2;
flex-grow: 0;
border: 1px solid #6d6a6a;
}
.btn {
font-size: 1em;
font-family: 'Montserrat', sans-serif;
height: 1.6em;
width: auto;
background: #f2b632;
border: 1px solid #f2b632;
color: #6d6a6a;
-webkit-transition: background .2s;
-webkit-transition: background .2s, border .2s;
transition: background .2s, border .2s;
}
.btn:hover,
.btn:active {
background: #e6cf8b;
border: 1px solid #e6cf8b;
}
*:focus { outline: none; }
/* --- CONTENTS --- */
.contents-header {
list-style: none;
text-align: center;
padding-bottom: 30px;
}
h2 {
text-align: center;
font-size: 2em;
font-weight: 200;
}
.current-summary {
list-style: none;
padding-left: 0;
padding-bottom: 32px;
text-align: center;
}
.current-summary li {
padding-bottom: 8px;
}
#c-icon {
font-size: 3.3em;
color: #252839;
padding-bottom: 12px;
}
#d-icon {
font-size: 1.7em;
color: #252839;
}
.w-temperature {
font-size: 3.0em;
font-weight: 200;
}
#current-list {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-around;
font-size: 1.2em;
}
.current-left,
.current-right {
list-style: none;
word-spacing: 1px;
letter-spacing: 1px;
padding-left: 0;
padding: 15px;
}
.current-span {
font-size: .6em;
font-weight: ;
color: #6d6a6a;
}
.daily-list {
list-style: none;
padding: 30px;
}
.daily-list li {
}
/* ---------------------------------------- */
/* FOOTER */
/* ---------------------------------------- */
footer {
background: #333;
}
h3 {
font-size: .9em;
color: #fff;
text-align: center;
padding: 13px;
}
.h3-span {
color: #f2b632;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Weather App</title>
<!-- any <link> tags that are not our own CSS appear before our own CSS so that our CSS will be more specific with respect to the cascade -->
<!-- viewport: to set the effective width to the same as the screen width -->
<!-- viewport: to disable pinch-zoom, add <maximum-scale=1.0> -->
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta name="description" content="Local weather application.">
<link rel="normalize" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/7.0.0/normalize.min.css">
<link rel="stylesheet" type="text/css" href="http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<link rel="stylesheet" type="text/css" href="vendors/css/weather-icons.min.css">
<link type="text/css" href="https://fonts.googleapis.com/css?family=Montserrat:200i,300,300i,400" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="resources/css/style.css">
<link rel="stylesheet" type="text/css" href="resources/css/queries.css">
</head>
<body>
<div id="app">
<header>
<h1>hJ <span class="h1-span">weather</span></h1>
<ul class="social-links">
<li>
<a href="" target="_blank"><i class="ion-ios-briefcase-outline icon-portfolio" title="portfolio"></i></a></li>
<li>
<a href="https://www.linkedin.com/in/jin827/" target="_blank"><i class="ion-social-linkedin-outline icon-linkedin" title="linkedin"></i></a></li>
<li>
<a href="https://github.com/Jin827/weather-app" target="_blank"><i class="ion-social-github-outline icon-github" title="github"></i></a></li>
</ul>
</header>
<section id="section-weather">
<!-- SEARCH BOX -->
<div class="search-box">
<form class="city-form">
<input type="text" class="city-input" placeholder="Search for location">
<button class="btn">search</button>
<!-- <i class="fa fa-search fa-2x" aria-hidden="true"></i>-->
</form>
</div>
<!-- CONTENTS -->
<div class="contents-box">
<!-- the content diplayed here is generated by DOM operations :) -->
<div class="contents-header">
</div>
<div>
<ul class="current-summary">
</ul>
</div>
<div id="current-list">
<ul class="current-left">
</ul>
<ul class="current-right">
</ul>
</div>
<div>
<ul class="daily-list">
</ul>
</div>
</div>
</section>
<!-- FOOTER -->
<footer>
<h3> Responsive CSS Flex-box <span class="h3-span">Hyojin Lee</span></h3>
</footer>
</div>
<script type="text/javascript" src="resources/app.js"></script>
</body>
</html>
最佳答案
我用 calc(100%/4) 和 flex wrap, space-around 解决了问题。
关于javascript - 水平对齐映射列表(vanilla js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48157196/
我希望能够像在 jsFiddle 中那样将元素列表对齐到右侧的复选框。这是如何做到这一点的最佳实践?传统上我从来没有 float 过相互嵌套的元素,所以我想确定这是否是解决此问题的正确方法。 代码(h
指令.align n是什么意思在数组中做什么? 更具体地说,假设我有以下部分代码: array: .align 2 .space 800 它的重要性是什么,为什么不跳过它并使用
基本上我正在寻找一种强制特定相对对齐的方法 即我想保证其他一些值(value) m s.t m > n alignment_of(foo) % 2^m == 2^n IE: .align 2^m; .
在我的代码中,我必须考虑一个数组数组,其中内部数组具有固定维度。为了使用 STL 算法,将数据实际存储为数组的数组很有用,但我还需要将该数据传递给 C 库,该库采用扁平化的 C 样式数组。 如果能够以
横向上,我想显示两个位图,并在它们之间显示一个标签字段。 代码看起来很简单,但所有字段都添加在屏幕左侧。 HorizontalFieldManager hfm = new HorizontalFiel
我想绘制一个变量名称及其符号。因为某些变量的名称很长,所以我试图将换行符与轴标签混合使用。这会导致对齐中发生有趣的事情: par(mar=c(1,12,1,1)) plot( y=1:6, 1:6,
使用这个脚本 df <- data.frame(x = 1:5, y = 1:5, color = letters[1:5]) ggplot(df, aes(x, y, fill = color))
我有一个带有标量字段的结构,比如妈妈,我想在屏幕上对齐的列中显示结构的值,可能还有一些标题。这是一个最小的工作示例: mom.a = 1; mom.b = 2; mom.veryLongName =
在 iOS6 中,我使用自动布局。 我有 2 个以编程方式创建的 View v1 和 v2。 v2 作为 subview 添加到 v1 v1 的约束已通过编程方式创建(此处未显示)。 我希望 v1 和
概述 浏览时operator new, operator new[] - cppreference.com ,似乎我们有许多选项来分配具有特定对齐要求的对象数组。但是,没有指定如何使用它们,而且我似乎
Widget _createProfileContainer() { return new Container( height: 64.0, child: ne
我正在使用 Bootstrap 和语义 UI 的组合来设计和对齐我的网页。目前,我在将页面 api map 和博客文章在整个页面上对齐时遇到问题,而不是像图像所示 那样堆叠在一起。 这是我的底层代码,
所以我已经添加了标签和所有内容,但我仍然在格式化和对齐所有内容时遇到问题。计算按钮显然应该居中。我知道使用 gridbag 将框架分割成坐标系,当一列大于其他列时,它会调整其他列并将其抛弃(对吗?)。
我必须将程序上的按钮对齐到中间,我运行的当前代码但显示的按钮与程序一样大,我想要一个特定大小的中心按钮,这是我尝试过的 /** * Created by Timk9 on 11/04/2016.
我正在尝试将 VIM 作为我的 ruby/rails 编辑器。太胖了,我对它的功能印象深刻 并且我能够安装以下插件以提供更好的 IDE 体验 自动配对 Better-snipmate-snippe
在结构内对齐成员的最佳或常规方法是什么?添加虚拟数组是最佳解决方案吗? 我有一个 double 的结构和 double 的三倍是吗? struct particle{ double mass;
我正在尝试对齐我的输出,但由于某种原因我无法做到我多么想要它,这真的很令人沮丧。标题不会正确对齐。我不知道我是否正确使用了 setw()。 #include using std::cout; usi
我正在开发一个 android 应用程序,其相对布局如下所示。 这是应用程序在屏幕上的显示方式的 imgur 链接:http://imgur.com/c4rNJ .我希望“Text 1”出现在“a l
我不确定为什么我不能在下面的代码中调整按钮的位置。我有几行设置了边界,但我一定遗漏了一些东西。 public DayGUI() { mainFrame = new JF
我有一个 html 页面,我想在页面底部对齐一个 iframe,使 iframe 占据所有宽度,我无法在底部对齐 iframe。请找到底部的 iframe 标签页面。 The rest of th
我是一名优秀的程序员,十分优秀!