This article describes the method of JS to move text with the mouse. Share it for your reference. The specific analysis is as follows:
This is a very simple mouse feature code. When moving the mouse on a web page, the mouse is moved by a string of text following the mouse.
Copy the code as follows: <html>
<head>
<style type="text/css">
.spanstyle {
COLOR: 000000; FONT-SIZE: 10pt; POSITION: absolute; TOP: -50px; VISIBILITY: visible
}
</style>
<script>
var x,y
var step=18 //This is the interval between two adjacent characters
var flag=0
var message="Wulin.com www.VeVB.COM Welcome to your visit!" //Put the text you need to display here
message=message.split("") //Decompose the string into an array
var xpos=new Array() //Create an array to record the x-coordinates of each position
for (i=0;i<=message.length-1;i++) { // Assign an initial value to each element first
xpos[i]=-50
}
var ypos=new Array() //Create an array to record the y coordinates of each position
for (i=0;i<=message.length-1;i++) {
ypos[i]=-200
}
function movehandler(e){ //handle mouse movement events
x = (document.layers) ? e.pageX : document.body.scrollLeft+event.clientX//Direct the horizontal position of the mouse according to the browser
y = (document.layers) ? e.pageY : document.body.scrollTop+event.clientY//Record the vertical position of the mouse
flag=1 //The mouse position has changed and needs to be recalculated
}
function makesnake() {
if (flag==1 && document.all) { //If it is IE
for (i=message.length-1; i>=1; i--) { //Processing coordinate queues
xpos[i]=xpos[i-1]+step //Move each coordinate data one grid and add character spacing
ypos[i]=ypos[i-1]
}
xpos[0]=x+step //Write new data into the tail of the coordinate data queue
ypos[0]=y
for (i=0; i<message.length-1; i++) {
var thisspan = eval("span"+(i)+".style")// Generate the current operation object spanx.style
thisspan.posLeft=xpos[i]
thisspan.posTop=ypos[i]
}
}
else if (flag==1 && document.layers) { //If it is NS
for (i=message.length-1; i>=1; i--) { //Processing coordinate queues
xpos[i]=xpos[i-1]+step //Move each coordinate data one grid and add character spacing
ypos[i]=ypos[i-1]
}
xpos[0]=x+step //Write new data into the tail of the coordinate data queue
ypos[0]=y
for (i=0; i<message.length-1; i++) { //Change the coordinates of the layer where each word is located according to the data of the array
var thispan = eval("document.span"+i) //Generate the current operation object document.spanx
thisspan.left=xpos[i]
thisspan.top=ypos[i]
}
}
var timer=setTimeout("makesnake()",30) //After 30 milliseconds, adjust the position of each character again according to the situation.
}
</script>
</head>
<body bgcolor="ffffff" onload="makesnake()">
<script>
<!-- Beginning of JavaScript -
// Here we generate a span as a container for each word
for (i=0;i<=message.length-1;i++) {
document.write("<span id='span"+i+"' class='spanstyle'>")
document.write(message[i])
document.write("</span>")
}
//Specify the process of handling mouse movement events
if (document.layers){
document.captureEvents(Event.MOUSEMOVE);
}
document.onmousemove = movehandler;
// - End of JavaScript - -->
</script>
</body>
<br><b>Is this effect very cool? </b></br>
</html>
I hope this article will be helpful to everyone's JavaScript programming.