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 相关文章推荐
Javascript改变CSS样式(局部和全局)
Dec 18 Javascript
js对象继承之原型链继承实例
Jan 10 Javascript
用JavaScript实现对话框的教程
Jun 04 Javascript
JS 拦截全局ajax请求实例解析
Nov 29 Javascript
AngularJS的ng-click传参的方法
Jun 19 Javascript
使用JavaScript实现表格编辑器(实例讲解)
Aug 02 Javascript
JS传播事件、取消事件默认行为、阻止事件传播详解
Aug 14 Javascript
使用RxJS更优雅地进行定时请求详析
Jun 02 Javascript
js实现一个简易计算器
Mar 30 Javascript
在vue中使用image-webpack-loader实例
Nov 12 Javascript
Nest.js环境变量配置与序列化详解
Feb 21 Javascript
vue实现可以快进后退的跑马灯组件
Apr 08 Vue.js
使用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
PHP用mysql数据库存储session的代码
2010/03/05 PHP
php使用parse_url和parse_str解析URL
2015/02/22 PHP
jquery+php实现导出datatables插件数据到excel的方法
2015/07/06 PHP
仿jQuery的siblings效果的js代码
2011/08/09 Javascript
javascript 基础篇1 什么是js 建立第一个js程序
2012/03/14 Javascript
javascript 事件处理、鼠标拖动效果实现方法详解
2012/05/11 Javascript
使用js检测浏览器的实现代码
2013/05/14 Javascript
jQuery获取和设置表单元素的方法
2014/02/14 Javascript
Javascript判断文件是否存在(客户端/服务器端)
2014/09/16 Javascript
浅谈js中调用函数时加不加括号的问题
2016/07/28 Javascript
微信小程序 中wx.chooseAddress(OBJECT)实例详解
2017/03/31 Javascript
利用Console来Debug的10个高级技巧汇总
2018/03/26 Javascript
Vue中控制v-for循环次数的实现方法
2018/09/26 Javascript
策略模式实现 Vue 动态表单验证的方法
2019/09/16 Javascript
JS实现简易留言板特效
2019/12/23 Javascript
vscode 插件开发 + vue的操作方法
2020/06/05 Javascript
JS实现audio音频剪裁剪切复制播放与上传(步骤详解)
2020/07/28 Javascript
编写v-for循环的技巧汇总
2020/12/01 Javascript
Python实现多线程抓取网页功能实例详解
2017/06/08 Python
Python使用pymongo模块操作MongoDB的方法示例
2018/07/20 Python
Python图像滤波处理操作示例【基于ImageFilter类】
2019/01/03 Python
python实现将多个文件分配到多个文件夹的方法
2019/01/07 Python
Python彻底删除文件夹及其子文件方式
2019/12/23 Python
amazeui模态框弹出后立马消失并刷新页面
2020/08/19 HTML / CSS
村官学习十八大感想
2014/01/15 职场文书
学习雷锋精神心得体会范文
2014/03/12 职场文书
弘扬焦裕禄精神践行三严三实心得体会
2014/10/13 职场文书
中学生旷课检讨书500字
2014/10/29 职场文书
2014年社区民政工作总结
2014/12/02 职场文书
初三英语教学计划
2015/01/23 职场文书
副校长2015年教育教学工作总结
2015/07/27 职场文书
高中生社会实践心得体会
2016/01/14 职场文书
2019通用版劳动合同范本!
2019/07/11 职场文书
python实现批量移动文件
2021/04/05 Python
Python各协议下socket黏包问题原理
2022/04/12 Python
浅谈为什么我的 z-index 又不生效了
2022/07/15 HTML / CSS