A brief analysis of how to implement simple carousel using js
Design idea: Use the timer in js to achieve the effect of picture carousel, and put all pictures into the img folder. I stored three photos at the time. Then put the three photos into three divs respectively. The display and hiding of each div is controlled by a timer. The div with three numbers in the lower left corner represents which picture this is. There are three pictures in total, so there are three div.
Code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style type="text/css"> .imgbox{ width: 100%; height:400px; background-color: black; transform: 1s; } .img{ width: 100%; height:100%; background-image: url(img/37fa7b724f5c49cd8c2d0efad885f1a8.jpeg); background-repeat:no-repeat; background-size:cover ; } .img0{ width: 100%; height:100%; background-image: url(img/37fa7b724f5c49cd8c2d0efad885f1a8.jpeg); background-repeat:no-repeat; background-size:100% ; } .img1{ width: 100%; height:100%; background-image: url(img/5572568_110213373115_2.jpg); background-repeat:no-repeat; background-size:100% ; } .img2{ width: 100%; height:100%; background-image: url(img/5875f5fb7a8f8.jpg); background-repeat:no-repeat; background-size:100% ; } .img3{ width: 100%; height:100%; background-image: url(img/980.jpg); background-repeat:no-repeat; background-size:100% ; } ul{ margin-left:-30px ; list-style-type: none; position: relative; margin-top: -100px; line-height: 50px; } ul li{ float: left; width: 50px; height:50px; border:1px solid #000000; text-align: center; background-color: aliceblue; } .div{ background-color: orange; line-height: 50px; } .div1{ background-color: gainsboro; line-height: 50px; } </style> <script type="text/javascript"> var i=0; function imgbox(){ i++; if(i<4){ document.getElementById("img").className="img"+i; if(i==1){ document.getElementById("one").className="div"; document.getElementById("two").className="div1"; document.getElementById("three").className="div1"; } if(i==2){ document.getElementById("one").className="div1"; document.getElementById("two").className="div"; document.getElementById("three").className="div1"; } if(i==3){ document.getElementById("one").className="div1"; document.getElementById("two").className="div1"; document.getElementById("three").className="div"; } } if(i==4){ i=0; clearInterval('imgbox()'); } } setInterval('imgbox()',1000); </script> </head> <body> <div class="imgbox"> <div class="img" id="img"></div> <ul id="ul"> <li id="one">1</li> <li id="two">2</li> <li id="three">3</li> </ul> </div> </body> </html>
Understand HTML
HTML is what we call a web page, and the file format of a web page is .html format. In our eyes, it is a hypertext language that does not require additional compilation or processing. Once written and opened, it is a web page.
Html consists of many tags such as
, ..., and some semantic tags such as
What is js:
js (JavaScript) is a literal scripting language. JavaScript scripts can be placed directly in HTML language and run on browsers that support js. It is widely used in web application development and is often used to add various dynamic functions to web pages to provide users with smoother and more beautiful browsing effects. When a certain operation is performed while browsing a web page, an event is generated, and a program written in JavaScript can respond to the corresponding event.
js timer: Define the timer setInterval('imgbox()',1000); to execute the imgbox method every second. The imgbox method includes changes to the image and the color of the div
Enable timer
The window object provides two methods to achieve the effect of the timer, namely window.setTimeout() and window.setInterval. The former can make a piece of code run after a specified time; while the latter can make a piece of code run once every specified time. Their prototypes are as follows:
window.setTimeout(code,millisec); window.setInterval(code,millisec);
Among them, code can be a piece of code enclosed in quotation marks, or it can be a function name. At the specified time, the system will automatically call the function. When using the function name as When calling a handle, it cannot take any parameters; when using a string, you can write the parameters to be passed in it. The second parameter in both methods is millisec, which represents the number of milliseconds for delay or repeated execution.
The specific writing method is as follows:
Function name, without parameter setTimeout (test,1000); string, executable code setTimeout ('test()', 1000); Anonymous function setTimeout (function(){},1000); Note: The usage of setInterval is the same as setTimeout
Call the function with parameter setTimeout ('test(parameter)',1000);
div layout: use ul, li for layout
Modify the style as follows:
ul{ margin-left:-30px ;//据左部边距 list-style-type: none;//去除默认ul样式 position: relative;//采用相对定位 margin-top: -100px;//据顶部边距 line-height: 50px;//行高 } ul li{ float: left;//浮动 width: 50px; height:50px; border:1px solid #000000;//边框 text-align: center;//文字居中 background-color: aliceblue; }
Html structure:
<div> <div id="img"></div> <ul id="ul"> <li id="one">1</li> <li id="two">2</li> <li id="three">3</li> </ul> </div>
Img is a large div box, img is a picture,
- It contains the picture number li.
The above is the detailed content of A brief analysis of how to implement simple carousel using js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

How to use JS and Baidu Maps to implement map polygon drawing function. In modern web development, map applications have become one of the common functions. Drawing polygons on the map can help us mark specific areas for users to view and analyze. This article will introduce how to use JS and Baidu Map API to implement map polygon drawing function, and provide specific code examples. First, we need to introduce Baidu Map API. You can use the following code to import the JavaScript of Baidu Map API in an HTML file
