JS實(shí)現(xiàn)鼠標(biāo)移動(dòng)拖尾
本文實(shí)例為大家分享了JS實(shí)現(xiàn)鼠標(biāo)移動(dòng)拖尾的具體代碼,供大家參考,具體內(nèi)容如下
JS 代碼
function getMousePos(event) {
var e = event || window.event;
var mouseInfo = {
mouseX : e.clientX,
mouseY : e.clientY
}
return mouseInfo;
}
function getMouseArt() {
this.artStyle = {
position: "fixed",
top: 0,
left: 0,
width: "50px",
height: "50px",
"font-size": 0,
"color": 0,
"text-transform": 0
};
this.init = function(obj) {
var character = ["你", "真", "的", "愛", "我", "嗎", "喜", "歡", "不", "對(duì)", "起", "彩", "色", "世", "界", "灰", "?"];
var font_trans = ["uppercase", "lowercase"];
this.Alpha = 1;
this.element = document.createElement('div');
var text = document.createTextNode(character[Math.floor(Math.random() * character.length)]);
this.element.appendChild(text);
this.addStyle(this.element, this.artStyle);
var offsetV = Math.floor(Math.random() * 60 - 30); // -30 ~ 30
this.element.style.left = obj.mouseX + offsetV +"px"; // x
this.element.style.top = obj.mouseY + offsetV +"px"; // y
this.element.style.fontSize = Math.floor(Math.random() * 20 + 10) + "px";
this.element.style.color = "hsla("+ Math.floor(Math.random() * 255) + ",100%,50%," + this.Alpha + ")";
this.element.style.textTransform = font_trans[Math.floor(Math.random() * 2)];
document.body.appendChild(this.element);
}
this.addStyle = function(ele, genuine) {
for (var k in genuine) {
ele.style[k] = genuine[k];
}
}
this.delElement = function() {
document.body.removeChild(this.element);
}
this.reduceColor = function(win) {
if (this.Alpha <= 1 && this.Alpha > 0) {
this.Alpha = this.Alpha - 0.1;
this.element.style.color = "hsla("+ Math.floor(Math.random() * 255) + ",100%,50%," + this.Alpha + ")";
console.log(this.Alpha);
}
else {
clearInterval(win);
this.delElement();
}
}
}
document.onmousemove = function(event) {
var obj = getMousePos(event);
var art = new getMouseArt();
art.init(obj);
var win = setInterval(function() {
art.reduceColor(win);
}, 30);
}
HTML 代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
background: black;
}
</style>
</head>
<body>
<script src="mouse.js" type="text/javascript"></script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
用JS簡(jiǎn)單實(shí)現(xiàn)九宮格抽獎(jiǎng)的示例代碼
在網(wǎng)上經(jīng)??匆娨恍┏楠?jiǎng)頁面,也玩過不同類型的抽獎(jiǎng)活動(dòng),但是一直沒有做過抽獎(jiǎng)的功能,所以今天來實(shí)現(xiàn)一個(gè)簡(jiǎn)單的九宮格抽獎(jiǎng)功能,文中有詳細(xì)的代碼示例供大家參考,感興趣的朋友可以自己動(dòng)手嘗試一下2023-12-12
JavaScript利用fetch實(shí)現(xiàn)異步請(qǐng)求的方法實(shí)例
傳遞信息到服務(wù)器,從服務(wù)器獲取信息,是前端發(fā)展的重中之重,尤其是現(xiàn)在前后端分離的大前提下,前后端的數(shù)據(jù)交互是前端的必修科目了,下面這篇文章主要給大家介紹了關(guān)于JavaScript利用fetch實(shí)現(xiàn)異步請(qǐng)求的相關(guān)資料,需要的朋友可以參考借鑒。2017-07-07
js使用數(shù)組判斷提交數(shù)據(jù)是否存在相同數(shù)據(jù)
判斷提交數(shù)據(jù)是否存在相同數(shù)據(jù),在本文將為大家介紹使用數(shù)組做到這一點(diǎn),感興趣的朋友可以參考下2013-11-11
JS如何實(shí)現(xiàn)動(dòng)態(tài)添加的元素綁定事件
這篇文章主要介紹了JS如何實(shí)現(xiàn)動(dòng)態(tài)添加的元素綁定事件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
JavaScript 完成注冊(cè)頁面表單校驗(yàn)的實(shí)例
下面小編就為大家?guī)硪黄狫avaScript 完成注冊(cè)頁面表單校驗(yàn)的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
JavaScript中報(bào)錯(cuò)Cannot?set?properties?of?undefined?(settin
這篇文章主要介紹了JavaScript中報(bào)錯(cuò)Cannot?set?properties?of?undefined?(setting?'1')的解決方案,文中通過代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或使用js具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2025-03-03
淺談bootstrap源碼分析之scrollspy(滾動(dòng)偵聽)
下面小編就為大家?guī)硪黄獪\談bootstrap源碼分析之scrollspy(滾動(dòng)偵聽)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06

