主机参考_WordPress技巧分享_纯代码利用js实现wordpress鼠标点击出现爱心/数字/文字特效

Wordpress5年前 (2019)发布 SUYEONE
990 0 0

欢迎回来,这次我们要探讨一些有趣的网页交互特效,这些特效会在用户点击鼠标时触发,为你的网站增添一丝趣味性。下面我将介绍三种不同的鼠标点击特效,并提供实现它们的代码片段。

**特效一:计数器**
每次点击鼠标,页面上的数字会自动增加1,并通过cookie持久保存,即使切换页面也会累加。要清除计数,只需删除cookie即可。

**实现方法:**
在你的`footer.php`文件的“标签之前添加以下代码:

“`html

/* 鼠标点击计数器特效
/* 参考来源:https://zhujicankao.com/1783.html
*/
$(“body”).on(“click”, function(e) {
if ($.cookie(“_click_count”) === undefined) {
$.cookie(“_click_count”, 0);
}
let _click_count = $.cookie(‘_click_count’);
_click_count++;
$.cookie(“_click_count”, _click_count);
let $i = $(““).text(“+” + _click_count);
let x = e.pageX,
y = e.pageY;
$i.css({
“z-index”: 99999,
“top”: y – 15,
“left”: x,
“position”: “absolute”,
“color”: “red”
});
$(“body”).append($i);
$i.animate(
{“top”: y – 180, “opacity”: 0},
1500,
function () {$i.remove();}
);
e.stopPropagation();
});

“`

**特效二:随机文字**
每次点击,会出现预设词汇列表中的一项,如“富强”、“民主”等。你可以自定义这个列表。

**实现方法:**
在`footer.php`的“标签之前插入:

“`html

/* 鼠标点击显示随机文字特效
/* 参考来源:https://zhujicankao.com/1783.html
*/
let a_idx = 0;
jQuery(document).ready(function ($) {
$(“body”).on(“click”, function (e) {
let a = [“富强”, “民主”, “文明”, “和谐”, “自由”, “平等”, “公正”, “法治”, “爱国”, “敬业”, “诚信”, “友善”];
let $i = $(““).text(a[a_idx]);
a_idx = (a_idx + 1) % a.length;
let x = e.pageX,
y = e.pageY;
$i.css({
“z-index”: 99999,
“top”: y – 20,
“left”: x,
“position”: “absolute”,
“font-weight”: “bold”,
“color”: “#ff6651”
});
$(“body”).append($i);
$i.animate(
{“top”: y – 180, “opacity”: 0},
1500,
function () {$i.remove();}
);
});
});

“`

**特效三:彩色爱心**
每次点击,屏幕上会飘落一个随机颜色的爱心,视觉效果十分可爱。

**实现方法:**
同样,在`footer.php`的“标签之前添加:

“`html

/* 鼠标点击生成彩色爱心特效
/* 参考来源:https://zhujicankao.com/1783.html
*/
(function (e, t, a) {
function r() {
for (let e = 0; e < s.length; e++)
s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) :
(s[e].y–, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");
requestAnimationFrame(r)
}
function n() {
"function" == typeof e.onclick && (e.onclick = function (e) {
e.onclick(), o(e)
})
}
function o(e) {
let a = t.createElement("div");
a.className = "heart", s.push({
el: a,
x: e.clientX – 5,
y: e.clientY – 5,
scale: 1,
alpha: 1,
color: c()
}), t.body.appendChild(a)
}
function i(e) {
let a = t.createElement("style");
a.type = "text/css";
try {
a.appendChild(t.createTextNode(e))
} catch (e) {
a.styleSheet.cssText = e
}
t.getElementsByTagName("head")[0].appendChild(a)
}
function c() {
return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
}
let s = [];
e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {
setTimeout(e, 1e3 / 60)
}, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r()
})(window, document);

“`

请注意,这三个特效不要同时使用,以免相互冲突。选择你喜欢的一个,为你的网站增添一点个性化吧!

© 版权声明

相关文章

暂无评论

暂无评论...