超炫网页背景效果

· WordPress · 53 次浏览

为网页增加雪花飘落特效还是比较简单的,只需要配置好 JS 代码就可以了,现将方法分享一下。

1、上传 js 代码

将 snow.js 文件上传到你的主题 js 目录,代码如下,可自行新建。

  1. /* 网页雪花飘落特效
  2. */
  3. (function() {
  4. function k(a, b, c) {
  5. if (a.addEventListener) a.addEventListener(b, c, false);
  6. else a.attachEvent && a.attachEvent("on" + b, c)
  7. }
  8. function g(a) {
  9. if (typeof window.onload != "function") window.onload = a;
  10. else {
  11. var b = window.onload;
  12. window.onload = function() {
  13. b();
  14. a()
  15. }}}
  16. function h() {
  17. var a = {};
  18. for (type in {
  19. Top: "",
  20. Left: ""
  21. }) {
  22. var b = type == "Top" ? "Y": "X";
  23. if (typeof window["page" + b + "Offset"] != "undefined")
  24. a[type.toLowerCase()] = window["page" + b + "Offset"];
  25. else {
  26. b = document.documentElement.clientHeight ? document.documentElement: document.body;
  27. a[type.toLowerCase()] = b["scroll" + type]
  28. }}
  29. return a
  30. }
  31. function l() {
  32. var a = document.body,
  33. b;
  34. if (window.innerHeight) b = window.innerHeight;
  35. else if (a.parentElement.clientHeight) b = a.parentElement.clientHeight;
  36. else if (a && a.clientHeight) b = a.clientHeight;
  37. return b
  38. }
  39. function i(a) {
  40. this.parent = document.body;
  41. this.createEl(this.parent, a);
  42. this.size = Math.random() * 10 + 15; /* 两个数字控制雪花的大小,目前代码控制雪花尺寸为 10~25px */
  43. this.el.style.width = Math.round(this.size) + "px";
  44. this.el.style.height = Math.round(this.size) + "px";
  45. this.maxLeft = document.body.offsetWidth - this.size;
  46. this.maxTop = document.body.offsetHeight - this.size;
  47. this.left = Math.random() * this.maxLeft;
  48. this.top = h().top + 1;
  49. this.angle = 1.4 + 0.2 * Math.random();
  50. this.minAngle = 1.4;
  51. this.maxAngle = 1.6;
  52. this.angleDelta = 0.01 * Math.random();
  53. this.speed = 2 + Math.random()
  54. }
  55. var j = false;
  56. g(function() {
  57. j = true
  58. });
  59. var f = true;
  60. window.createSnow = function(a, b) {
  61. if (j) {
  62. var c = [],
  63. m = setInterval(function() {
  64. f && b > c.length && Math.random()
  65. < b * 0.0025 && c.push(new i(a)); ! f && !c.length && clearInterval(m);
  66. for (var e = h().top, n = l(), d = c.length - 1; d >= 0; d--)
  67. if (c[d]) if (c[d].top < e || c[d].top + c[d].size + 1 > e + n) {
  68. c[d].remove();
  69. c[d] = null;
  70. c.splice(d, 1)
  71. } else {
  72. c[d].move();
  73. c[d].draw()
  74. }},
  75. 40);
  76. k(window, "scroll",
  77. function() {
  78. for (var e = c.length - 1; e >= 0; e--) c[e].draw()
  79. })
  80. } else g(function() {
  81. createSnow(a, b)
  82. })
  83. };
  84. window.removeSnow = function() {
  85. f = false
  86. };
  87. i.prototype = {
  88. createEl: function(a, b) {
  89. this.el = document.createElement("img");
  90. this.el.setAttribute
  91. ("src", b+"http://www.99bsy.com/wp-content/themes/begin/img/snow"+Math.floor(Math.random()*4)+".gif");/* 记得将 localhost/wp/ 改为自己的域名 */
  92. this.el.style.position = "absolute";
  93. this.el.style.display = "block";
  94. this.el.style.zIndex = "99999";
  95. this.parent.appendChild(this.el)
  96. },
  97. move: function() {
  98. if (this.angle < this.minAngle || this.angle > this.maxAngle)
  99. this.angleDelta = -this.angleDelta;
  100. this.angle += this.angleDelta;
  101. this.left += this.speed * Math.cos(this.angle * Math.PI);
  102. this.top -= this.speed * Math.sin(this.angle * Math.PI);
  103. if (this.left < 0) this.left = this.maxLeft;
  104. else if (this.left > this.maxLeft) this.left = 0
  105. },
  106. draw: function() {
  107. this.el.style.top = Math.round(this.top) + "px";
  108. this.el.style.left = Math.round(this.left) + "px"
  109. },
  110. remove: function() {
  111. this.parent.removeChild(this.el);
  112. this.parent = this.el = null
  113. }}
  114. })();
  115. createSnow("", 40);

2、上传雪花素材文件

将下载的 snow 文件夹中雪花素材文件到你的网站图片文件夹,不要修改图片名字。

3、配置 js 代码

将 js 代码配置到博客 header.php 文件中</head>之前(也可以放到 footer.php,看你喜欢)如下:

  1. <script type="text/javascript" src="http://www.99bsy.com/wp-content/themes/begin/js/snow.js"></script><!--圣诞飘雪花-->

是不是已经出现了雪花飘飘的效果呢?

4、注意事项

1、js 文件中可以自行调整雪花大小,也可自行更换雪花样式。

2、注意填写正确雪花文件地址,笔者是将 snow 文件夹直接放在了根目录所以雪花素材和 js 文件地址为“http://www.99bsy.com/...”请大家根据自己的实际情况进行修改。

相关代码、素材文件下载: [reply][url href=http://www.99bsy.com/wp-content/uploads/2017/07/雪花飘落效果的素材和 JS 文件包.zip]素材下载[/url][/reply]

评论 (5)

  1. QQ游客 回复

    个人不喜欢特效过多,怕网站打开速度。

    1. 九哥 回复

      @我爱动感单车网: 嗯嗯,JS太多会影响打开速度,还是少点好 😳

  2. devoted 回复

    页底的qq群是用php函数控制的还是html写的?

    1. 九哥 回复

      @devoted: 你说的文章底部那个吧?那是主题设置里面的。