怎么给html5背景加上js粒子特效

JavaScript013

怎么给html5背景加上js粒子特效,第1张

使用了particles.js

particles.js可以从github网站下载到最新的源码,网址是 https://github.com/VincentGarreau/particles.js/

使用方法非常简单

第一步,在html中引入脚本文件 particles.min.js,这个文件在下载的压缩包里可以找到

<script src="particles.min.js"></script>

第二步,在html中放入一个div容器,设置id为particles-js。这个一般放在所有网页元素的最后面就可以。

<div id="particles-js"></div>

<style type="text/css">

#particles-js {

position: absolute

top:0

width:100%

}

</style>

第三步,设置窗口样式

<style type="text/css">

#particles-js {

z-index: -1

position: absolute

top: 0

width: 100%

background: #aaa

}</style>

第四步,脚本生成粒子效果,可以单独放在一个js文件里,也可以放在<script>标签里。无论如何,这段脚本要出现在div容器之后。

particlesJS("particles-js", {  "particles": {    "number": {      "value": 380,      "density": {        "enable": true,        "value_area": 800

      }

    },    "color": {      "value": "#ffffff"

    },    "shape": {      "type": "circle",      "stroke": {        "width": 0,        "color": "#000000"

      },      "polygon": {        "nb_sides": 5

      },      "image": {        "src": "img/github.svg",        "width": 100,        "height": 100

      }

    },    "opacity": {      "value": 0.5,      "random": false,      "anim": {        "enable": false,        "speed": 1,        "opacity_min": 0.1,        "sync": false

      }

    },    "size": {      "value": 3,      "random": true,      "anim": {        "enable": false,        "speed": 40,        "size_min": 0.1,        "sync": false

      }

    },    "line_linked": {      "enable": true,      "distance": 150,      "color": "#ffffff",      "opacity": 0.4,      "width": 1

    },    "move": {      "enable": true,      "speed": 6,      "direction": "none",      "random": false,      "straight": false,      "out_mode": "out",      "bounce": false,      "attract": {        "enable": false,        "rotateX": 600,        "rotateY": 1200

      }

    }

  },  "interactivity": {    "detect_on": "canvas",    "events": {      "onhover": {        "enable": true,        "mode": "grab"

      },      "onclick": {        "enable": true,        "mode": "push"

      },      "resize": true

    },    "modes": {      "grab": {        "distance": 140,        "line_linked": {          "opacity": 1

        }

      },      "bubble": {        "distance": 400,        "size": 40,        "duration": 2,        "opacity": 8,        "speed": 3

      },      "repulse": {        "distance": 200,        "duration": 0.4

      },      "push": {        "particles_nb": 4

      },      "remove": {        "particles_nb": 2

      }

    }

  },  "retina_detect": true})

使用原生js自己写或js库(框架)都是可以的,由于目前HTML5并不是所有的浏览器都完美支持,使用兼容性比较好的js库是个不错的选择。

例如Highcharts图标库就可以实现各类曲线图、折线图、区域图、3D图、柱状图等等。具体使用参考:http://www.hcharts.cn/demo/index.php。

Highcharts中做折线图的demo代码可以作为参考:

<html lang="en">

<head>

  <script type="text/javascript" src="http://cdn.hcharts.cn/jquery/jquery-1.8.3.min.js"></script>

  <script type="text/javascript" src="http://cdn.hcharts.cn/highcharts/highcharts.js"></script>

  <script type="text/javascript" src="http://cdn.hcharts.cn/highcharts/exporting.js"></script>

  <script>

    $(function () {

    $('#container').highcharts({

        chart: {

            type: 'line'

        },

        title: {

            text: 'Monthly Average Temperature'

        },

        subtitle: {

            text: 'Source: WorldClimate.com'

        },

        xAxis: {

            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

        },

        yAxis: {

            title: {

                text: 'Temperature (°C)'

            }

        },

        tooltip: {

            enabled: false,

            formatter: function() {

                return '<b>'+ this.series.name +'</b><br/>'+this.x +': '+ this.y +'°C'

            }

        },

        plotOptions: {

            line: {

                dataLabels: {

                    enabled: true

                },

                enableMouseTracking: false

            }

        },

        series: [{

            name: 'Tokyo',

            data: [7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]

        }, {

            name: 'London',

            data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]

        }]

    })

})

  </script>

</head>

<body>

  <div id="container" style="min-width:700pxheight:400px"></div>

</body>

</html>