瞎折腾之好玩的代码片段汇总

记录一些有意思的JS代码片段,拿来即可用。

第一款:给网站添加点击特效 心

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<script type="text/javascript">
!function (e, t, a) {
function r() {
for (var 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() {
var t = "function" == typeof e.onclick && e.onclick;
e.onclick = function (e) {
t && t(), o(e)
}
}
function o(e) {
var 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) {
var a = t.createElement("style");
a.type = "text/css";
try {
a.appendChild(t.createTextNode(e))
} catch (t) {
a.styleSheet.cssText = e
}
t.getElementsByTagName("head")[0].appendChild(a)
}
function c() {
return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
}
var 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);
</script>

第二款:网站焦点监控

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<script>
var OriginTitile = document.title,
titleTime;
document.addEventListener("visibilitychange",
function() {
if (document.hidden) {
$;
document.title = "哎呀,404 Not Found!";
clearTimeout(titleTime)
} else {
$;
document.title = "咦,好像又好了?" + OriginTitile;
titleTime = setTimeout(function() {
document.title = OriginTitile
},
2000)
}
});
</script>

Demo2

1
2
3
4
5
<script>
var originalTitle = document.title;
window.onblur = function(){document.title = '要玩捉迷藏吗'};
window.onfocus = function(){document.title = originalTitle};
</script>

第三款:闪烁字体

先在适当位置添加<p id="binft"></p>,再引入JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<script>
var binft = function (r) {
function t() {
return b[Math.floor(Math.random() * b.length)]
}

function e() {
return String.fromCharCode(94 * Math.random() + 33)
}

function n(r) {
for (var n = document.createDocumentFragment(), i = 0; r > i; i++) {
var l = document.createElement("span");
l.textContent = e(), l.style.color = t(), n.appendChild(l)
}
return n
}

function i() {
var t = o[c.skillI];
c.step ? c.step-- : (c.step = g, c.prefixP < l.length ? (c.prefixP >= 0 && (c.text += l[c.prefixP]), c.prefixP++) : "forward" === c.direction ? c.skillP < t.length ? (c.text += t[c.skillP], c.skillP++) : c.delay ? c.delay-- : (c.direction = "backward", c.delay = a) : c.skillP > 0 ? (c.text = c.text.slice(0, -1), c.skillP--) : (c.skillI = (c.skillI + 1) % o.length, c.direction = "forward")), r.textContent = c.text, r.appendChild(n(c.prefixP < l.length ? Math.min(s, s + c.prefixP) : Math.min(s, t.length - c.skillP))), setTimeout(i, d)
}
var l = "",
o = ["无论蓬户荆扉", "都将因你的倚闾", "而成为我的凯旋门", "生活琐碎", "三两烈酒"].map(function (r) {
return r + "."
}),
a = 2,
g = 1,
s = 5,
d = 75,
b = ["rgb(110,64,170)", "rgb(150,61,179)", "rgb(191,60,175)", "rgb(228,65,157)", "rgb(254,75,131)", "rgb(255,94,99)", "rgb(255,120,71)", "rgb(251,150,51)", "rgb(226,183,47)", "rgb(198,214,60)", "rgb(175,240,91)", "rgb(127,246,88)", "rgb(82,246,103)", "rgb(48,239,130)", "rgb(29,223,163)", "rgb(26,199,194)", "rgb(35,171,216)", "rgb(54,140,225)", "rgb(76,110,219)", "rgb(96,84,200)"],
c = {
text: "",
prefixP: -s,
skillI: 0,
skillP: 0,
direction: "forward",
delay: a,
step: g
};
i()
};
binft(document.getElementById('binft'));
</script>

第四款:网站运营时间统计

1
<p>本站已安全运行 <b> <strong> <script language="JavaScript" type ="text/javascript"> var urodz = new Date("12/07/2017"); var now = new Date(); var ile = now.getTime() - urodz.getTime(); var dni = Math.floor(ile / (1000 * 60 * 60 * 24)); document.write( + dni) </script> </strong> </b> 天</p>

第二种为 (像时钟一样走动),合适位置添加<span>博客已运行</span><span id=bin_tm></span>,再引入JavaScript

1
2
3
<script type="text/javascript">
function show_date_time(){window.setTimeout("show_date_time()",1e3);var BirthDay=new Date("2016/06/06 00:00:00"),today=new Date,timeold=today.getTime()-BirthDay.getTime(),msPerDay=864e5,e_daysold=timeold/msPerDay,daysold=Math.floor(e_daysold),e_hrsold=24*(e_daysold-daysold),hrsold=Math.floor(e_hrsold),e_minsold=60*(e_hrsold-hrsold),minsold=Math.floor(60*(e_hrsold-hrsold)),seconds=Math.floor(60*(e_minsold-minsold));bin_tm.innerHTML=daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"秒";}
show_date_time();</script>

第五款:复制弹窗提示

引入javascript

1
<script src="//cdn.hap5.top/code/layer/layer.js"></script>

并且在自定义JavaScript中添加

1
2
3
<script type="text/javascript">
document.body.oncopy = function() {layer.msg('复制成功,若要转载请务必保留原文链接!');};
</script>

第六款:打字特效

1
2
3
4
5
6
<script src="//demo.fooor.cn/js/activate-power-mode.js "></script>  
<script>
POWERMODE.colorful = true; // 是否开启冒光特效
POWERMODE.shake = false; // 是否开启抖动特效
document.body.addEventListener('input', POWERMODE);
</script>

第七款:页面滚动顶部进度条加载效果代码

1
2
3
4
5
6
7
8
9
<div class="test"></div>
<script src="jquery.min.js"></script>
<script src="headBand.min.js"></script>
<script>
$('.test').headBand({//
'background':'#008aff',//颜色
'height':"1"//宽度
});
</script>

第八款:标题打字特效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!--标题打字特效引入-->
<p id ="typetitle">可能我在也不会遇到第二这样的她</p>
<script>
var typingbefore = document.getElementById("typetitle").innerText;//获取标题内容
document.getElementById('typetitle').innerText = "";//将标题内容赋值为空
var i = 0;
function typetitle(){
var typingafter = document.getElementById('typetitle');//获取标题元素
if(i <= typingbefore.length){
typingafter.innerHTML = typingbefore.slice(0,i++)+'|';//将标题内容通过slice()方法返回
setTimeout('typetitle()',100);//每100毫秒执行一次
}else{
typingafter.innerHTML = typingbefore;//当标题内容全部返回后,去掉最后的‘|’
}
}
typetitle();
</script>

第九款:文字抖动效果

1
2
3
<link rel="stylesheet" href="doudong.css">
<p id="shaky" class="shaky c">十一♥</p>
<span class="shaky" ><p id="shaky">(●'◡'●)ノ</p></span>

第十款:社会主义价值观

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<script type="text/javascript"> 
/* 鼠标特效 */
var a_idx = 0;
jQuery(document).ready(function($) {
$("body").click(function(e) {
var a = new Array("富强", "民主", "文明", "和谐", "自由", "平等", "公正" ,"法治", "爱国", "敬业", "诚信", "友善");
var $i = $("<span/>").text(a[a_idx]);
a_idx = (a_idx + 1) % a.length;
var x = e.pageX,
y = e.pageY;
$i.css({
"z-index": 999999999999999999999999999999999999999999999999999999999999999999999,
"top": y - 20,
"left": x,
"position": "absolute",
"font-weight": "bold",
"color": " #e8425c"
});
$("body").append($i);
$i.animate({
"top": y - 200,
"opacity": 0
},
1500,
function() {
$i.remove();
});
});
});
</script>

第十一款:自定义滚动条

1
2
3
4
5
<style type="text/css"> 
::-webkit-scrollbar {width: 3px;height: 3px;}
::-webkit-scrollbar-track {-webkit-border-radius: 2em;-moz-border-radius: 2em;border-radius:2em;}
::-webkit-scrollbar-thumb {background-color: #cccccc;background-image: -webkit-linear-gradient(24deg, rgba(255, 255, 255, .3) 100%, transparent 100%, transparent);-webkit-border-radius: 2em;-moz-border-radius: 2em;border-radius:2em;}
</style>
0%