Home Web Front-end JS Tutorial js method to realize text moving according to mouse movement_javascript skills

js method to realize text moving according to mouse movement_javascript skills

May 16, 2016 pm 04:12 PM
js letter method move follow mouse

The example in this article describes how js implements text movement following mouse movement. Share it with everyone for your reference. The specific analysis is as follows:

This is a very simple mouse feature code. When you move the mouse on a web page, a string of text will follow the mouse movement

Copy code The code is as follows:



<script><br> var x,y<br> var step=18 //This is the interval between two adjacent words <br> var flag=0<br> var message="Script House www.jb51.net welcomes you!" //Put the text that needs to be displayed here<br> message=message.split("") //Split the string into an array<br> var xpos=new Array() //Create an array to record the x coordinate of each position<br> for (i=0;i<=message.length-1;i) { //Assign an initial value to each element<br /> xpos[i]=-50<br /> }<br /> var ypos=new Array() //Create an array to record the y coordinate of each position<br /> for (i=0;i<=message.length-1;i) {<br /> ypos[i]=-200<br /> }<br /> function movehandler(e){ //Handle mouse move events<br /> x = (document.layers) ? e.pageX : document.body.scrollLeft event.clientX //According to different browsers, record the horizontal position of the mouse <br /> y = (document.layers) ? e.pageY : document.body.scrollTop event.clientY //Record the vertical position of the mouse<br /> flag=1 //The mouse position has changed and needs to be recalculated <br /> }<br /> function makesnake() {<br /> if (flag==1 && document.all) { //If it is IE<br /> for (i=message.length-1; i>=1; i--) { //Process coordinate queue<br> xpos[i]=xpos[i-1] step //Move each coordinate data forward one space and add character spacing <br> ypos[i]=ypos[i-1]<br> }<br> xpos[0]=x step //Write new data into the end of the coordinate data queue<br> ypos[0]=y<br> for (i=0; i<message.length-1; i ) {<br /> var thisspan = eval("span" (i) ".style")// Generate the current operation object spanx.style<br /> thisspan.posLeft=xpos[i]<br /> thisspan.posTop=ypos[i]<br /> }<br /> }<br /> else if (flag==1 && document.layers) { //If it is NS<br /> for (i=message.length-1; i>=1; i--) { //Process coordinate queue<br> xpos[i]=xpos[i-1] step //Move each coordinate data forward one space and add character spacing <br> ypos[i]=ypos[i-1]<br> }<br> xpos[0]=x step //Write new data into the end of the coordinate data queue<br> ypos[0]=y<br> for (i=0; i<message.length-1; i ) { //Change the coordinates of the layer where each word is located according to the data in the array <br /> var thisspan = eval("document.span" i) //Generate the current operation object document.spanx<br /> thisspan.left=xpos[i]<br /> thisspan.top=ypos[i]<br /> }<br /> }<br /> var timer=setTimeout("makesnake()",30) //After 30 milliseconds, adjust the position of each character according to the situation again<br /> }<br /> </script>


<script><br> <!-- Beginning of JavaScript -<br /> //Generate a span as a container for each word here <br /> for (i=0;i<=message.length-1;i) {<br /> document.write("<span id='span" i "' class='spanstyle'>")<br> document.write(message[i])<br> document.write("</span>")<br> }<br> //Specify the processing process of mouse movement events<br> if (document.layers){<br> document.captureEvents(Event.MOUSEMOVE);<br> }<br> document.onmousemove = movehandler;<br> // - End of JavaScript - --><br> </script>


Isn’t this effect cool?

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make round pictures and text in ppt How to make round pictures and text in ppt Mar 26, 2024 am 10:23 AM

First, draw a circle in PPT, then insert a text box and enter text content. Finally, set the fill and outline of the text box to None to complete the production of circular pictures and text.

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

Tomato Novel is a very popular novel reading software. We often have new novels and comics to read in Tomato Novel. Every novel and comic is very interesting. Many friends also want to write novels. Earn pocket money and edit the content of the novel you want to write into text. So how do we write the novel in it? My friends don’t know, so let’s go to this site together. Let’s take some time to look at an introduction to how to write a novel. Share the Tomato novel tutorial on how to write a novel. 1. First open the Tomato free novel app on your mobile phone and click on Personal Center - Writer Center. 2. Jump to the Tomato Writer Assistant page - click on Create a new book at the end of the novel.

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

HP launches Professor 1 three-mode soft mouse: 4000DPI, Blue Shadow RAW3220, initial price 99 yuan HP launches Professor 1 three-mode soft mouse: 4000DPI, Blue Shadow RAW3220, initial price 99 yuan Apr 01, 2024 am 09:11 AM

According to news from this website on March 31, HP recently launched a Professor1 three-mode Bluetooth mouse on JD.com, available in black and white milk tea colors, with an initial price of 99 yuan, and a deposit of 10 yuan is required. According to reports, this mouse weighs 106 grams, adopts ergonomic design, measures 127.02x79.59x51.15mm, has seven optional 4000DPI levels, is equipped with a Blue Shadow RAW3220 sensor, and uses a 650 mAh battery. It is said that it can be used on a single charge. 2 months. The mouse parameter information attached to this site is as follows:

Razer | Pokémon Gengar wireless mouse and mouse pad are now available, with a set price of 1,549 yuan Razer | Pokémon Gengar wireless mouse and mouse pad are now available, with a set price of 1,549 yuan Jul 19, 2024 am 04:17 AM

According to news from this site on July 12, Razer today announced the launch of the Razer|Pokémon Gengar wireless mouse and mouse pad. The single product prices are 1,299 yuan and 299 yuan respectively, and the package price including the two products is 1,549 yuan. This is not the first time that Razer has launched Gengar co-branded peripheral products. In 2023, Razer launched the Gengar-style Yamata Orochi V2 gaming mouse. The two new products launched this time all use a dark purple background similar to the appearance of the Ghost, Ghost, and Gengar families. They are printed with the outlines of these three Pokémon and Poké Balls, with the character Gengar in the middle. A large, colorful image of a classic ghost-type Pokémon. This site found that the Razer|Pokémon Gengar wireless mouse is based on the previously released Viper V3 Professional Edition. Its overall weight is 55g and equipped with Razer’s second-generation FOC

VGN co-branded 'Elden's Circle' keyboard and mouse series products are now on the shelves: Lani / Faded One custom theme, starting from 99 yuan VGN co-branded 'Elden's Circle' keyboard and mouse series products are now on the shelves: Lani / Faded One custom theme, starting from 99 yuan Aug 12, 2024 pm 10:45 PM

According to news from this site on August 12, VGN launched the co-branded "Elden Ring" keyboard and mouse series on August 6, including keyboards, mice and mouse pads, designed with a customized theme of Lani/Faded One. The current series of products It has been put on JD.com, priced from 99 yuan. The co-branded new product information attached to this site is as follows: VGN丨Elden Law Ring S99PRO Keyboard This keyboard uses a pure aluminum alloy shell, supplemented by a five-layer silencer structure, uses a GASKET leaf spring structure, has a single-key slotted PCB, and the original height PBT material Keycaps, aluminum alloy personalized backplane; supports three-mode connection and SMARTSPEEDX low-latency technology; connected to VHUB, it can manage multiple devices in one stop, starting at 549 yuan. VGN丨Elden French Ring F1PROMAX wireless mouse the mouse

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Mar 23, 2024 am 10:42 AM

In today's society, mobile phones have become an indispensable part of our lives. As an important tool for our daily communication, work, and life, WeChat is often used. However, it may be necessary to separate two WeChat accounts when handling different transactions, which requires the mobile phone to support logging in to two WeChat accounts at the same time. As a well-known domestic brand, Huawei mobile phones are used by many people. So what is the method to open two WeChat accounts on Huawei mobile phones? Let’s reveal the secret of this method. First of all, you need to use two WeChat accounts at the same time on your Huawei mobile phone. The easiest way is to

See all articles