-
Notifications
You must be signed in to change notification settings - Fork 0
/
links.js
40 lines (35 loc) · 1.26 KB
/
links.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
let elements = document.querySelectorAll('.js-change');
const colors = ['#efc967', '#dd523c', '#deb45f', '#7500f9', '#29abe2', '#73b24f', '#000000'];
for (let i = 0; i < elements.length; i++) {
let a = elements[i];
a.addEventListener('mouseenter', (evt) => {
if (!a.transformed) {
let link = a.querySelector('a');
a.transformed = true;
a.original_content = link.innerText;
a.original_attributes = link.attributes;
let chars = a.original_content.split('');
let newhtml = `<a ${getAttrString(link.attributes)}>`
for (let c = 0; c < chars.length; c++) {
newhtml += `<b style="color: ${getRandom()}">${chars[c]}</b>`
}
newhtml += '</a>'
a.innerHTML = newhtml;
}
})
a.addEventListener('mouseout', (evt) => {
a.transformed = false;
a.innerHTML = `<a ${getAttrString(a.original_attributes)}>${a.original_content}</a>`;
})
}
function getAttrString(attrs) {
let str = ''
for (let a = 0; a < attrs.length; a++) {
let attr = attrs[a];
str += ` ${attr.name}="${attr.value}"`;
}
return str;
}
function getRandom() {
return colors[Math.floor(Math.random() * colors.length)];
}