欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery实现的下雪动画效果示例【附源码下载】

程序员文章站 2024-01-13 22:59:04
本文实例讲述了jquery实现的下雪动画效果。分享给大家供大家参考,具体如下: html部分:

本文实例讲述了jquery实现的下雪动画效果。分享给大家供大家参考,具体如下:

html部分:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes" />
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <!-- snow -->
    <script src="js/jquery.snow.js"></script>
  </head>
  <body>
    <div id="content-wrapper">
     <div class="inner clearfix">
      <section id="main-content">
       <img src="images/merry_christmasa.jpg" alt="chrismas">
      </section>
     </div>
    </div>
    <script>
     $(document).ready( function(){
     $.fn.snow( { minsize: 2, maxsize: 150, newon: 200, flakecolor: '#ffffff' } );
     });
    </script>
    </body>
</html>

jquery.snow.js:

/**
 * jquery.snow - jquery snow effect plugin
 *
 * available under mit licence
 *
 * @version 1 (21. jan 2012)
 * @author ivan lazarevic
 * @requires jquery
 * @see http://workshop.rs
 *
 * @params minsize - min size of snowflake, 10 by default
 * @params maxsize - max size of snowflake, 20 by default
 * @params newon - frequency in ms of appearing of new snowflake, 500 by default
 * @params flakecolor - color of snowflake, #ffffff by default
 * @example $.fn.snow({ maxsize: 200, newon: 1000 });
 */
(function($){
  $.fn.snow = function(options){
      var $flake     = $('<div id="flake" />').css({'position': 'absolute', 'top': '-50px'}).html('❄'),
        documentheight = $(document).height(),
        documentwidth  = $(document).width(),
        defaults    = {
                  minsize   : 10,
                  maxsize   : 20,
                  newon    : 500,
                  flakecolor : "#ffffff"
                },
        options     = $.extend({}, defaults, options);
      //setinterval-setinterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。
      //开始一个周期-开始添加雪花
      var interval    = setinterval( function(){
        var startpositionleft  = math.random() * documentwidth - 100,
          startopacity    = 0.5 + math.random(),
          sizeflake      = options.minsize + math.random() * options.maxsize,
          endpositiontop   = documentheight - 40,
          endpositionleft   = startpositionleft - 100 + math.random() * 200,
          durationfall    = documentheight * 10 + math.random() * 5000;
        $flake
          .clone()
          .appendto('body')
          .css(
            {
              left: startpositionleft,
              opacity: startopacity,
              'font-size': sizeflake,
              color: options.flakecolor
            }
          )
          .animate(//增加雪花动态效果
            {
              top: endpositiontop,
              left: endpositionleft,
              opacity: 0.2
            },
            durationfall,
            'linear',
            function() {
              $(this).remove()
            }
          );
      }, options.newon);
      //结束周期-停止添加雪花
      settimeout(function(){
        window.clearinterval(interval);
      },5000);
  };
})(jquery);

不需要css样式

主要用到:setinterval-setinterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。& animate动画

运行效果:

jQuery实现的下雪动画效果示例【附源码下载】

附:完整实例代码点击此处。

更多关于jquery相关内容感兴趣的读者可查看本站专题:《jquery常用插件及用法总结》、《jquery扩展技巧总结》、《jquery拖拽特效与技巧总结》、《jquery常见经典特效汇总》、《jquery动画与特效用法总结》及《jquery选择器用法总结

希望本文所述对大家jquery程序设计有所帮助。