fadeIn.html 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  6. <title>Document</title>
  7. <!--[if lt IE 9]>
  8. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  9. <![endif]-->
  10. </head>
  11. <body>
  12. <div id="test" style="width: 100px; height: 100px; background: red"></div>
  13. <script type="text/javascript">
  14. var fadeIn = function(element, options) {
  15. var start = new Date,
  16. from = 0,
  17. intvl = setInterval(function() {
  18. var passed = new Date - start,
  19. progress = passed / options.duration;
  20. element.style.opacity = from + progress;
  21. if (progress >= 1) {
  22. clearInterval(intvl);
  23. options.complete();
  24. }
  25. }, options.delay || 10);
  26. }
  27. fadeIn(document.getElementById('test'), {
  28. duration: 2000,
  29. complete: function() {
  30. alert('Complete');
  31. }
  32. });
  33. </script>
  34. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  35. </body>
  36. </html>