为wordpress添加梅花飘落特效【WP系列教程之二十一】

为wordpress添加梅花飘落特效,小编一直在用,实际上就是一个js文件。最近小编突然发现这个特效也会拉慢网站,所以小编就考虑撤掉这个特效,特在此记录。以便后期再用。那么怎么为wordpress添加梅花飘落特效呢?

首先如下:

将以下代码保存为一个js文件:我们现以“梅花特效”首字母命名为:mhtx.js

(function() {
function k(a, b, c) {
if (a.addEventListener) a.addEventListener(b, c, false);
else a.attachEvent && a.attachEvent("on" + b, c)
}
function g(a) {
if (typeof window.onload != "function") window.onload = a;
else {
var b = window.onload;
window.onload = function() {
b();
a()
}}}
function h() {
var a = {};
for (type in {
Top: "",
Left: ""
}) {
var b = type == "Top" ? "Y": "X";
if (typeof window["page" + b + "Offset"] != "undefined")
a[type.toLowerCase()] = window["page" + b + "Offset"];
else {
b = document.documentElement.clientHeight ? document.documentElement: document.body;
a[type.toLowerCase()] = b["scroll" + type]
}}
return a
}
function l() {
var a = document.body,
b;
if (window.innerHeight) b = window.innerHeight;
else if (a.parentElement.clientHeight) b = a.parentElement.clientHeight;
else if (a && a.clientHeight) b = a.clientHeight;
return b
}
function i(a) {
this.parent = document.body;
this.createEl(this.parent, a);
this.size = Math.random() * 4 + 8; /* 两个数字控制梅花的大小,目前代码控制梅花尺寸为10~25px */
this.el.style.width = Math.round(this.size) + "px";
this.el.style.height = Math.round(this.size) + "px";
this.maxLeft = document.body.offsetWidth - this.size;
this.maxTop = document.body.offsetHeight - this.size;
this.left = Math.random() * this.maxLeft;
this.top = h().top + 1;
this.angle = 1.4 + 0.2 * Math.random();
this.minAngle = 1.4;
this.maxAngle = 1.6;
this.angleDelta = 0.01 * Math.random();
this.speed = 2 + Math.random()
}
var j = false;
g(function() {
j = true
});
var f = true;
window.createSnow = function(a, b) {
if (j) {
var c = [],
m = setInterval(function() {
f && b > c.length && Math.random()
< b * 0.0025 && c.push(new i(a)); ! f && !c.length && clearInterval(m);
for (var e = h().top, n = l(), d = c.length - 1; d >= 0; d--)
if (c[d]) if (c[d].top < e || c[d].top + c[d].size + 1 > e + n) {
c[d].remove();
c[d] = null;
c.splice(d, 1)
} else {
c[d].move();
c[d].draw()
}},
40);
k(window, "scroll",
function() {
for (var e = c.length - 1; e >= 0; e--) c[e].draw()
})
} else g(function() {
createSnow(a, b)
})
};
window.removeSnow = function() {
f = false
};
i.prototype = {
createEl: function(a, b) {
this.el = document.createElement("img");
this.el.setAttribute
("src", b+"http://www.liedun.top/tu/meihua/snow"+Math.floor(Math.random()*4)+".gif");/* 图片文件所在地址 */
this.el.style.position = "absolute";
this.el.style.display = "block";
this.el.style.zIndex = "99999";
this.parent.appendChild(this.el)
},
move: function() {
if (this.angle < this.minAngle || this.angle > this.maxAngle)
this.angleDelta = -this.angleDelta;
this.angle += this.angleDelta;
this.left += this.speed * Math.cos(this.angle * Math.PI);
this.top -= this.speed * Math.sin(this.angle * Math.PI);
if (this.left < 0) this.left = this.maxLeft;
else if (this.left > this.maxLeft) this.left = 0
},
draw: function() {
this.el.style.top = Math.round(this.top) + "px";
this.el.style.left = Math.round(this.left) + "px"
},
remove: function() {
this.parent.removeChild(this.el);
this.parent = this.el = null
}}
})();
createSnow("", 40);

然后将mhtx.js文件上传到主题js文件夹中,最后在主题头部或页脚引入mhtx.js,代码如下:

<script type="text/javascript" language="javascript" src="/wp-content/themes/主题名称/js/mhtx.js"></script>

至此,即可完美实现!

下面是梅花花瓣素材:

文件下载:  

古风网络博客资源声明

本站资源来源于网络收集和网友分享,只供学习交流使用,请下载后24小时内删除。
如本站侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
本站部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
本站部分文章为原创,部分为转载,引用请保留链接信息,谢谢合作!
本文链接地址:https://www.liedun.top/872.html 为wordpress添加梅花飘落特效【WP系列教程之二十一】

发表评论