Home >Web Front-end >JS Tutorial >How to achieve fade-in and fade-out effect in js_javascript skills

How to achieve fade-in and fade-out effect in js_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:33:281904browse

The fade in and fade out 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 has been encapsulated and is useful. Friends, you can use it directly. There is also a method for setting element transparency in the code. It is set according to IE rules (0~100). If it is changed to the standard setting method (0.00~1.00), please consider floating point to accurately express the difference when using it below.

Parameter description:
Both fadeIn() and fadeOut() have three parameters. The first one is event, which is required; the second one is fade speed, which is a positive integer. You can weigh the size by yourself. It is optional. Parameter; the third one is to specify the transparency value to fade in and out to (similar to fadeTo() in jQuery), a positive integer value from 0 to 100, which is also an optional parameter.
Key code:

 //淡入效果(含淡入到指定透明度) 
 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'; 
   } 
  })(); 
 } 

Rendering:

Core code, you can

copy the code directly to see the effect

 
 
 
 
原生JS实现淡入淡出效果 
 
<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==&gt;需要淡入的元素 
   * speed==&gt;淡入速度,正整数(可选) 
   * opacity==&gt;淡入到指定的透明度,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 &lt;= opacity) { 
    setTimeout(arguments.callee, speed) 
   } 
  })(); 
 } 
  
 //淡出效果(含淡出到指定透明度) 
 function fadeOut(elem, speed, opacity){ 
  /* 
   * 参数说明 
   * elem==&gt;需要淡入的元素 
   * speed==&gt;淡入速度,正整数(可选) 
   * opacity==&gt;淡入到指定的透明度,0~100(可选) 
   */ 
  speedspeed = speed || 20; 
  opacityopacity = opacity || 0; 
  //初始化透明度变化值为0 
  var val = 100; 
  //循环将透明值以5递减,即淡出效果 
  (function(){ 
   iBase.SetOpacity(elem, val); 
   val -= 5; 
   if (val &gt;= opacity) { 
    setTimeout(arguments.callee, speed); 
   }else if (val &lt; 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> 
 
 
 
 

脚本之家是国内专业的网站建设资源、脚本编程学习类网站,提供asp、php、asp.net、javascript、jquery、vbscript、dos批处理、网页制作、网络编程、网站建设等编程资料。

脚本之家

脚本之家是国内专业的网站建设资源、脚本编程学习类网站,提供asp、php、asp.net、javascript、jquery、vbscript、dos批处理、网页制作、网络编程、网站建设等编程资料。

脚本之家

脚本之家是国内专业的网站建设资源、脚本编程学习类网站,提供asp、php、asp.net、javascript、jquery、vbscript、dos批处理、网页制作、网络编程、网站建设等编程资料。

The above is all the code for realizing the fade-in and fade-out effect in native js. I hope it will be helpful to everyone's learning.

Statement:
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