Home > Article > Web Front-end > JavaScript implements simple barrage effect example analysis
This article mainly helps you to simply realize the JavaScriptbarrage effect
I don’t know if you have felt it, but the barrage is another good show! !
However, I personally prefer not to be disturbed by barrages when watching TV. Today we will write a simple barrage. How simple is it? Take a look at the effect:
As you can see from the picture, our silly HTML structure is indeed very simple.
is composed of a p, an input box and a button.
<p id="box" class="box"></p> <input type="text" id="txt" /> <button onclick="send()">提交内容</button>
First post jsCode:
function $(str) { return document.getElementById(str); } function send() { var word = $('txt').value; var span = document.createElement('span'); var top = parseInt(Math.random() * 500) - 20; var color1 = parseInt(Math.random() * 256); var color2 = parseInt(Math.random() * 256); var color3 = parseInt(Math.random() * 256); var color = "rgb(" + color1 + "," + color2 + "," + color3 + ")"; top = top < 0 ? 0 : top; span.style.position = 'absolute'; span.style.top = top + "px"; span.style.color = color; span.style.left = '500px'; span.style.whiteSpace = 'nowrap'; var nub = (Math.random() * 10) + 1; span.setAttribute('speed', nub); span.speed = nub; span.innerHTML = word; $('box').appendChild(span); $('txt').value = ""; } setInterval(move, 200); function move() { var spanArray = $('box').children; for (var i = 0; i < spanArray.length; i++) { spanArray[i].style.left = parseInt(spanArray[i].style.left) - spanArray[i].speed + 'px'; } }
The principle is briefly explained:
First step, We need to get the content in the input box, var word = $('txt').value;
Second step, we have to try our best to stuff this content into the p to be scrolled and displayed Inside, there are three principles: ① Random color ② Random height ③ The distance from the left border changes in real time;
The third step, append this content to p $('box').appendChild (span);
It can be seen from the principles of the above steps that the second step is the most critical step,
realize the first principle:
var color1 = parseInt(Math.random() * 256); var color2 = parseInt(Math.random() * 256); var color3 = parseInt(Math.random() * 256); var color = "rgb(" + color1 + "," + color2 + "," + color3 + ")"; span.style.color = color;
小Extension:
RGB(R,G,B);
R:
Red value. Positive Integer | Percentage
G:
Green value. Positive integer | Percentage
B:
Blue value. Positive integer | Percent
This should be easy to understand.
Implement the second principle:
var top = parseInt(Math.random() * 500) - 20; top = top < 0 ? 0 : top; span.style.top = top + "px";
Implement the third principle:
span.style.left = '500px'; setInterval(move, 200); function move() { var spanArray = $('box').children; for (var i = 0; i < spanArray.length; i++) { spanArray[i].style.left = parseInt(spanArray[i].style.left) - spanArray[i].speed + 'px'; } }
is to use the principle of timer to let the value of left changes in real time.
At this point, it should be very clear.
The above is the detailed content of JavaScript implements simple barrage effect example analysis. For more information, please follow other related articles on the PHP Chinese website!