要实现的效果如下图:

HTML代码如下:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="common.css"/>
</head>
<body>
    <div class="weather-container">
        <canvas id="myCanvas" width="640" height="150"></canvas>
        <a class="oneDayWeather pastDay" href="http://www.sinohuaxia.cn" target="_blank">
            <div class="weekDay">周二</div>
            <div class="curWeather">阵雨</div>
            <div class="curWeatherPic"></div>
            <div class="highTempNum" id="highTempNum0">32°</div>
            <div class="highTemp" id="highTemp0"></div>
            <div class="lowTemp" id="lowTemp0"></div>
            <div class="lowTempNum" id="lowTempNum0">20°</div>
            <div class="dayWeatherPic"></div>
            <div class="dayWeather">雷阵雨</div>
            <div class="dayDate">06/09</div>
        </a>
        <a class="oneDayWeather pastDay" href="http://www.sinohuaxia.cn" target="_blank">
            <div class="weekDay">周二</div>
            <div class="curWeather">阵雨</div>
            <div class="curWeatherPic"></div>
            <div class="highTempNum" id="highTempNum1">29°</div>
            <div class="highTemp" id="highTemp1"></div>
            <div class="lowTemp" id="lowTemp1"></div>
            <div class="lowTempNum" id="lowTempNum1">24°</div>
            <div class="dayWeatherPic"></div>
            <div class="dayWeather">雷阵雨</div>
            <div class="dayDate">06/09</div>
        </a>
        <a class="oneDayWeather" href="http://www.sinohuaxia.cn" target="_blank">
            <div class="weekDay">周二</div>
            <div class="curWeather">阵雨</div>
            <div class="curWeatherPic"></div>
            <div class="highTempNum" id="highTempNum2">32°</div>
            <div class="highTemp" id="highTemp2"></div>
            <div class="lowTemp" id="lowTemp2"></div>
            <div class="lowTempNum" id="lowTempNum2">22°</div>
            <div class="dayWeatherPic"></div>
            <div class="dayWeather">雷阵雨</div>
            <div class="dayDate">06/09</div>
        </a>
        <a class="oneDayWeather" href="http://www.sinohuaxia.cn" target="_blank">
            <div class="weekDay">周二</div>
            <div class="curWeather">阵雨</div>
            <div class="curWeatherPic"></div>
            <div class="highTempNum" id="highTempNum3">28°</div>
            <div class="highTemp" id="highTemp3"></div>
            <div class="lowTemp" id="lowTemp3"></div>
            <div class="lowTempNum" id="lowTempNum3">9°</div>
            <div class="dayWeatherPic"></div>
            <div class="dayWeather">雷阵雨</div>
            <div class="dayDate">06/09</div>
        </a>
        <a class="oneDayWeather" href="http://www.sinohuaxia.cn" target="_blank">
            <div class="weekDay">周二</div>
            <div class="curWeather">阵雨</div>
            <div class="curWeatherPic"></div>
            <div class="highTempNum" id="highTempNum4">31°</div>
            <div class="highTemp" id="highTemp4"></div>
            <div class="lowTemp" id="lowTemp4"></div>
            <div class="lowTempNum" id="lowTempNum4">20°</div>
            <div class="dayWeatherPic"></div>
            <div class="dayWeather">雷阵雨</div>
            <div class="dayDate">06/09</div>
        </a>
        <a class="oneDayWeather" href="http://www.sinohuaxia.cn" target="_blank">
            <div class="weekDay">周二</div>
            <div class="curWeather">阵雨</div>
            <div class="curWeatherPic"></div>
            <div class="highTempNum" id="highTempNum5">28°</div>
            <div class="highTemp" id="highTemp5"></div>
            <div class="lowTemp" id="lowTemp5"></div>
            <div class="lowTempNum" id="lowTempNum5">12°</div>
            <div class="dayWeatherPic"></div>
            <div class="dayWeather">雷阵雨</div>
            <div class="dayDate">06/09</div>
        </a>
    </div>
    
    <script type="text/javascript">
    var canvas=document.getElementById('myCanvas');
    var canvasTop=canvas.offsetTop;
    var ctx=canvas.getContext('2d');
    var sixdayHighTemperature=[32,29,32,28,31,28];
    var sixdayLowTemperature=[20,24,22,9,20,12];
    var picHeight=150;
    var maxHighTemp=maxNum(sixdayHighTemperature);
    var minLowTemp=minNum(sixdayLowTemperature);
    var maxRange=maxHighTemp-minLowTemp;
    function maxNum(arr){
        var maxNum=0;
        if(arr.length){
            for(var i=0,len=arr.length;i<len;i++){
                if(i==0){
                    maxNum=arr[0];
                }else if(maxNum<arr[i]){
                    maxNum=arr[i];
                }
            }
        }
        return maxNum;
    }
    function minNum(arr){
        var minNum=0;
        if(arr.length){
            for(var i=0,len=arr.length;i<len;i++){
                if(i==0){
                    minNum=arr[0];
                }else if(minNum>arr[i]){
                    minNum=arr[i];
                }
            }
        }
        return minNum;
    }
    ctx.strokeStyle="#fff";
    ctx.lineWidth=2;
    (function drawHighTemp(temps){
            var drawX=0,drawY=0;
            var distance=Math.floor(picHeight/maxRange);
            for(var i=0,len=temps.length;i<len;i++){
                drawX=i*106+53;
                drawY=(maxHighTemp-temps[i])*distance;
                document.getElementById('highTemp'+i).style.top=(canvasTop+drawY-5)+"px";
                document.getElementById('highTempNum'+i).style.top=(canvasTop+drawY-25)+"px";
                if(i==0){
                    ctx.moveTo(drawX,drawY);
                }else{
                    ctx.lineTo(drawX,drawY);
                }
            }
            ctx.stroke();
        })(sixdayHighTemperature);
        (function drawHighTemp(temps){
            var drawX=0,drawY=0;
            var distance=Math.floor(picHeight/maxRange);
            for(var i=0,len=temps.length;i<len;i++){
                drawX=i*106+53;
                drawY=picHeight-((temps[i]-minLowTemp)*distance);
                document.getElementById('lowTemp'+i).style.top=(canvasTop+drawY-5)+"px";
                document.getElementById('lowTempNum'+i).style.top=(canvasTop+drawY+10)+"px";
                if(i==0){
                    ctx.moveTo(drawX,drawY);
                }else{
                    ctx.lineTo(drawX,drawY);
                }
            }
            ctx.stroke();
        })(sixdayLowTemperature)
    
    </script>
</body>
</html>

CSS代码如下:

body {
    background: url(img/bg_night_sunny.jpg) no-repeat;
    margin: 0;
    padding: 0;
}
.weather-container{
    width:640px;
    height:360px;
    position:relative;
}
.oneDayWeather{
    position: relative;
    overflow: hidden;
    width:105px;
    height:360px;
    float:left;
    text-align: center;
    line-height: 20px;
    color:#fff;
    border-right:1px solid rgba(255,255,255,0.25);
}
.oneDayWeather:last-child{
    border:none;
}
.oneDayWeather:active{
    background:rgba(0,0,0,0.2);
}
.pastDay{
    opacity: 0.6;
}
.weekDay{
    position: absolute;
    left:0;
    top:0;
    width:100%;
}
.curWeather{
    position: absolute;
    left:0;
    top:20px;
    width:100%;
}
.curWeatherPic{
    position: absolute;
    left:0;
    top:40px;
    width:100%;
    height:30px;
    background:url(img/w1.png) center 0 no-repeat;
    background-size:contain;
}
.highTemp,.lowTemp{
    position: absolute;
    left:0;
    top:-240px;
    width:100%;
    height:10px;
    background: url(img/splash_indicator_focused.png) center 0 no-repeat;
}
.highTempNum,.lowTempNum{
    position: absolute;
    left:0;
    top:-20em;
    width:100%;
    height:20px;
    text-indent: 15px
}
.dayWeatherPic{
    position: absolute;
    left:0;
    bottom:40px;
    width:100%;
    height:30px;
    background:url(img/w2.png) center 0 no-repeat;
    background-size:contain;
}
.dayWeather{
    position: absolute;
    left:0;
    bottom:20px;
    width:100%;
}
.dayDate{
    position: absolute;
    left:0;
    bottom:0;
    width:100%;
}
#myCanvas{
    position:absolute;
    top:105px;
    left:0;
}

Canvas制作天气预报走势图的更多相关文章

  1. Html5 canvas 绘制彩票走势图

    因须要 要实现一个类似彩票走势图的功能,初次学Html5 ,非常多地方不明白,前段时间也发帖请教过这个问题.也是没给个明白说话,在网上搜了非常多,也没有实现的样例,今天细致研究了下.发现事实上也不是非 ...

  2. canvas制作柱形图/折线图/饼状图,Konva写动态饼状图

    制作饼状图 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  3. WebService学习--股票走势图+天气预报实现

        互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取股票数据和天气预报为例进行学习 ...

  4. 用Canvas制作简单的画图工具

    今天用Canvas制作了一个画图工具,非常简单,功能也不是很多,主要有背景网格,画线,画圆,画矩形和画圆角矩形,也用到了canvas的一些基本知识,在这里一一列举. 1.线段的绘制: 如何绘制真正的1 ...

  5. Vue+WebSocket+ES6+Canvas 制作「你画我猜」小游戏

    Vue+WebSocket+ES6+Canvas 制作「你画我猜」小游戏 转载 来源:jrainlau 链接:https://segmentfault.com/a/1190000005804860 项 ...

  6. HTML5 canvas制作童年的回忆大风车

    今天看到一篇CSS3写的大风车http://www.cnblogs.com/yaojaa/archive/2013/01/30/2882521.html,感觉CSS3太神奇了,这在以前用CSS是想都不 ...

  7. 使用canvas制作的移动端color picker

    使用canvas制作的移动端color picker 项目演示地址(用手机或者手机模式打开) 我在另一个中demo,需要用到color picker,但是找不到我需要的移动端color picker, ...

  8. 【转】使用Python matplotlib绘制股票走势图

    转载出处 一.前言 matplotlib[1]是著名的python绘图库,它提供了一整套绘图API,十分适合交互式绘图.本人在工作过程中涉及到股票数据的处理如绘制K线等,因此将matplotlib的使 ...

  9. 酷!使用 jQuery & Canvas 制作相机快门效果

    在今天的教程中,我们将使用 HTML5 的 Canvas 元素来创建一个简单的摄影作品集,它显示了一组精选照片与相机快门的效果.此功能会以一个简单的 jQuery 插件形式使用,你可以很容易地整合到任 ...

随机推荐

  1. JS 阶段练习~ 仿flash的图片轮换效果

    结合了所学的简单运动框架~  做这样一个综合小实例~~ -------------------------主要问题: 1.getByClassName  IE低版的兼容性 2.DOM不够严谨 … 各种 ...

  2. Keepalived使用梳理

    keepalived介绍keepalived观察其名可知,保持存活,在网络里面就是保持在线了,也就是所谓的高可用或热备,它集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防 ...

  3. [Network Analysis] 复杂网络分析总结

    在我们的现实生活中,许多复杂系统都可以建模成一种复杂网络进行分析,比如常见的电力网络.航空网络.交通网络.计算机网络以及社交网络等等.复杂网络不仅是一种数据的表现形式,它同样也是一种科学研究的手段.复 ...

  4. 自动判断应该Ajax还是return

    起因 最近回顾以前的代码,发现一个偶尔会见到的现象.一个类里面的方法可能需要Ajax返回,也有可能需要函数return.这个现象发生在网站MVC中的 逻辑层(或模型层),示例如下.IndexCtrl是 ...

  5. ASP.NET Core MVC 在linux上的创建及发布

    前言 ASP.NET core转眼都发布半月多了,社区最近也是非常活跃,虽然最近从事python工作,但也一直对.NET念念不忘,看过了园区大神们搭建的Asp.net core项目之后,自己也是跃跃欲 ...

  6. JavaScript模板引擎artTemplate.js——template.helper()方法

    上一篇文章我们已经讲到了helper()方法,但是上面的例子只是一个参数的写法,如果是多个参数,写法就另有区别了. <div id="user_info"></d ...

  7. MongoDB的备份和恢复

    1.导出数据库/备份: @echo off F: cd F:\software1\mongdb\mongodb-win32-x86_64-\bin start mongodump.exe -h -d ...

  8. CSS优先级

    一.CSS代码出现的几个位置 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况. 一般情况下,优先级如下:(外部样式)Extern ...

  9. jQuery 邮箱下拉列表自动补全

    综述 我想大家一定见到过,在某个网站填写邮箱的时候,还没有填写完,就会出现一系列下拉列表,帮你自动补全邮箱的功能.现在我们就用jQuery来实现一下. 博主原创代码,如有代码写的不完善的地方还望大家多 ...

  10. Puzzle 面向服务/切面(AOP/IOC)开发框架 For .Net

    Puzzle 面向服务/切面AOP开发框架 For .Net AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效 ...