Home  >  Article  >  Web Front-end  >  How to use javascript to automatically transform images

How to use javascript to automatically transform images

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-04-08 09:58:347675browse

Method: 1. Create an array of image paths; 2. Use timeInterval to define the switching time, and setInterval() to set the time to execute the function regularly; 3. Obtain the img object, set the initial curIndex, and compare the image names with the if statement to achieve continuous Change the image name and then change the image path to achieve a switching effect.

How to use javascript to automatically transform images

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.

How to use JavaScript to automatically transform images

1. Create a new html file, named test.html, to explain how to use javascript Realize scheduled picture switching.

How to use javascript to automatically transform images

2. In test.html, use the img tag to create a picture display, and set its id attribute to obj, so that the element object can be obtained below.

How to use javascript to automatically transform images

3. In the script tag of the test.html page, use new Array() to create an array of image paths and put the file names of the three images in it.

How to use javascript to automatically transform images

4. In the script tag of the test.html page, use the variable timeInterval to define the image switching time as 1 second, and set it every second through the setInterval() timer method. Execute the changeImg() function once.

How to use javascript to automatically transform images

5. In the changeImg() function, use the document.getElementById() method to obtain the img object, set an initial curIndex variable, and compare the names of the pictures through the if statement to achieve Keep changing the name of the picture.

Note: The name of the picture must be an ordered number such as 0, 1, 2, etc.

How to use javascript to automatically transform images

#6. In the changeImg() function, use the image name obtained in the previous step to change the image path of img, and finally achieve regular switching of images.

How to use javascript to automatically transform images

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of How to use javascript to automatically transform images. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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