含羞默默一張一合效果
含羞默默一張一合效果---田
首先展示“田”字效果
實(shí)現(xiàn)思想主要分為幾部分
隨機(jī)生成顏色值
- var getRandomColor = function(){
- return '#' +
- (function(color){
- return (color += '0123456789abcdef'[Math.floor(Math.random()*16)])
- && (color.length == 6) ? color : arguments.callee(color);
- })('');
- }
創(chuàng)建span標(biāo)簽,插入div中。
creSpan函數(shù),n指當(dāng)前個(gè)數(shù),mpid指父容器div,mleft指當(dāng)前span的left的值,mtop指當(dāng)前span的top值
- function creSpan(n,mpId,mleft,mtop){
- var mSpan = document.createElement("span");
- var pId = mpId[0];
- pId.appendChild(mSpan);
- with(mSpan.style){
- left = mleft+"px";
- top = mtop+"px";
- background = getRandomColor();
- }
- }
生成“田”字
創(chuàng)建一個(gè)二維數(shù)組保存每個(gè)creSpan的對(duì)象。myleft=100,mtop=50 默認(rèn)初始值距左距頂?shù)木嚯x。
畫“田”字,使用雙重循環(huán)生成。
- var myleft = 100;
- var mytop = 50;
- var arr = new Array();
- var test = $("#test");
- for(var j=0;j<23;j++){
- arr[j] = new Array();
- if(j<3){
- for(var i=0;i<19;i++){
- myleft+=32;
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- }
- else if(j>2&&j<10){
- for(var i=0;i<19;i++){
- myleft+=32;
- if(i<3){
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- else if(i>7&&i<11){
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- else if(i>15){
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- }
- }
- else if(j>9&&j<13){
- for(var i=0;i<19;i++){
- myleft+=32;
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- }
- else if(j>12&&j<20){
- for(var i=0;i<19;i++){
- myleft+=32;
- if(i<3){
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- else if(i>7&&i<11){
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- else if(i>15){
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- }
- }
- else{
- for(var i=0;i<19;i++){
- myleft+=32;
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- }
- mytop+=32;
- myleft=100;
- }
當(dāng)鼠標(biāo)移動(dòng)到每個(gè)span上時(shí)尖尖縮小,然后慢慢張開。
主要采用jquery中的animate函數(shù)??刂苭idth,height,left,top的值。
- $.each($("#test span"),function(k,v){
- $(this).mouseover(function(){
- $(this).animate({
- width:"10px",
- height:"10px",
- left:"+="+parseInt(30-20)/2+"px",
- top:"+="+parseInt(30-20)/2+"px"
- },3000,function(){
- $(this).animate({
- width:"30px",
- height:"30px",
- left:"-="+parseInt(30-20)/2+"px",
- top:"-="+parseInt(30-20)/2+"px"
- },1000);
- });
- });
- });
#p#
完整代碼:
- <!DOCTYPE html>
- <html>
- <head>
- <title>含羞默默一張一合效果---田</title>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <script type="text/javascript" src="http://files.cnblogs.com/kuikui/jquery.js"></script>
- <style type="text/css">
- *{margin:0px;padding:0px;}
- #test{width:800px; height: 800px; margin: 30px auto 0px; overflow: hidden; position: relative; background-color: #F1F1F1;}
- #test span{display: block; position: absolute; width: 30px; height: 30px; }
- </style>
- </head>
- <body>
- <div id="test"></div>
- <script type="text/javascript">
- var getRandomColor = function(){
- return '#' +
- (function(color){
- return (color += '0123456789abcdef'[Math.floor(Math.random()*16)])
- && (color.length == 6) ? color : arguments.callee(color);
- })('');
- }
- function creSpan(n,mpId,mleft,mtop){
- var mSpan = document.createElement("span");
- var pId = mpId[0];
- pId.appendChild(mSpan);
- with(mSpan.style){
- left = mleft+"px";
- top = mtop+"px";
- background = getRandomColor();
- }
- }
- </script>
- <script type="text/javascript">
- $(function(){
- var myleft = 100;
- var mytop = 50;
- var arr = new Array();
- var test = $("#test");
- for(var j=0;j<23;j++){
- arr[j] = new Array();
- if(j<3){
- for(var i=0;i<19;i++){
- myleft+=32;
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- }
- else if(j>2&&j<10){
- for(var i=0;i<19;i++){
- myleft+=32;
- if(i<3){
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- else if(i>7&&i<11){
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- else if(i>15){
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- }
- }
- else if(j>9&&j<13){
- for(var i=0;i<19;i++){
- myleft+=32;
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- }
- else if(j>12&&j<20){
- for(var i=0;i<19;i++){
- myleft+=32;
- if(i<3){
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- else if(i>7&&i<11){
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- else if(i>15){
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- }
- }
- else{
- for(var i=0;i<19;i++){
- myleft+=32;
- arr[j][i] = new creSpan((j+1)*(i+1),test,myleft,mytop);
- }
- }
- mytop+=32;
- myleft=100;
- }
- $.each($("#test span"),function(k,v){
- $(this).mouseover(function(){
- $(this).animate({
- width:"10px",
- height:"10px",
- left:"+="+parseInt(30-20)/2+"px",
- top:"+="+parseInt(30-20)/2+"px"
- },3000,function(){
- $(this).animate({
- width:"30px",
- height:"30px",
- left:"-="+parseInt(30-20)/2+"px",
- top:"-="+parseInt(30-20)/2+"px"
- },1000);
- });
- });
- });
- });
- </script>
- </body>
- </html>
原文鏈接:http://www.cnblogs.com/kuikui/archive/2012/07/19/2598491.html
【編輯推薦】