


[JS+CSS3] Sample code to implement slideshow effect with preview_javascript skills
1. Early preparation
1.1 Case Analysis
Applicable scenario: singleton layout
1.2 Methodology
V view HTML+CSS+Debug
C js implements control flow
D data optimization extension
2. Code
Structure
<div class="slider"><!-- 特效区 --> <div class="main"><!-- 主视图区 --> <div class="main_i"> <div class="caption"> <h2>h2 caption</h2> <h2>h3 caption</h2> </div> <img src="images/{{index}}.jpg" alt=""> </div> </div><!-- 主视图区结束 --> <div class="ctrl"><!-- 控制区 --> <a href="javascript:;"><img src="images/{{index}}.jpg" alt=""> </a> </div><!-- 控制区结束 --> </div><!-- 特效区结束 -->
Style (CSS omitted)
Script function development
>>Content output
Template transformation, output slideshow & control button, picture position adjustment
>>Switch control
Switch slides .main_i_active Switch control buttons .ctrl_i_active
0. Modify VIEW ->Template (keyword replacement) and add Template id
Picture area
{{h2}}}
{{h3}}}

Button area

The following is the key point of JS script writing~~
<script type="text/javascript"> // 1、数据定义(实际生产环境,应由后台给出) var data = [ {img:1,h2:"我是标题二,小标题",h3:"我是标题三,大标题"}, {img:2,h2:"我是标题二,小标题",h3:"我是标题三,大标题"}, {img:3,h2:"我是标题二,小标题",h3:"我是标题三,大标题"}, {img:4,h2:"我是标题二,小标题",h3:"我是标题三,大标题"}, {img:5,h2:"我是标题二,小标题",h3:"我是标题三,大标题"}, {img:6,h2:"我是标题二,小标题",h3:"我是标题三,大标题"}, {img:7,h2:"我是标题二,小标题",h3:"我是标题三,大标题"}, ]; // 2、通用函数 var g = function(id){ if( id.substr(0,1) =="." ){ return document.getElementsByClassName( id.substr(1) ); } return document.getElementById(id); } // 3、添加幻灯片的操作(所有幻灯片&对应的按钮) function addSliders(){ // 3.1 获取模版 var tpl_main = g("template_main").innerHTML .replace(/^\s*/,'') .replace(/\s*$/,''); var tpl_ctrl = g("template_ctrl").innerHTML .replace(/^\s*/,'') .replace(/\s*$/,''); // 3.2 定义最终输出HTML的变量 var out_main = []; var out_ctrl = []; // 3.3 遍历所有数据,构建最终输出的HTML for( i in data ){ var _html_main = tpl_main .replace(/{{index}}/g,data[i].img) .replace(/{{h2}}/g,data[i].h2) .replace(/{{h3}}/g,data[i].h3) .replace(/{{css}}/g,['','main_i_right'][i%2]); var _html_ctrl = tpl_ctrl .replace(/{{index}}/g,data[i].img); out_main.push(_html_main); out_ctrl.push(_html_ctrl); } // 3.4 把HTML回写到对应的DOM里面 g("template_main").innerHTML = out_main.join(''); g("template_ctrl").innerHTML = out_ctrl.join(''); // 7、增加#main_background g('template_main').innerHTML += tpl_main .replace(/{{index}}/g,'{{index}}') .replace(/{{h2}}/g,data[i].h2) .replace(/{{h3}}/g,data[i].h3); g('main_{{index}}').id = 'main_background'; } // 5、幻灯片切换 function switchSliders(n){ // 5.1 获得要展现的幻灯片&控制按钮 DOM var main = g("main_"+n); var ctrl = g("ctrl_"+n); // 5.2 获得所有的幻灯片&控制按钮 var clear_main = g('.main_i'); var clear_ctrl = g('.ctrl_i'); // 5.3 清除他们的active样式 for(var i=0;i<clear_ctrl.length;i++){ clear_main[i].className = clear_main[i].className.replace('main_i_active',''); clear_ctrl[i].className = clear_ctrl[i].className.replace('ctrl_i_active',''); } // 5.4为当前控制按钮和幻灯片附加样式 g("main_"+n).className += ' main_i_active'; g("ctrl_"+n).className += ' ctrl_i_active'; // 7.2切换时 复制上一张幻灯片到main_background中 setTimeout(function(){ g('main_background').innerHTML = main.innerHTML; },1000); } // 6、动态调整图片的margin-top 使其垂直居中 function movePictures(){ var pictures = g('.picture'); for(i=0;i<pictures.length;i++){ pictures[i].style.marginTop = -(pictures[i].clientHeight/3) + 'px'; } } // 4、定义何时处理幻灯片输出 window.onload = function(){ addSliders(); switchSliders(1); setTimeout(function(){ movePictures(); },100) } </script>
Renderings… = =Don’t complain about these pictures again~~~
Encountered a problem:
1. Add top:50% directly to the picture; there will be a bug and it will not work. The reason may be that the height is not fixed~~ Just give a fixed height
But the problem comes again. 2. After the height is fixed, the button group cannot adapt to the width... and will be squeezed into the second row...
The above [JS+CSS3] sample code to implement the slideshow effect with preview is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Script Home.

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

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

1. Open the PPT slide document, and in the [Outline, Slide] column on the left side of the PPT, click to select the slide where you want to insert the basic [shape]. 2. After selecting, select the [Insert] menu in the function menu bar above the PPT and click. 3. After clicking the [Insert] menu, the [Insert] submenu bar pops up below the function menu bar. Select the [Shape] menu in the [Insert] submenu bar. 4. Click the [Shape] menu to pop up the preset shape type selection page. 5. In the shape type selection page, select the [Heptagon] shape and click. 6. After clicking, move the mouse to the slide editing interface, press the mouse to draw, and release the mouse after the drawing is completed. Complete the insertion of the [heptagon] shape.

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

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

1. The color scheme is naturally related to design, so we click on the design toolbar and on the right side, you can see the color options. Point the mouse up, and you can see that the current theme color is: Default design template 1. 2. Click on this option, and the first one is the current color design. To change that, we're going to go down below the panel: New Theme Colors. 3. Click in, and in the column of colors that appear, you can see that there are three accent text colors, all of which are light and almost impossible to display on a light background. Among them is the text color in this example. 4. Click the drop-down arrow on the right side of the color to display the color selection panel. In order not to destroy the overall color matching, choose a dark color in the primary color system to replace the primary color. Replace the other two colors in the same way. 5. Compare the changed color with the original color. On the left is

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:
