te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>fade effect using javascript no jquery? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

fade effect using javascript no jquery? - Stack Overflow

programmeradmin2浏览0评论

jQuery derived from javascript have beautiful method of creating fade effect.

But how can I achieve same height using my own custom javascript code?

I wanted the most simple one.. which fades out to zero.

jQuery derived from javascript have beautiful method of creating fade effect.

But how can I achieve same height using my own custom javascript code?

I wanted the most simple one.. which fades out to zero.

Share Improve this question edited Feb 25, 2011 at 3:46 Zed 3,38521 silver badges14 bronze badges asked Feb 24, 2011 at 11:32 HTweaksHTweaks 811 gold badge1 silver badge4 bronze badges 5
  • 5 why you want to reinvent the wheel by yourself? – Furqan Hameedi Commented Feb 24, 2011 at 11:36
  • 7 Maybe to get a lightweight app, jquery is "kind of" heavy paring to a 10 line script ... reinvent the wheel no, but avoid killing a bee with a flame-thrower yes ... – yent Commented Feb 24, 2011 at 11:41
  • I'll forward the same question to the one who've assigned me this project ;). And obviously, it will be credited to you. – HTweaks Commented Feb 24, 2011 at 11:41
  • If you want to know how to write the fade effect in pure JavaScript then I remend reading this source code because they did it really well. – Zed Commented Feb 24, 2011 at 11:44
  • Possible duplicate of Fade HTML element with raw javascript – rogerdpack Commented Apr 17, 2017 at 17:04
Add a ment  | 

5 Answers 5

Reset to default 4

Here is a pure implementation of fade in and fade out effect using JavaScript.

  1. Make use of CSS Opacity property

  2. During fade out process, we reduce the opacity value from 0.9 to 0

  3. Similarly fade in process increase the 0.0 to 0.9

  4. For IE its 10 to 90

  5. Use setTimeout() function to call the fade function recursively with delay and increase / decrease opacity value to achieve the fade effect

      function fadeOut(id,val){ if(isNaN(val)){ val = 9;}
      document.getElementById(id).style.opacity='0.'+val;
      //For IE
      document.getElementById(id).style.filter='alpha(opacity='+val+'0)';
      if(val>0){
        val--;
        setTimeout('fadeOut("'+id+'",'+val+')',90);
      }else{return;}
    }
    
    function fadeIn(id,val){
    // ID of the element to fade, Fade value[min value is 0]
      if(isNaN(val)){ val = 0;}
      document.getElementById(id).style.opacity='0.'+val;
      //For IE
      document.getElementById(id).style.filter='alpha(opacity='+val+'0)';
      if(val<9){
        val++;
        setTimeout('fadeIn("'+id+'",'+val+')',90);
      }else{return;}
    }
    

Cheers -Clain

Try this :

function fade(what, duration) {
  what.opct = 100;
  what.ih = window.setInterval(function() {
    what.opct--;
    if(what.opct) {
      what.MozOpacity = what.opct / 100;
      what.KhtmlOpacity = what.opct / 100;
      what.filter = "alpha(opacity=" + what.opct + ")";
      what.opacity = what.opct / 100;
    }else{
      window.clearInterval(what.ih);
      what.style.display = 'none';
    }
  }, 10 * duration);
}

Use it like :

fade(htmlobject, 2); // delay is in second

Creating animations is quite a delicate task. You have to take care of browser differences in handling CSS properties. Then you have to be sure you know how to work with timers, because they are usually not very accurate in Javascript. In short it will be easy to write a simple fading effect, but it will take a fair amount of work to make one that is parable to jQuery.

You can read this (well, you have to wait for it to be finished) to have a better idea of how jQuery is structured, and then try to rool your own.

you can define a color with hexadcimal system or ordinary decimal system. An example that uses the hexadecimal system

BODY {background-color:#FF00FF;}

and an example that uses the decimal system

BODY {background-color:rgb(51,0,102)}

The definition rgb(255,255,255) represents the color white, the code rgb(0,0,0) represents black.

So how you can made a fade effekt? Well, the easiest way is to use the way with decimal code:

<script type="text/javascript">
 var red = 255;
 var green = 255;
 var blue = 255;
 var interVal = window.setInterval("fadeEffect()", 1000);
 function fadeEffect() {
  if (red > 0 && green > 0 && blue > 0) red--;
  if (red == 0 && green > 0 && blue > 0) green--;
  if (red == 0 && green == 0 && blue > 0) blue--;
  if (red == 0 && green == 0 && blue == 0) window.clearInterval(interVal);

  //Creates the new code of color
  colorAttr = "rgb("+red+","+green+","+blue+")";
  //However or whereever you make the new color public
  ...

  //Reset the first two colors
  if (red == 0) red == 255;
  if (green == 0) green = 255;
 }
</script>

Hopefully it will answer your question or help you to e up with your own idea. If you want to use hexadecimal numbers, then you had to convert into hexa code before you had created the new argument.

Why not using CSS3 transitions?

...

opacity: 100;
-webkit-transition: opacity .5s ease-out;
-moz-transition: opacity .5s ease-out;
-o-transition: opacity .5s ease-out;
transition: opacity .5s ease-out;
发布评论

评论列表(0)

  1. 暂无评论