jQuery+css实现炫目的动态块漂移效果


Posted in Javascript onJanuary 28, 2016

本文实例讲述了jQuery+css实现的动态块漂移效果.分享给大家供大家参考,具体如下:

运行效果截图如下:

jQuery+css实现炫目的动态块漂移效果

具体代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>
  <script src="jquery-1.7.1.min.js" type="text/javascript"></script>
  <script type="text/javascript" >
    function createColor() {
      var color = [];
      for (var i = 0; i < 3; i++) {
        color.push(Math.round(Math.random() * 256));
      }
      return "rgb(" + color.join(",") + ")"
    }
    function createRect(left, top, index) {
      var width = Math.round(Math.random() * 150) + 10;
      var height = Math.round(Math.random() * 150) + 10;
      left = left > width ? left - width : left;
      top = top > height ? top - height : top;
      var html = [];
      html.push('<div id="rect_'+index+'" class="rect shadow radius" style="background:');
      html.push(createColor());
      html.push(';left:');
      html.push(left);
      html.push('px;top:');
      html.push(top);
      html.push('px;width:');
      html.push(width);
      html.push('px; height:');
      html.push(height);
      html.push('px;"></div>');
      return html.join("");
    }
    function initRect() {
      var body = $("#body");
      var width = body.width();
      var height = body.height();
      var index = new Date().getTime();
      body.append(createRect(Math.round(Math.random() * width), Math.round(Math.random() * height), index));
      setAnimate(index, height);
    }
    function setAnimate(index, height) {
      var rect = $("#rect_" + index);
      var top = parseInt(rect.position().top);
      var selfHeight = rect.height();
      if (top > height - selfHeight) {
        rect.remove();
        initRect();
      } else {
        var filter = top / height;
        rect.css({ "top": (top + 5) + "px", "opacity": filter });
        setTimeout(function () {
          setAnimate(index, height);
        }, 33);
      }
    }
    function initAllRect() {
      for (var i = 0; i < 20; i++) {
        initRect();
      }
    }
    $(document).ready(function () {
      initAllRect();
    });
  </script>
  <style type="text/css" >
  .rect {
        background:#DDDDDD;
        width:100px;
        height:100px;
        position:absolute;
  }
  .radius 
  {
    border-radius:8px;
      -moz-border-radius:8px;
      -webkit-border-radius:8px;
  }
  .shadow 
  {
    -moz-box-shadow:-5px -5px 5px #999 inset;
      -webkit-box-shadow:-5px -5px 5px #999 inset;
      box-shadow:-5px -5px 5px #999 inset; 
  }
  #body { height:700px; width:100%; background:black; margin:0; }
  </style>
</head>
<body>
<div id="body" class="shadow radius">
</div>
</body>
</html>

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

Javascript 相关文章推荐
jfreechart插件将数据展示成饼状图、柱状图和折线图
Apr 13 Javascript
jQuery选择器用法实例详解
Dec 17 Javascript
AngularJS中watch监听用法分析
Nov 04 Javascript
jQuery实现页面倒计时并刷新效果
Mar 13 Javascript
js控制文本框禁止输入特殊字符详解
Apr 07 Javascript
jQuery+Cookie实现切换皮肤功能【附源码下载】
Mar 25 jQuery
Angular2进阶之如何避免Dom误区
Apr 02 Javascript
微信小程序实现留言板功能
Nov 02 Javascript
浅谈Vue 性能优化之深挖数组
Dec 11 Javascript
8个有意思的JavaScript面试题
Jul 30 Javascript
微信小程序移动拖拽视图-movable-view实例详解
Aug 17 Javascript
node 标准输入流和输出流代码实例
Sep 19 Javascript
使用node+vue.js实现SPA应用
Jan 28 #Javascript
jQuery+css实现的tab切换标签(兼容各浏览器)
Jan 28 #Javascript
javascript实现随机显示星星特效
Jan 28 #Javascript
基于javascript实现全国省市二级联动下拉选择菜单
Jan 28 #Javascript
JS实现动态生成表格并提交表格数据向后端
Nov 25 #Javascript
jQuery+css实现的时钟效果(兼容各浏览器)
Jan 27 #Javascript
jQuery实现的分子运动小球碰撞效果
Jan 27 #Javascript
You might like
phpmyadmin中禁止外网使用的方法
2014/11/04 PHP
Yii2简单实现给表单添加验证码的方法
2016/07/18 PHP
PHP中phar包的使用教程
2017/06/14 PHP
PHP实现SMTP邮件的发送实例
2018/09/27 PHP
在JavaScript中使用inline函数的问题
2007/03/08 Javascript
JavaScript插入动态样式实现代码
2012/02/22 Javascript
jQuery之折叠面板的深入解析
2013/06/19 Javascript
javascript继承机制实例详解
2014/11/20 Javascript
JSON字符串转换JSONObject和JSONArray的方法
2016/06/03 Javascript
JavaScript基于自定义函数判断变量类型的实现方法
2016/11/23 Javascript
解析预加载显示图片艺术
2016/12/05 Javascript
vue用Object.defineProperty手写一个简单的双向绑定的示例
2018/07/09 Javascript
jQuery插件实现的日历功能示例【附源码下载】
2018/09/07 jQuery
详解angularjs跨页面传参遇到的一些问题
2018/11/01 Javascript
vue elementUI使用tabs与导航栏联动
2019/06/21 Javascript
javascript 易错知识点实例小结
2020/04/25 Javascript
ES11屡试不爽的新特性,你用上了几个
2020/10/21 Javascript
[49:05]OG vs Newbee 2019DOTA2国际邀请赛淘汰赛 胜者组 BO3 第二场 8.21.mp4
2020/07/19 DOTA
python在Windows8下获取本机ip地址的方法
2015/03/14 Python
python实现指定字符串补全空格的方法
2015/04/30 Python
Python中矩阵库Numpy基本操作详解
2017/11/21 Python
Django urls.py重构及参数传递详解
2019/07/23 Python
python将三维数组展开成二维数组的实现
2019/11/30 Python
Django restframework 框架认证、权限、限流用法示例
2019/12/21 Python
Django操作session 的方法
2020/03/09 Python
Python绘制组合图的示例
2020/09/18 Python
VICHY薇姿俄罗斯官方网上商店:法国护肤品牌,火山温泉水
2019/11/22 全球购物
伦敦新晋轻奢耳饰潮牌:Tada & Toy
2020/05/25 全球购物
为什么如下的代码int a=100,b=100;long int c=a * b;不能工作
2013/11/29 面试题
简述安装Slackware Linux系统的过程
2012/05/08 面试题
体育教师自荐信范文
2013/12/16 职场文书
外企办公室竞聘演讲稿
2013/12/29 职场文书
中国梦读书活动总结
2014/07/10 职场文书
活动总结结尾怎么写
2014/08/30 职场文书
司法局2014法制宣传日活动总结
2014/11/01 职场文书
常用的文件对应的MIME类型汇总
2022/04/26 HTML / CSS