Javascript achieves image carousel effect (1) beating pictures _ javascript skills

Source: Internet
Author: User
The image carousel effect can be seen on the homepage of all major websites. It is quite common. The following section describes how Javascript achieves image carousel effect (1) to beat pictures, if you need a friend, refer to the image carousel effect, which can be seen on the homepage of each major website, which is quite common. The following is a simple implementation of this effect.

1. The picture jumps

2. Implementation of Image Sequence Control

3. Implementation of front and back button control

In this article, we will see that the image is switched according to the interval.

Let's first complete the structure code, so I will not explain it in detail. Let's show it to you:

The Code is as follows:

 

  1. 1
  2. 2
  3. 3
Next

Okay. Now we can use JS to control the image jump.

First, we need to find the location of the image. Here we use ul for layout, so we need to first find the number of LI under UL.

Then let the image be displayed one by one. We use the window mode, that is, the mask layer mode. # slider is a window, ul is a view outside the window, and ul's view is horizontally formatted

Then let the outside view be the size of the window, that is, let each image be the size of each window, here is to control the size of the image to be the same as the size of the window.

The above is a concept, that is, layout processing. Now we have javascript control to achieve different display intervals for images. we need to use setInterval or setTimeout of JS. here we use setInterval (because it is convenient to use .)

Each jump, we control the left of UL's position, so that the scenery under ul is displayed differently at each time, the left is determined based on the window width. The first left is of course-800*0, the second is-800*1, and the third is-800*2... and so on. then we can get the following code:

《script》(function(){var slider = document.getElementById("slider");var imgul = slider.getElementsByTagName("ul")[0];var imglis = imgul.getElementsByTagName("li");var len = imglis.length;var index = 0;setInterval(function(){if(index >= len){index = 0;}imgul.style.left = - (800 * index) + "px";index++;},2000);})();《script》

The JS Code looks very simple. Here I set 2 seconds to jump in sequence, and then the number of jumps is greater than or equal to the number of images, let it return to the first image.

The above content is a small Editor to introduce you to the Javascript Implementation of image carousel effect (1) Let the picture beat all the content, I hope to help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.