Home > Web Front-end > JS Tutorial > body text

Native js and jQuery implement fade-in and fade-out carousel effect_jquery

WBOY
Release: 2016-05-16 15:23:28
Original
1498 people have browsed it

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'>&lt</div>
 <div id='next'>&gt</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>
Copy after login

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(); 
})

Copy after login

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 &#63; 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> 
Copy after login

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.

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template