设为首页收藏本站订阅更新

无忧脚本

 找回密码
 加入无忧

QQ登录

只需一步,快速开始

搜索
查看: 3109|回复: 1

[原创] 九宫格程序

[复制链接]
发表于 2011-12-26 15:17:37 | 显示全部楼层 |阅读模式


  1. <?php
  2. /*
  3. 九宫格计算程序
  4. by 张阳 潜龙勿用 541635879@qq.com
  5. 20111226

  6. */


  7. echo "<span style='font-size:12px'>这里是九宫格</span>";

  8. ?>
  9. <script>
  10. var init_x = 0;
  11. var init_y = 0;

  12. var mouseX;
  13. var mouseY;
  14. </script>
  15. <?

  16. $int_dims = intval($_REQUEST['maxlength'])?intval($_REQUEST['maxlength']):3;

  17. $int_dims = $int_dims%2?$int_dims:$int_dims+1;
  18. $dims_limit = $int_dims*$int_dims;

  19. $y = $int_dims - 1;
  20. $x = ($int_dims - 1)/2;

  21. $init_arr = array();

  22. for($i=1;$i<=$dims_limit;$i++){
  23. $init_arr[$y%$int_dims][$x%$int_dims] = $i;
  24. if($init_arr[($y+1)%$int_dims][($x+1)%$int_dims]){
  25. $y--;
  26. }else{
  27. $x++;$y++;
  28. }
  29. }
  30. echo "<hr />";
  31. echo "<div style='width:".($int_dims*50+1)."px;border-left:1px solid #000;border-top:1px solid #000' onmouseout=\"hide_result()\" >\n";
  32. for($i=0;$i<$int_dims;$i++){
  33. for($j=0;$j<$int_dims;$j++){
  34. echo "<div id='div_".$i."_".$j."' style=\"width:50px;height:50px;float:left;
  35. border-right:1px solid #000;border-bottom:1px solid #000;text-align:center;text-valign:middle;font-size:12px;\"
  36. onmouseover = \"deep_div(".$i.",".$j.")\" onmouseout=\"deep_div2()\" onclick=\"result(".$i.",".$j.")\" onmousemove = \"view_result()\"
  37. >".$init_arr[$i][$j]."</div>\n";
  38. }
  39. }
  40. echo "</div>\n";
  41. ?>
  42. <div id="result" style="border:1px solid #ccc;background:#FFFFFF;position:absolute;left:1px;top:1px;font-size:12px;padding:10px;display:none">
  43. </div>
  44. <form action=? method=get>
  45. 请输入方阵最大项:<input type=text name=maxlength value=<?=$int_dims?>><br />
  46. 输入的最大项应该为奇数,如果最大项为偶数,则自动增加1<br />
  47. <input type=submit value="提交">
  48. </form>
  49. <script>
  50. function deep_div(y,x){

  51. var div_result = document.getElementById('result');

  52. var x_text_arr = new Array();
  53. var y_text_arr = new Array();

  54. var z_text_l_arr = new Array();
  55. var z_text_r_arr = new Array();

  56. var x_nums = 0;
  57. var y_nums = 0;

  58. var z_nums_l = 0;
  59. var z_nums_r = 0;

  60. div_result.innerHTML = "";

  61. if(x!=init_x||y!=init_y){
  62. for(i=0;i<<?=$int_dims?>;i++){
  63. x_text_arr.push(document.getElementById("div_" + y + "_" + i).innerHTML*1);
  64. x_nums = (x_nums + document.getElementById("div_" + y + "_" + i).innerHTML*1);
  65. y_text_arr.push(document.getElementById("div_" + i + "_" + x).innerHTML*1);
  66. y_nums = (y_nums + document.getElementById("div_" + i + "_" + x).innerHTML*1);

  67. z_text_l_arr.push(document.getElementById("div_" + i + "_" + i).innerHTML*1);
  68. z_nums_l = (z_nums_l + document.getElementById("div_" + i + "_" + i).innerHTML*1);
  69. z_text_r_arr.push(document.getElementById("div_" + (<?=$int_dims?> - 1 - i) + "_" + i).innerHTML*1);
  70. z_nums_r = (z_nums_r + document.getElementById("div_" + (<?=$int_dims?> - 1 - i) + "_" + i).innerHTML*1);

  71. document.getElementById("div_" + y + "_" + i).style.background='#D8D8EB';
  72. document.getElementById("div_" + i + "_" + x).style.background='#D8D8EB';
  73. }
  74. init_x = x;
  75. init_y = y;

  76. div_result.innerHTML = '当前行' + (y+1)*1 + ':<br />' + x_text_arr.join(' + ') + " = " + x_nums + '<br />当前列' + (x+1)*1 + ':<br />' + y_text_arr.join(' + ') + " = " + y_nums + '<br /><br />左上右下之和:<br />' + z_text_l_arr.join(' + ') + " = " + z_nums_l + '<br />左下右上之和:<br />' + z_text_r_arr.join(' + ') + " = " + z_nums_r ;
  77. }

  78. }


  79. function deep_div2(){
  80. for(i=0;i<<?=$int_dims?>;i++){
  81. document.getElementById("div_" + init_y + "_" + i).style.background='#FFFFFF';
  82. document.getElementById("div_" + i + "_" + init_x).style.background='#FFFFFF';
  83. }
  84. var div_result = document.getElementById('result');

  85. }

  86. function hide_result(){
  87. var div_result = document.getElementById('result');
  88. div_result.style.display = 'none';



  89. }

  90. function view_result(){
  91. var div_result = document.getElementById('result');

  92. mouseX = event.clientX;
  93. mouseY = event.clientY;

  94. div_result.style.left = mouseX+20;
  95. div_result.style.top = mouseY+20;
  96. div_result.style.display = '';

  97. }




  98. </script>

复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?加入无忧

x
发表于 2012-5-2 19:30:41 | 显示全部楼层
好东西啊
您需要登录后才可以回帖 登录 | 加入无忧

本版积分规则

小黑屋|手机版|Archiver|无忧脚本 ( 苏ICP备05080427号 )|值班电话:027-62300445   鄂公网安备 42011102000433号

GMT+8, 2017-11-20 01:41 , Processed in 0.090411 second(s), 8 queries , Gzip On, Memcache On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表