找了個3d標簽云的代碼,想給它改成Vue組件,表示自己太菜不太會改,煩請大佬們幫忙改下
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>#rotate a{position:absolute;top:0px;left:0px;color:#00bdfe;font-weight:bold;padding:3px 6px;font-size:14px;z-index: 0 !important;} #rotate a:hover{border:1px solid #eee; background:#00bdfe; color: white; border-radius:5px; text-decoration:none;}</style></head>
<body>
<div id="rotate" style="height: 480px;">
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">車牌號測吉兇</a>
<a href="https://bbs.csdn.net/use/287" target="_blank">周邊上車點推薦</a>
<a href="https://bbs.csdn.net/use/7" target="_blank">火車票識別</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">全國火車票查詢</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">猜一猜</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">中文分詞</a>
<a href="https://bbs.csdn.net/use/41" target="_blank">在線活體檢測</a>
<a href="https://bbs.csdn.net/use/236" target="_blank">獲取外網IP資訊</a>
<a href="https://bbs.csdn.net/use/188" target="_blank">藥品查詢</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">影訊查詢</a>
<a href="https://bbs.csdn.net/use/72" target="_blank">歷史上的今天</a>
<a href="https://bbs.csdn.net/use/71" target="_blank">匯率</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">微信小程式查詢</a>
<a href="https://bbs.csdn.net/use/269" target="_blank">輸入提示</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">經緯度轉換地址</a>
<a href="https://bbs.csdn.net/use/231" target="_blank">微信公眾號查詢</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">同義詞</a>
<a href="https://bbs.csdn.net/use/245" target="_blank">識別身份證文字</a>
<a href="https://bbs.csdn.net/use/207" target="_blank">辭海</a>
<a href="https://bbs.csdn.net/use/261" target="_blank">駕車路徑規劃</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">網游排行榜</a>
<a href="https://bbs.csdn.net/use/44" target="_blank">人體關鍵點識別</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">全國房產資訊</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">三合一收款碼</a>
<a href="https://bbs.csdn.net/use/187" target="_blank">疾病資訊</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">姓名打分</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">腦筋急轉彎</a>
<a href="https://bbs.csdn.net/use/191" target="_blank">藥房藥店</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">全球IP地址查詢</a>
<a href="https://bbs.csdn.net/use/241" target="_blank">音樂搜索</a></div>
<script>var radius = 150;
var dtr = Math.PI / 180;
var d = 300;
var mcList = [];
var active = false;
var lasta = 1;
var lastb = 1;
var distr = true;
var tspeed = 2;
var size = 250;
var mouseX = 0;
var mouseY = 0;
var howElliptical = 1;
var aA = null;
var oDiv = null;
window.onload = function() {
var i = 0;
var oTag = null;
oDiv = document.getElementById('rotate');
aA = oDiv.getElementsByTagName('a');
for (i = 0; i < aA.length; i++) {
oTag = {};
oTag.offsetWidth = aA[i].offsetWidth;
oTag.offsetHeight = aA[i].offsetHeight;
mcList.push(oTag);
}
sineCosine(0, 0, 0);
positionAll();
oDiv.onmouseover = function() {
active = true;
};
oDiv.onmouseout = function() {
active = false;
};
oDiv.onmousemove = function(ev) {
var oEvent = window.event || ev;
mouseX = oEvent.clientX - (oDiv.offsetLeft + oDiv.offsetWidth / 2);
mouseY = oEvent.clientY - (oDiv.offsetTop + oDiv.offsetHeight / 2);
mouseX /= 5;
mouseY /= 5;
};
setInterval(update, 30);
};
function update() {
var a;
var b;
if (active) {
a = ( - Math.min(Math.max( - mouseY, -size), size) / radius) * tspeed;
b = (Math.min(Math.max( - mouseX, -size), size) / radius) * tspeed;
} else {
a = lasta * 0.98;
b = lastb * 0.98;
}
lasta = a;
lastb = b;
if (Math.abs(a) <= 0.01 && Math.abs(b) <= 0.01) {
return;
}
var c = 0;
sineCosine(a, b, c);
for (var j = 0; j < mcList.length; j++) {
var rx1 = mcList[j].cx;
var ry1 = mcList[j].cy * ca + mcList[j].cz * ( - sa);
var rz1 = mcList[j].cy * sa + mcList[j].cz * ca;
var rx2 = rx1 * cb + rz1 * sb;
var ry2 = ry1;
var rz2 = rx1 * ( - sb) + rz1 * cb;
var rx3 = rx2 * cc + ry2 * ( - sc);
var ry3 = rx2 * sc + ry2 * cc;
var rz3 = rz2;
mcList[j].cx = rx3;
mcList[j].cy = ry3;
mcList[j].cz = rz3;
per = d / (d + rz3);
mcList[j].x = (howElliptical * rx3 * per) - (howElliptical * 2);
mcList[j].y = ry3 * per;
mcList[j].scale = per;
mcList[j].alpha = per;
mcList[j].alpha = (mcList[j].alpha - 0.6) * (10 / 6);
}
doPosition();
depthSort();
}
function depthSort() {
var i = 0;
var aTmp = [];
for (i = 0; i < aA.length; i++) {
aTmp.push(aA[i]);
}
aTmp.sort(function(vItem1, vItem2) {
if (vItem1.cz > vItem2.cz) {
return - 1;
} else if (vItem1.cz < vItem2.cz) {
return 1;
} else {
return 0;
}
});
for (i = 0; i < aTmp.length; i++) {
aTmp[i].style.zIndex = i;
}
}
function positionAll() {
var phi = 0;
var theta = 0;
var max = mcList.length;
var i = 0;
var aTmp = [];
var oFragment = document.createDocumentFragment();
//隨機排序
for (i = 0; i < aA.length; i++) {
aTmp.push(aA[i]);
}
aTmp.sort(function() {
return Math.random() < 0.5 ? 1 : -1;
});
for (i = 0; i < aTmp.length; i++) {
oFragment.appendChild(aTmp[i]);
}
oDiv.appendChild(oFragment);
for (var i = 1; i < max + 1; i++) {
if (distr) {
phi = Math.acos( - 1 + (2 * i - 1) / max);
theta = Math.sqrt(max * Math.PI) * phi;
} else {
phi = Math.random() * (Math.PI);
theta = Math.random() * (2 * Math.PI);
}
//坐標裝換
mcList[i - 1].cx = radius * Math.cos(theta) * Math.sin(phi);
mcList[i - 1].cy = radius * Math.sin(theta) * Math.sin(phi);
mcList[i - 1].cz = radius * Math.cos(phi);
aA[i - 1].style.left = mcList[i - 1].cx + oDiv.offsetWidth / 2 - mcList[i - 1].offsetWidth / 2 + 'px';
aA[i - 1].style.top = mcList[i - 1].cy + oDiv.offsetHeight / 2 - mcList[i - 1].offsetHeight / 2 + 'px';
}
}
function doPosition() {
var l = oDiv.offsetWidth / 2;
var t = oDiv.offsetHeight / 2;
for (var i = 0; i < mcList.length; i++) {
aA[i].style.left = mcList[i].cx + l - mcList[i].offsetWidth / 2 + 'px';
aA[i].style.top = mcList[i].cy + t - mcList[i].offsetHeight / 2 + 'px';
aA[i].style.fontSize = Math.ceil(12 * mcList[i].scale / 2) + 8 + 'px';
aA[i].style.filter = "alpha(opacity=" + 100 * mcList[i].alpha + ")";
aA[i].style.opacity = mcList[i].alpha;
}
}
function sineCosine(a, b, c) {
sa = Math.sin(a * dtr);
ca = Math.cos(a * dtr);
sb = Math.sin(b * dtr);
cb = Math.cos(b * dtr);
sc = Math.sin(c * dtr);
cc = Math.cos(c * dtr);
}</script>
</body>
</html>
uj5u.com熱心網友回復:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#rotate a {
position: absolute;
top: 0px;
left: 0px;
color: #00bdfe;
font-weight: bold;
padding: 3px 6px;
font-size: 14px;
z-index: 0 !important;
}
#rotate a:hover {
border: 1px solid #eee;
background: #00bdfe;
color: white;
border-radius: 5px;
text-decoration: none;
}
</style>
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.min.js"></script>
</head>
<body>
<div id="app">
<div id="rotate" style="height: 480px;">
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">車牌號測吉兇</a>
<a href="https://bbs.csdn.net/use/287" target="_blank">周邊上車點推薦</a>
<a href="https://bbs.csdn.net/use/7" target="_blank">火車票識別</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">全國火車票查詢</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">猜一猜</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">中文分詞</a>
<a href="https://bbs.csdn.net/use/41" target="_blank">在線活體檢測</a>
<a href="https://bbs.csdn.net/use/236" target="_blank">獲取外網IP資訊</a>
<a href="https://bbs.csdn.net/use/188" target="_blank">藥品查詢</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">影訊查詢</a>
<a href="https://bbs.csdn.net/use/72" target="_blank">歷史上的今天</a>
<a href="https://bbs.csdn.net/use/71" target="_blank">匯率</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">微信小程式查詢</a>
<a href="https://bbs.csdn.net/use/269" target="_blank">輸入提示</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">經緯度轉換地址</a>
<a href="https://bbs.csdn.net/use/231" target="_blank">微信公眾號查詢</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">同義詞</a>
<a href="https://bbs.csdn.net/use/245" target="_blank">識別身份證文字</a>
<a href="https://bbs.csdn.net/use/207" target="_blank">辭海</a>
<a href="https://bbs.csdn.net/use/261" target="_blank">駕車路徑規劃</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">網游排行榜</a>
<a href="https://bbs.csdn.net/use/44" target="_blank">人體關鍵點識別</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">全國房產資訊</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">三合一收款碼</a>
<a href="https://bbs.csdn.net/use/187" target="_blank">疾病資訊</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">姓名打分</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">腦筋急轉彎</a>
<a href="https://bbs.csdn.net/use/191" target="_blank">藥房藥店</a>
<a href="javascript:layer.msg("暫未接入該介面",{icon: 2,shift: 6});" target="_blank">全球IP地址查詢</a>
<a href="https://bbs.csdn.net/use/241" target="_blank">音樂搜索</a>
</div>
</div>
<script>
new Vue({
el: '#app',
data() {
return {
radius: 150,
dtr: Math.PI / 180,
d: 300,
mcList: [],
active: false,
lasta: 1,
lastb: 1,
distr: true,
tspeed: 2,
size: 250,
mouseX: 0,
mouseY: 0,
howElliptical: 1,
aA: null,
oDiv: null,
}
},
methods: {
update() {
var a;
var b;
if (this.active) {
a = (-Math.min(Math.max(-this.mouseY, -this.size), this.size) / this.radius) * this.tspeed;
b = (Math.min(Math.max(-this.mouseX, -this.size), this.size) / this.radius) * this.tspeed;
} else {
a = this.lasta * 0.98;
b = this.lastb * 0.98;
}
this.lasta = a;
this.lastb = b;
if (Math.abs(a) <= 0.01 && Math.abs(b) <= 0.01) {
return;
}
var c = 0;
this.sineCosine(a, b, c);
for (var j = 0; j < this.mcList.length; j++) {
var rx1 = this.mcList[j].cx;
var ry1 = this.mcList[j].cy * this.ca + this.mcList[j].cz * (-this.sa);
var rz1 = this.mcList[j].cy * this.sa + this.mcList[j].cz * this.ca;
var rx2 = rx1 * this.cb + rz1 * this.sb;
var ry2 = ry1;
var rz2 = rx1 * (-this.sb) + rz1 * this.cb;
var rx3 = rx2 * this.cc + ry2 * (-this.sc);
var ry3 = rx2 * this.sc + ry2 * this.cc;
var rz3 = rz2;
this.mcList[j].cx = rx3;
this.mcList[j].cy = ry3;
this.mcList[j].cz = rz3;
per = this.d / (this.d + rz3);
this.mcList[j].x = (this.howElliptical * rx3 * per) - (this.howElliptical * 2);
this.mcList[j].y = ry3 * per;
this.mcList[j].scale = per;
this.mcList[j].alpha = per;
this.mcList[j].alpha = (this.mcList[j].alpha - 0.6) * (10 / 6);
}
this.doPosition();
this.depthSort();
},
depthSort() {
var i = 0;
var aTmp = [];
for (i = 0; i < this.aA.length; i++) {
aTmp.push(this.aA[i]);
}
aTmp.sort(function (vItem1, vItem2) {
if (vItem1.cz > vItem2.cz) {
return -1;
} else if (vItem1.cz < vItem2.cz) {
return 1;
} else {
return 0;
}
});
for (i = 0; i < aTmp.length; i++) {
aTmp[i].style.zIndex = i;
}
},
positionAll() {
var phi = 0;
var theta = 0;
var max = this.mcList.length;
var i = 0;
var aTmp = [];
var oFragment = document.createDocumentFragment();
//隨機排序
for (i = 0; i < this.aA.length; i++) {
aTmp.push(this.aA[i]);
}
aTmp.sort(function () {
return Math.random() < 0.5 ? 1 : -1;
});
for (i = 0; i < aTmp.length; i++) {
oFragment.appendChild(aTmp[i]);
}
this.oDiv.appendChild(oFragment);
for (var i = 1; i < max + 1; i++) {
if (this.distr) {
phi = Math.acos(-1 + (2 * i - 1) / max);
theta = Math.sqrt(max * Math.PI) * phi;
} else {
phi = Math.random() * (Math.PI);
theta = Math.random() * (2 * Math.PI);
}
//坐標裝換
this.mcList[i - 1].cx = this.radius * Math.cos(theta) * Math.sin(phi);
this.mcList[i - 1].cy = this.radius * Math.sin(theta) * Math.sin(phi);
this.mcList[i - 1].cz = this.radius * Math.cos(phi);
this.aA[i - 1].style.left = this.mcList[i - 1].cx + this.oDiv.offsetWidth / 2 - this.mcList[i - 1].offsetWidth / 2 + 'px';
this.aA[i - 1].style.top = this.mcList[i - 1].cy + this.oDiv.offsetHeight / 2 - this.mcList[i - 1].offsetHeight / 2 + 'px';
}
},
doPosition() {
var l = this.oDiv.offsetWidth / 2;
var t = this.oDiv.offsetHeight / 2;
for (var i = 0; i < this.mcList.length; i++) {
this.aA[i].style.left = this.mcList[i].cx + l - this.mcList[i].offsetWidth / 2 + 'px';
this.aA[i].style.top = this.mcList[i].cy + t - this.mcList[i].offsetHeight / 2 + 'px';
this.aA[i].style.fontSize = Math.ceil(12 * this.mcList[i].scale / 2) + 8 + 'px';
this.aA[i].style.filter = "alpha(opacity=" + 100 * this.mcList[i].alpha + ")";
this.aA[i].style.opacity = this.mcList[i].alpha;
}
},
sineCosine(a, b, c) {
this.sa = Math.sin(a * this.dtr);
this.ca = Math.cos(a * this.dtr);
this.sb = Math.sin(b * this.dtr);
this.cb = Math.cos(b * this.dtr);
this.sc = Math.sin(c * this.dtr);
this.cc = Math.cos(c * this.dtr);
}
},
mounted() {
var oTag = null;
this.oDiv = document.getElementById('rotate');
this.aA = this.oDiv.getElementsByTagName('a');
for (var i = 0; i < this.aA.length; i++) {
oTag = {};
oTag.offsetWidth = this.aA[i].offsetWidth;
oTag.offsetHeight = this.aA[i].offsetHeight;
this.mcList.push(oTag);
}
this.sineCosine(0, 0, 0);
this.positionAll();
this.oDiv.onmouseover = ()=> {
this.active = true;
};
this.oDiv.onmouseout = ()=> {
this.active = false;
};
this.oDiv.onmousemove = (ev) => {
var oEvent = window.event || ev;
this.mouseX = oEvent.clientX - (this.oDiv.offsetLeft + this.oDiv.offsetWidth / 2);
this.mouseY = oEvent.clientY - (this.oDiv.offsetTop + this.oDiv.offsetHeight / 2);
this.mouseX /= 5;
this.mouseY /= 5;
};
setInterval(() => {
this.update()
}, 30);
},
})
</script>
</body>
</html>
自己寫個vue檔案copy #app里面內容放到template里面,js除了實力vue其他都復制
uj5u.com熱心網友回復:
如果是這么寫我會,主要是這段
this.oDiv = document.getElementById('rotate');
this.aA = this.oDiv.getElementsByTagName('a');
不想直接操作dom元素,document.getElementById('rotate');可以用ref注冊然后Vue獲取到,主要了下面的this.oDiv.getElementsByTagName('a');這個我不知道用Vue該怎么處理
uj5u.com熱心網友回復:
你要回圈生成a標簽,然后給每個標簽設定一個ref?那你自己寫個陣列,多加一個style的屬性動態系結到dom上uj5u.com熱心網友回復:
不是沒想過,總覺得這么做不太好,所以來論壇問問大佬們該怎么處理
uj5u.com熱心網友回復:
要獲取dom當前style計算新的style不獲取dom怎么可能,這個本來就是操作dom css來實作的插件,就算改成:style={}的方式還是一樣的操作了dom轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/75498.html
標籤:JavaScript
