


Native js and jQuery implement fade-in and fade-out carousel effect_jquery
The example in this article introduces the key code for realizing the fade-in and fade-out carousel effect based on jQuery, and shares it with you for your reference. The specific content is as follows:
Basic principle: Position all pictures absolutely at the same position, set the transparency to 0, and then use jQuery's fade-in and fade-out to achieve the picture switching effect.
html code:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>一个轮播</title> <style> #scrollPlay{ width: 730px; height: 336px; /*overflow: hidden;*/ } #pre{ position: absolute; margin-top: 150px; width:30px; height: 30px; background: #000; color:#fff; opacity: 0.7; text-align: center; line-height: 30px; font-size: 20px; z-index: 10; } img{ opacity: 0; position: absolute; } #next{ position: absolute; margin-left:700px; margin-top: 150px; width:30px; height: 30px; background: #000; color:#fff; opacity: 0.7; text-align: center; line-height: 30px; font-size: 20px; z-index: 10; } span{ display: block; width: 15px; height: 15px; float: left; border: 1px solid #fff; } #buttons{ position: absolute; background: #000; margin-top: 300px; margin-left: 300px; z-index: 10; } .onactive{ background: green; } </style> <script src='jquery.js'></script> <script src='index.js'></script> </head> <body> <div id='scrollPlay'> <div id='buttons'> <span index = 0 class='onactive'></span> <span index = 1></span> <span index = 2></span> <span index = 3></span> <span index = 4></span> </div> <div id='pre'><</div> <div id='next'>></div> <img src='images/1.jpg' alt='pics' style='opacity:1'> <img src='images/2.jpg' alt='pics'> <img src='images/3.jpg' alt='pics'> <img src='images/4.jpg' alt='pics'> <img src='images/5.jpg' alt='pics'> </div> </body> </html>
JS:
$(function(){ var index = 0; var flag = false; //用于判断是否处于动画状态 //切换函数 function move(offset){ flag=true; //console.log(offset) $('img').eq(index).fadeOut('slow',function(){ if(offset>0){ if(index ==4){ index=0; }else{ //console.log(index); index=index+offset; //console.log(index); } } if(offset<0){ if(index==0){ index=4; }else{ index=index+offset } } $('img').eq(index).fadeTo('slow',1) //使用fadeIn不成功:$('img').eq(index).fadeIn('slow') showButton(); flag=false; }); } //点击切换上一张 $('#pre').click(function(){ if(flag==false){ move(-1) } }) //点击切换下一张 $('#next').click(function(){ if(flag==false){ move(1) } }) //点击按钮直接切换 $('span').click(function(){ if(flag==false){ var i= $(this).attr('index') //console.log(i) //console.log(index) //console.log(i-index) move(i-index) showButton(); } }) //高亮显示按钮 function showButton(){ if($('span').hasClass('onactive')){ $('span').removeClass(); } $('span').eq(index).addClass('onactive') } //自动播放 var timer; function go(){ timer = setInterval(function(){ $('#next').trigger('click'); },3000) } //鼠标悬停,清除自动播放 $('#scrollPlay').mouseover(function(){ clearInterval(timer) }) //鼠标移开,开始自动播放 $('#scrollPlay').mouseout(function(){ go(); }) go(); })
At the end of the article, I raised a small question for you, and I hope you can give me a solution.
There is no effect when using fadeIn to fade in. In the end, it can only be achieved by using fadeTo. What is the reason?
Let me share with you a small example: Native JS implements fade-in/fade-out effect (fadeIn/fadeOut/fadeTo)
The fade effect is often used in daily projects. Unfortunately, native JS does not have a similar method, and sometimes small pages are not worth introducing a jQuery library, so I wrote one myself. It is encapsulated and useful. Friends who have it can use it directly. There is also a method to set the transparency of elements in the code, which is set according to IE rules (0~100). If it is changed to the standard setting method (0.00~1.00), please consider floating when using it below. Point to express the difference accurately.
Parameter description:
Both fadeIn() and fadeOut() have three parameters. The first is the event, which is required; the second is the fade-in and fade-out speed, a positive integer, the size is weighed by yourself, and is optional; the third is the specified fade-in and fade-out point. Transparency value (similar to fadeTo() in jQuery), a positive integer value from 0 to 100, which is also an optional parameter.
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>原生JS实现淡入淡出效果</title> <style> /*demo css*/ #demo div.box {float:left;width:31%;margin:0 1%} #demo div.box h2{margin-bottom:10px} #demo div.box h2 input{padding:5px 8px;font-size:14px;font-weight:bolder} #demo div.box div{text-indent:10px; line-height:22px;border:2px solid #555;padding:0.5em;overflow:hidden} </style> <script> window.onload = function(){ //底层共用 var iBase = { Id: function(name){ return document.getElementById(name); }, //设置元素透明度,透明度值按IE规则计,即0~100 SetOpacity: function(ev, v){ ev.filters ? ev.style.filter = 'alpha(opacity=' + v + ')' : ev.style.opacity = v / 100; } } //淡入效果(含淡入到指定透明度) function fadeIn(elem, speed, opacity){ /* * 参数说明 * elem==>需要淡入的元素 * speed==>淡入速度,正整数(可选) * opacity==>淡入到指定的透明度,0~100(可选) */ speedspeed = speed || 20; opacityopacity = opacity || 100; //显示元素,并将元素值为0透明度(不可见) elem.style.display = 'block'; iBase.SetOpacity(elem, 0); //初始化透明度变化值为0 var val = 0; //循环将透明值以5递增,即淡入效果 (function(){ iBase.SetOpacity(elem, val); val += 5; if (val <= opacity) { setTimeout(arguments.callee, speed) } })(); } //淡出效果(含淡出到指定透明度) function fadeOut(elem, speed, opacity){ /* * 参数说明 * elem==>需要淡入的元素 * speed==>淡入速度,正整数(可选) * opacity==>淡入到指定的透明度,0~100(可选) */ speedspeed = speed || 20; opacityopacity = opacity || 0; //初始化透明度变化值为0 var val = 100; //循环将透明值以5递减,即淡出效果 (function(){ iBase.SetOpacity(elem, val); val -= 5; if (val >= opacity) { setTimeout(arguments.callee, speed); }else if (val < 0) { //元素透明度为0后隐藏元素 elem.style.display = 'none'; } })(); } var btns = iBase.Id('demo').getElementsByTagName('input'); btns[0].onclick = function(){ fadeIn(iBase.Id('fadeIn')); } btns[1].onclick = function(){ fadeOut(iBase.Id('fadeOut'),40); } btns[2].onclick = function(){ fadeOut(iBase.Id('fadeTo'), 20, 10); } } </script> </head> <body> <!--DEMO start--> <div id="demo"> <div class="box"> <h2><input type="button" value="点击淡入" /></h2> <div id="fadeIn" style="display:none"> <p>脚本之家</p> <p>www.jb51.net</p> </div> <p>脚本之家是国内专业的网站建设资源.</p> </div> <div class="box"> <h2><input type="button" value="点击淡出" /></h2> <div id="fadeOut"> <p>脚本之家</p> <p>www.jb51.net</p> </div> <p>脚本之家是国内专业的网站建设资源.</p> </div> <div class="box"> <h2><input type="button" value="点击淡出至指定透明度" /></h2> <div id="fadeTo"> </div> <p>脚本之家是国内专业的网站建设资源.</p> </div> </div> <!--DEMO end--> </body> </html>
The above is the entire content of this article. I hope it will be helpful for everyone to learn native js and jQuery to achieve the fade-in and fade-out carousel effect.

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s
