Untitled
1 week ago in Plain Text
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>wrap span</title>

<style>
.text {
width: 25%;
margin: 0;
padding: 0;
border: 0;
font-family: Arial, Helvetica, sans-serif;
}

.text span {
opacity: 0;
animation: fadeIn 350ms forwards;
}

@keyframes fadeIn {
from {
opacity: 0;
}

to {
opacity: 1;
}
}
</style>
</head>

<body>

<div id="text" class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus odio mauris, porta
vitae enim sed, imperdiet tristique tortor. Aliquam dui augue, aliquam in ipsum ut, fringilla pharetra ex. Proin
mollis mattis lacus eget lacinia. Curabitur consequat, nunc sed volutpat elementum, quam mauris scelerisque
nunc, sed mattis turpis augue at ligula. Donec nec mauris aliquam purus imperdiet gravida.#4sdjhdfkhg358956
Morbi vel hendrerit risus, nec scelerisque nunc. Vestibulum placerat diam et dui commodo, nec ullamcorper elit
tincidunt. Maecenas vel viverra lorem. Nunc suscipit nulla non lorem imperdiet, at ullamcorper nisl pharetra.
</div>
<script>
var txt = document.getElementById('text');
window.addEventListener('resize', wrapTextToSpan);
wrapTextToSpan();

function wrapTextToSpan() {
txt.innerHTML = '<span>' + getWrappedText(txt.innerText, txt.offsetHeight, txt.className).replace(/\n/gi, '</span><span>') + '</span>';
txt.querySelectorAll("span").forEach(function (span, index) { span.style.animationDelay = index * 50 + "ms"; });
}

var txtArea;
function getWrappedText(str, height, className) {
txtArea = document.createElement('textarea');
document.body.appendChild(txtArea);

txtArea.style.height = height + "px";
txtArea.style.visibility = "hidden";
txtArea.style.position = "absolute";
txtArea.className = className;
txtArea.value = "";
txtArea.wrap = "off";

var wordArr = str.replace(/\n/g, '').split(/\s/gi),
scrollEmptyWidth = txtArea.scrollWidth;
for (var i = 0; i < wordArr.length; i++) {
txtArea.value += wordArr[i] + " ";
if (txtArea.scrollWidth > scrollEmptyWidth)
txtArea.value = txtArea.value.slice(0, txtArea.value.length - wordArr[i].length - 1) + "\n" + wordArr[i] + " ";
}
document.body.removeChild(txtArea);

return txtArea.value;
}
</script>
</body>

</html>
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80