小球圆周运动

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. .box{
  8. position: relative;
  9. width: 400px;
  10. height: 400px;
  11. border: 1px solid red;
  12. margin: 200px auto 0px;
  13. border-radius: 50%;
  14. }
  15. .smallbox{
  16. position: absolute;
  17. width: 10px;
  18. height: 10px;
  19. border-radius:50%;
  20. background-color: black;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div class="box">
  26. <span class="smallbox"></span>
  27. </div>
  28. <script>
  29. var r=200;
  30. var d=document.getElementsByClassName("smallbox");
  31. var deg=0;
  32. var x,y;
  33. function fn(){
  34. y=Math.sin(Math.PI/180*deg)*r+r-5;
  35. x=Math.cos(Math.PI/180*deg)*r+r-5;
  36. d[0].style.cssText="top:"+y+"px;left:"+x+"px;";
  37. console.log(x+"====="+y);
  38. console.log(deg);
  39. ++deg;
  40. }
  41. setInterval(fn,1);
  42. </script>
  43. </body>
  44. </html>

时间运动框架

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>时间运动框架</title>
  6. <style>
  7. #box{
  8. width: 300px;
  9. height: 200px;
  10. background-color: pink;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="box"></div>
  16. </body>
  17. <script>
  18. var box=document.getElementById("box");//获取元素
  19. animation(box,{
  20. data:{
  21. width:1100,//属性:属性值
  22. height:200,
  23. opacity:0.5
  24. },
  25. options:{
  26. speed:"easeInOut",//速度类型
  27. type:"Sine" //运动类型
  28. }
  29. },1000);
  30. function animation(ele,data,time,callback){
  31. //IE兼容
  32. window.requestAnimationFrame=window.requestAnimationFrame||function(method){
  33. setTimeout(method,1000/60);
  34. };
  35. //开始时间
  36. var startTime=new Date();
  37. //获取css样式以及IE兼容
  38. var eCss=ele.currentStyle||getComputedStyle(ele);
  39. //要变化的值
  40. var diffData={};
  41. //原本属性的值
  42. var nowData={};
  43. //运动方式类型
  44. var oType=data["options"]["type"];
  45. //运动方式速度
  46. var oSpeed=data["options"]["speed"];
  47. //Tween.js
  48. var Tween = {
  49. //直线运动
  50. Linear:{
  51. easeIn:function(t, b, c, d) { return c*t/d + b; }
  52. },
  53. //二次曲线
  54. Quad: {
  55. easeIn: function(t, b, c, d) {
  56. return c * (t /= d) * t + b;
  57. },
  58. easeOut: function(t, b, c, d) {
  59. return -c *(t /= d)*(t-2) + b;
  60. },
  61. easeInOut: function(t, b, c, d) {
  62. if ((t /= d / 2) < 1) return c / 2 * t * t + b;
  63. return -c / 2 * ((--t) * (t-2) - 1) + b;
  64. }
  65. },
  66. //三次曲线
  67. Cubic: {
  68. easeIn: function(t, b, c, d) {
  69. return c * (t /= d) * t * t + b;
  70. },
  71. easeOut: function(t, b, c, d) {
  72. return c * ((t = t/d - 1) * t * t + 1) + b;
  73. },
  74. easeInOut: function(t, b, c, d) {
  75. if ((t /= d / 2) < 1) return c / 2 * t * t*t + b;
  76. return c / 2*((t -= 2) * t * t + 2) + b;
  77. }
  78. },
  79. //四次曲线
  80. Quart: {
  81. easeIn: function(t, b, c, d) {
  82. return c * (t /= d) * t * t*t + b;
  83. },
  84. easeOut: function(t, b, c, d) {
  85. return -c * ((t = t/d - 1) * t * t*t - 1) + b;
  86. },
  87. easeInOut: function(t, b, c, d) {
  88. if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;
  89. return -c / 2 * ((t -= 2) * t * t*t - 2) + b;
  90. }
  91. },
  92. //五
  93. Quint: {
  94. easeIn: function(t, b, c, d) {
  95. return c * (t /= d) * t * t * t * t + b;
  96. },
  97. easeOut: function(t, b, c, d) {
  98. return c * ((t = t/d - 1) * t * t * t * t + 1) + b;
  99. },
  100. easeInOut: function(t, b, c, d) {
  101. if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
  102. return c / 2*((t -= 2) * t * t * t * t + 2) + b;
  103. }
  104. },
  105. Sine: {
  106. easeIn: function(t, b, c, d) {
  107. return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
  108. },
  109. easeOut: function(t, b, c, d) {
  110. return c * Math.sin(t/d * (Math.PI/2)) + b;
  111. },
  112. easeInOut: function(t, b, c, d) {
  113. return -c / 2 * (Math.cos(Math.PI * t/d) - 1) + b;
  114. }
  115. },
  116. Expo: {
  117. easeIn: function(t, b, c, d) {
  118. return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
  119. },
  120. easeOut: function(t, b, c, d) {
  121. return (t==d) ? b + c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  122. },
  123. easeInOut: function(t, b, c, d) {
  124. if (t==0) return b;
  125. if (t==d) return b+c;
  126. if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
  127. return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
  128. }
  129. },
  130. Circ: {
  131. easeIn: function(t, b, c, d) {
  132. return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
  133. },
  134. easeOut: function(t, b, c, d) {
  135. return c * Math.sqrt(1 - (t = t/d - 1) * t) + b;
  136. },
  137. easeInOut: function(t, b, c, d) {
  138. if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
  139. return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
  140. }
  141. },
  142. Bounce: {
  143. easeIn: function(t, b, c, d) {
  144. return c - Tween.Bounce.easeOut(d-t, 0, c, d) + b;
  145. },
  146. easeOut: function(t, b, c, d) {
  147. if ((t /= d) < (1 / 2.75)) {
  148. return c * (7.5625 * t * t) + b;
  149. } else if (t < (2 / 2.75)) {
  150. return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
  151. } else if (t < (2.5 / 2.75)) {
  152. return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
  153. } else {
  154. return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
  155. }
  156. },
  157. easeInOut: function(t, b, c, d) {
  158. if (t < d / 2) {
  159. return Tween.Bounce.easeIn(t * 2, 0, c, d) * .5 + b;
  160. } else {
  161. return Tween.Bounce.easeOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b;
  162. }
  163. }
  164. }
  165. };
  166. //算出每个属性的差值,算出每个属性现在的值分别给到两个对象
  167. for(var key in data["data"]){
  168. if(eCss[key]!==data["data"][key]){
  169. //为了兼容IE的透明属性
  170. if(key!=="opacity"){
  171. nowData[key]=parseFloat(eCss[key]);
  172. }else{
  173. nowData[key]=parseFloat(eCss[key])||1;
  174. }
  175. diffData[key]=data["data"][key]-nowData[key];
  176. }
  177. }
  178. //判断对于速度类型的传值问题
  179. if(!data["options"]){
  180. oType="Linear";
  181. oSpeed="easeIn";
  182. }else{
  183. if(!data["options"]["speed"]){
  184. oSpeed="easeIn";
  185. }
  186. if(!data["options"]["speed"]){
  187. oType="Linear"
  188. }
  189. }
  190. //请求动画帧进行变量的
  191. function calcSeconds(){
  192. var interTime=new Date()-startTime;
  193. interTime=Math.min(time,interTime);
  194. for(var key in diffData){
  195. var val=Tween[oType][oSpeed](interTime,nowData[key],diffData[key],time);
  196. if(key!=="opacity"){
  197. ele.style[key]=val+"px";
  198. }else{
  199. ele.style[key]=val;
  200. ele.style["filter"]="alpha(opacity="+(val*100)+")";
  201. }
  202. }
  203. if(interTime===time){
  204. callback&&callback();
  205. }else{
  206. requestAnimationFrame(calcSeconds);
  207. }
  208. }
  209. requestAnimationFrame(calcSeconds);
  210. }
  211. </script>
  212. </html>

获取当前时间

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. #box{
  8. width: 400px;
  9. height: 400px;
  10. border: 1px solid red;
  11. margin: 200px auto 0px;
  12. background-color: green;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="box">
  18. </div>
  19. <script>
  20. function getMyTime(){
  21. var t=new Date();
  22. var weeks=["日","一","二","三","四","五","六"]
  23. var year=t.getFullYear();
  24. var month=t.getMonth()>8?t.getMonth()+1:"0"+(t.getMonth()+1);
  25. var day=t.getDate()>9?t.getDate():"0"+t.getDate();
  26. var week=weeks[t.getDay()];
  27. var hours=t.getHours()>9?t.getHours():"0"+t.getHours();
  28. var minutes=t.getMinutes()>9?t.getMinutes():"0"+t.getMinutes();
  29. var seconds=t.getSeconds()>9?t.getSeconds():"0"+t.getSeconds();
  30. var time=year+"年"+month+"月"+day+"日 星期"+week+" "+hours+":"+minutes+":"+seconds;
  31. return time;
  32. }
  33. box.innerHTML=getMyTime();
  34. setInterval(function(){
  35. box.innerHTML=getMyTime();
  36. },1000);
  37. </script>
  38. </body>
  39. </html>

时间图标

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. #box{
  8. width: 900px;
  9. height: 700px;
  10. border: 1px solid red;
  11. margin: 200px auto 0px;
  12. background-color: green;
  13. }
  14. span{
  15. font-size: 50px;
  16. position: relative;
  17. top: -12px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="box">
  23. <img src="images/0.png" class="photo" alt="">
  24. <img src="images/1.png" class="photo" alt="">
  25. <img src="images/2.png" class="photo" alt="">
  26. <img src="images/3.png" class="photo" alt=""><span>年</span>
  27. <img src="images/4.png" class="photo" alt="">
  28. <img src="images/5.png" class="photo" alt=""><span>月</span>
  29. <img src="images/6.png" class="photo" alt="">
  30. <img src="images/7.png" class="photo" alt=""><span>日</span>
  31. <img src="images/8.png" class="photo" alt="">
  32. <img src="images/9.png" class="photo" alt="">
  33. <img src="images/d.png" class="" alt="">
  34. <img src="images/9.png" class="photo" alt="">
  35. <img src="images/9.png" class="photo" alt="">
  36. <img src="images/d.png" class="" alt="">
  37. <img src="images/9.png" class="photo" alt="">
  38. <img src="images/9.png" class="photo" alt="">
  39. </div>
  40. <script>
  41. function getMyNumber(num,index){
  42. if(isNaN(num)){
  43. return null;
  44. }
  45. var len=new String(num).length;
  46. if(index===1){
  47. return Math.floor(num/Math.pow(10,len-index));
  48. }else if(index===len){
  49. return Math.floor(num%10);
  50. }else{
  51. return Math.floor(num%Math.pow(10,len-index+1)/Math.pow(10,len-index));
  52. }
  53. }
  54. function photoTime(){
  55. var t=new Date();
  56. var year=t.getFullYear();
  57. var t0=getMyNumber(year,1);
  58. var t1=getMyNumber(year,2);
  59. var t2=getMyNumber(year,3);
  60. var t3=getMyNumber(year,4);
  61. var month=t.getMonth()>8?t.getMonth()+1:"0"+(t.getMonth()+1);
  62. var t4=getMyNumber(month,1);
  63. var t5=getMyNumber(month,2);
  64. var day=t.getDate()>9?t.getDate():"0"+t.getDate();
  65. var t6=getMyNumber(day,1);
  66. var t7=getMyNumber(day,2);
  67. var hours=t.getHours()>9?t.getHours():"0"+t.getHours();
  68. var t8=getMyNumber(hours,1);
  69. var t9=getMyNumber(hours,2);
  70. var minutes=t.getMinutes()>9?t.getMinutes():"0"+t.getMinutes();
  71. var t10=getMyNumber(minutes,1);
  72. var t11=getMyNumber(minutes,2);
  73. var seconds=t.getSeconds()>9?t.getSeconds():"0"+t.getSeconds();
  74. var t12=getMyNumber(seconds,1);
  75. var t13=getMyNumber(seconds,2);
  76. for(let j=0;j<14;j++){
  77. var b="t"+j;
  78. i[j].setAttribute("src","images/"+eval(b)+".png");
  79. }
  80. }
  81. var i=document.getElementsByClassName("photo");
  82. photoTime();
  83. setInterval(photoTime,1000);
  84. </script>
  85. </body>
  86. </html>

图片飞出

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. </head>
  7. <style>
  8. #box{
  9. width: 400px;
  10. height: 400px;
  11. background-color: transparent;
  12. position: relative;
  13. }
  14. .smallbox{
  15. width: 40px;
  16. height: 40px;
  17. background: transparent;
  18. position: absolute;
  19. }
  20. </style>
  21. <body>
  22. <div id="box">
  23. <div class="smallbox"></div>
  24. <div class="smallbox"></div>
  25. <div class="smallbox"></div>
  26. <div class="smallbox"></div>
  27. <div class="smallbox"></div>
  28. <div class="smallbox"></div>
  29. <div class="smallbox"></div>
  30. <div class="smallbox"></div>
  31. <div class="smallbox"></div>
  32. <div class="smallbox"></div>
  33. <div class="smallbox"></div>
  34. <div class="smallbox"></div>
  35. <div class="smallbox"></div>
  36. <div class="smallbox"></div>
  37. <div class="smallbox"></div>
  38. <div class="smallbox"></div>
  39. <div class="smallbox"></div>
  40. <div class="smallbox"></div>
  41. <div class="smallbox"></div>
  42. <div class="smallbox"></div>
  43. <div class="smallbox"></div>
  44. <div class="smallbox"></div>
  45. <div class="smallbox"></div>
  46. <div class="smallbox"></div>
  47. <div class="smallbox"></div>
  48. <div class="smallbox"></div>
  49. <div class="smallbox"></div>
  50. <div class="smallbox"></div>
  51. <div class="smallbox"></div>
  52. <div class="smallbox"></div>
  53. <div class="smallbox"></div>
  54. <div class="smallbox"></div>
  55. <div class="smallbox"></div>
  56. <div class="smallbox"></div>
  57. <div class="smallbox"></div>
  58. <div class="smallbox"></div>
  59. <div class="smallbox"></div>
  60. <div class="smallbox"></div>
  61. <div class="smallbox"></div>
  62. <div class="smallbox"></div>
  63. <div class="smallbox"></div>
  64. <div class="smallbox"></div>
  65. <div class="smallbox"></div>
  66. <div class="smallbox"></div>
  67. <div class="smallbox"></div>
  68. <div class="smallbox"></div>
  69. <div class="smallbox"></div>
  70. <div class="smallbox"></div>
  71. <div class="smallbox"></div>
  72. <div class="smallbox"></div>
  73. <div class="smallbox"></div>
  74. <div class="smallbox"></div>
  75. <div class="smallbox"></div>
  76. <div class="smallbox"></div>
  77. <div class="smallbox"></div>
  78. <div class="smallbox"></div>
  79. <div class="smallbox"></div>
  80. <div class="smallbox"></div>
  81. <div class="smallbox"></div>
  82. <div class="smallbox"></div>
  83. <div class="smallbox"></div>
  84. <div class="smallbox"></div>
  85. <div class="smallbox"></div>
  86. <div class="smallbox"></div>
  87. <div class="smallbox"></div>
  88. <div class="smallbox"></div>
  89. <div class="smallbox"></div>
  90. <div class="smallbox"></div>
  91. <div class="smallbox"></div>
  92. <div class="smallbox"></div>
  93. <div class="smallbox"></div>
  94. <div class="smallbox"></div>
  95. <div class="smallbox"></div>
  96. <div class="smallbox"></div>
  97. <div class="smallbox"></div>
  98. <div class="smallbox"></div>
  99. <div class="smallbox"></div>
  100. <div class="smallbox"></div>
  101. <div class="smallbox"></div>
  102. <div class="smallbox"></div>
  103. <div class="smallbox"></div>
  104. <div class="smallbox"></div>
  105. <div class="smallbox"></div>
  106. <div class="smallbox"></div>
  107. <div class="smallbox"></div>
  108. <div class="smallbox"></div>
  109. <div class="smallbox"></div>
  110. <div class="smallbox"></div>
  111. <div class="smallbox"></div>
  112. <div class="smallbox"></div>
  113. <div class="smallbox"></div>
  114. <div class="smallbox"></div>
  115. <div class="smallbox"></div>
  116. <div class="smallbox"></div>
  117. <div class="smallbox"></div>
  118. <div class="smallbox"></div>
  119. <div class="smallbox"></div>
  120. <div class="smallbox"></div>
  121. <div class="smallbox"></div>
  122. <div class="smallbox"></div>
  123. </div>
  124. </body>
  125. <script src="js/animation.js"></script>
  126. <script>
  127. var box=document.getElementById("box");
  128. var smallBox=document.querySelectorAll(".smallbox");
  129. function getMyNumber(num,index){
  130. if(isNaN(num)){
  131. return null;
  132. }
  133. var len=new String(num).length;
  134. if(index===1){
  135. return Math.floor(num/Math.pow(10,len-index));
  136. }else if(index===len){
  137. return Math.floor(num%10);
  138. }else{
  139. return Math.floor(num%Math.pow(10,len-index+1)/Math.pow(10,len-index));
  140. }
  141. }
  142. for(let i=0;i<100;i++){
  143. if(i<10){
  144. var ii="0"+i;
  145. smallBox[i].index=ii;
  146. }else{
  147. smallBox[i].index=i;
  148. }
  149. }
  150. for(let i=0;i<100;i++){
  151. var random=Math.floor(Math.random()*100);
  152. var medium=smallBox[i].index;
  153. smallBox[i].index=smallBox[random].index;
  154. smallBox[random].index=medium;
  155. }
  156. var j=0;
  157. var time=setInterval(function(){
  158. if(j>99){
  159. clearTimeout(time);
  160. }
  161. let y=getMyNumber(smallBox[j].index,1)*40;
  162. let x=getMyNumber(smallBox[j].index,2)*40;
  163. console.log("background: url('images/cd.JPG') no-repeat "+(-x)+"px "+(-y)+"px;");
  164. smallBox[j].style.cssText="background: url('images/cd.JPG') no-repeat "+(-x)+"px "+(-y)+"px;";
  165. animation(smallBox[j],{
  166. data:{
  167. top:y,
  168. left:x
  169. }
  170. },500);
  171. j++;
  172. },500);
  173. </script>
  174. </html>

无缝轮播

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. </head>
  7. <style>
  8. *{
  9. list-style: none;
  10. margin: 0px;
  11. padding: 0px;
  12. }
  13. .big-box{
  14. border: 1px solid blue;
  15. margin: 200px auto;
  16. width: 700px;
  17. overflow: hidden;
  18. position: relative;
  19. }
  20. .box{
  21. width: 8888px;
  22. position: relative;
  23. overflow: hidden;
  24. }
  25. .box>li{
  26. float: left;
  27. }
  28. .box li>img{
  29. vertical-align: top;
  30. }
  31. .big-box .left{
  32. position: absolute;
  33. width: 40px;
  34. height: 40px;
  35. top: 50%;
  36. transform: translate(0%, -50%);
  37. left: -20px;
  38. background: deeppink;
  39. opacity: 0.8;
  40. border-radius: 50%;
  41. cursor: pointer;
  42. }
  43. .big-box .right{
  44. position: absolute;
  45. width: 40px;
  46. height: 40px;
  47. top: 50%;
  48. transform: translate(0%, -50%);
  49. right: -20px;
  50. background: deeppink;
  51. opacity: 0.8;
  52. border-radius: 50%;
  53. cursor: pointer;
  54. }
  55. .circle{
  56. overflow: hidden;
  57. width: 150px;
  58. background-color: black;
  59. opacity: 0.3;
  60. position: absolute;
  61. bottom: 20px;
  62. left: 50%;
  63. transform: translate(-50%,0%);
  64. border-radius: 20px;
  65. }
  66. .circle li{
  67. float: left;
  68. width: 20px;
  69. height: 20px;
  70. border-radius: 50%;
  71. margin:2px 5px;
  72. background: orange;
  73. opacity: 0.3;
  74. }
  75. </style>
  76. <body>
  77. <div class="big-box">
  78. <ul class="box">
  79. <li><img src="images/cd (1).JPG" alt=""></li>
  80. <li><img src="images/cd (2).JPG" alt=""></li>
  81. <li><img src="images/cd (3).JPG" alt=""></li>
  82. <li><img src="images/cd (4).JPG" alt=""></li>
  83. <li><img src="images/cd (5).JPG" alt=""></li>
  84. <li><img src="images/cd (1).JPG" alt=""></li>
  85. </ul>
  86. <span class="left"></span>
  87. <span class="right"></span>
  88. <ul class="circle">
  89. <li></li>
  90. <li></li>
  91. <li></li>
  92. <li></li>
  93. <li></li>
  94. </ul>
  95. </div>
  96. <script src="js/animation.js"></script>
  97. <script>
  98. var left=document.querySelector(".left");
  99. var right=document.querySelector(".right");
  100. var box=document.querySelector(".box");
  101. var circleLi=document.querySelectorAll(".circle li");
  102. var len=circleLi.length;
  103. var num=0;
  104. var date=new Date();
  105. circleLi[num].style.opacity="1";
  106. function clearOpacity(){
  107. for(let i=0;i<len;i++){
  108. circleLi[i].style.opacity="0.3";
  109. }
  110. if(num===0||num===5){
  111. circleLi[0].style.opacity="1";
  112. }else{
  113. circleLi[num].style.opacity="1";
  114. }
  115. }
  116. right.onclick=function(){
  117. num++;
  118. if(num>5)num=1;
  119. if((new Date()-date)>500){
  120. clearOpacity();
  121. console.log(num);
  122. animation(box,{
  123. data:{
  124. left:-700*num
  125. }
  126. },500,function(){
  127. if(num===5)
  128. box.style.left="0px";
  129. });
  130. }
  131. date=new Date();
  132. }
  133. left.onclick=function(){
  134. num--;
  135. if(num<0)num=4;
  136. if((new Date-date)>500){
  137. if((num+1)===5)box.style.left="-3500px"
  138. clearOpacity()
  139. console.log(num);
  140. animation(box,{
  141. data:{
  142. left:-700*num
  143. }
  144. },500);
  145. }
  146. date=new Date();
  147. }
  148. for(let i=0;i<len;i++){
  149. circleLi[i].onclick=function(){
  150. for(let i=0;i<len;i++){
  151. circleLi[i].style.opacity="0.3";
  152. }
  153. this.style.opacity="1";
  154. animation(box,{
  155. data:{
  156. left:-700*i
  157. }
  158. },500);
  159. }
  160. }
  161. </script>
  162. </body>
  163. </html>

小球运动

示意图:
1566536768663_60.png

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title> </title>
  6. <style>
  7. #ball{
  8. width: 200px;
  9. height: 200px;
  10. background-color: purple;
  11. position: absolute;
  12. top: 0px;
  13. left: 0px;
  14. border-radius: 50%;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="ball"></div>
  20. </body>
  21. <script>
  22. var ball=document.getElementById("ball");
  23. var speedX=4; //小球每次x轴移动的像素
  24. var speedY=4; //小球每次y轴移动的像素
  25. var moveX=ball.offsetLeft; //小球x轴移动了多少
  26. var moveY=ball.offsetTop; //小球y轴移动了多少
  27. var maxX=document.documentElement.clientWidth-ball.offsetWidth;//x轴最大值
  28. var maxY=document.documentElement.clientHeight-ball.offsetHeight;
  29. window.onresize=function(){//监控窗口变化
  30. maxY=document.documentElement.clientHeight-ball.offsetHeight;
  31. maxX=document.documentElement.clientWidth-ball.offsetWidth;
  32. };
  33. function ballRun(){
  34. moveX+=speedX;
  35. moveY+=speedY;
  36. if(moveX<=0||moveX>=maxX){//防止超过边界值,以及变化方向
  37. moveX=Math.max(0,moveX);
  38. moveX=Math.min(maxX,moveX);
  39. speedX=-speedX;
  40. }
  41. if(moveY<=0||moveY>=maxY){
  42. moveY=Math.max(0,moveY);
  43. moveY=Math.min(maxY,moveY);
  44. speedY=-speedY;
  45. }
  46. ball.style.left=moveX+"px";//改变小球的绝对定位进行移动
  47. ball.style.top=moveY+"px";
  48. requestAnimationFrame(ballRun);
  49. }
  50. ballRun();
  51. </script>
  52. </html>

盒子移动和放大缩小

重点代码:
boxMoveX=Math.min(mouseMoveX-mouseNowX,parseFloat(getStyle(box).width)-100);
boxMoveY=Math.min(mouseMoveY-mouseNowY,parseFloat(getStyle(box).height)-100);

  1. <!DOCTYPE html>
  2. <html lang="en" ondragstart="return false">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>盒子拖放</title>
  6. <style>
  7. *{
  8. margin: 0px;
  9. padding: 0px;
  10. list-style: none;
  11. }
  12. #box{
  13. width: 300px;
  14. height: 300px;
  15. background-color: purple;
  16. position: absolute;
  17. top: 0px;
  18. left: 0px;
  19. }
  20. #box>div{
  21. width: 20px;
  22. height: 20px;
  23. background-color: red;
  24. position: absolute;
  25. }
  26. #eft-top{
  27. top: 0px;
  28. left: 0px;
  29. }
  30. #left{
  31. top: 50%;
  32. left: 0px;
  33. transform: translate(0, -50%);
  34. }
  35. #left-bottom{
  36. bottom:0px;
  37. left: 0px;
  38. }
  39. #right-top{
  40. right: 0px;
  41. top: 0px;
  42. }
  43. #right{
  44. right: 0px;
  45. top: 50%;
  46. transform: translate(0, -50%);
  47. }
  48. #right-bottom{
  49. right: 0px;
  50. bottom: 0px;
  51. }
  52. #top{
  53. left: 50%;
  54. top: 0px;
  55. transform: translate(-50%, 0);
  56. }
  57. #bottom{
  58. left: 50%;
  59. bottom: 0px;
  60. transform: translate(-50%, 0);
  61. }
  62. </style>
  63. </head>
  64. <body>
  65. <div id="box">
  66. <div id="top"></div>
  67. <div id="bottom"></div>
  68. <div id="left-top"></div>
  69. <div id="left"></div>
  70. <div id="left-bottom"></div>
  71. <div id="right-top"></div>
  72. <div id="right"></div>
  73. <div id="right-bottom"></div>
  74. </div>
  75. <script>
  76. var box=document.getElementById("box");//获取盒子的id
  77. box.onmousedown=function(e){ //盒子的鼠标按下事件
  78. e=e||window.event; //对于鼠标对象的ie兼容
  79. var mouseDownX=e.clientX, //获取第一下鼠标按下的点距离左边x轴值
  80. mouseDownY=e.clientY; //获取第一下鼠标按下的点距离右边y轴值
  81. targetId=e.target.id;
  82. document.onmousemove=function(e){//鼠标按下后的鼠标移动事件
  83. e=e||window.event; //兼容鼠标对象
  84. //假设场景有很多定位元素嵌套,建议还是写个方法获取到它的左边和上边的距离
  85. var boxX=box.offsetLeft, //盒子到浏览器左边距离
  86. boxY=box.offsetTop, //盒子到浏览器上边距离
  87. mouseMoveX=e.clientX, //获取每次鼠标移动获取的点距离左边x轴值
  88. mouseMoveY=e.clientY, //获取每次鼠标移动获取的点距离左边y轴值
  89. mouseDiffX=mouseMoveX-mouseDownX, //获取第一次按下和每次移动x轴的差值
  90. mouseDiffY=mouseMoveY-mouseDownY; //获取第一次按下和每次移动Y轴的差值
  91. boxWidth=box.offsetWidth; //获取盒子模型的宽度
  92. boxHeight=box.offsetHeight; //获取盒子模型的高度
  93. mouseDownX=mouseMoveX; //每次移动完都将每次移动的x轴的值重新赋值给第一次鼠标按下的x轴的值
  94. mouseDownY=mouseMoveY; //每次移动完都将每次移动的x轴的值重新赋值给第一次鼠标按下的y轴的值
  95. switch(targetId){//根据事件代理的得到的id使用switch语句赋予对应功能
  96. case "top":
  97. mouseDiffY=Math.min(mouseDiffY,boxHeight-150);//最重点就是这个,限制盒子的大小
  98. box.style.height=boxHeight-mouseDiffY+"px";
  99. box.style.top=boxY+mouseDiffY+"px";
  100. break;
  101. case "bottom":
  102. mouseDiffY=Math.max(mouseDiffY,-(boxHeight-150));
  103. box.style.height=boxHeight+mouseDiffY+"px";
  104. break;
  105. case "left":
  106. mouseDiffX=Math.min(mouseDiffX,boxWidth-150);
  107. box.style.width=boxWidth-mouseDiffX+"px";
  108. box.style.left=boxX+mouseDiffX+"px";
  109. break;
  110. case "right":
  111. mouseDiffX=Math.max(mouseDiffX,-(boxWidth-150));
  112. box.style.width=boxWidth+mouseDiffX+"px";
  113. break;
  114. case "left-top":
  115. mouseDiffX=Math.min(mouseDiffX,boxWidth-150);
  116. mouseDiffY=Math.min(mouseDiffY,boxHeight-150);
  117. box.style.width=boxWidth-mouseDiffX+"px";
  118. box.style.left=boxX+mouseDiffX+"px";
  119. box.style.height=boxHeight-mouseDiffY+"px";
  120. box.style.top=boxY+mouseDiffY+"px";
  121. break;
  122. case "left-bottom":
  123. mouseDiffX=Math.min(mouseDiffX,boxWidth-150);
  124. mouseDiffY=Math.max(mouseDiffY,-(boxHeight-150));
  125. box.style.width=boxWidth-mouseDiffX+"px";
  126. box.style.left=boxX+mouseDiffX+"px";
  127. box.style.height=boxHeight+mouseDiffY+"px";
  128. break;
  129. case "right-top":
  130. mouseDiffX=Math.max(mouseDiffX,-(boxWidth-150));
  131. mouseDiffY=Math.min(mouseDiffY,boxHeight-150);
  132. box.style.width=boxWidth+mouseDiffX+"px";
  133. box.style.height=boxHeight-mouseDiffY+"px";
  134. box.style.top=boxY+mouseDiffY+"px";
  135. break;
  136. case "right-bottom":
  137. mouseDiffX=Math.max(mouseDiffX,-(boxWidth-150));
  138. mouseDiffY=Math.max(mouseDiffY,-(boxHeight-150));
  139. box.style.width=boxWidth+mouseDiffX+"px";
  140. box.style.height=boxHeight+mouseDiffY+"px";
  141. break;
  142. default:
  143. box.style.left=boxX+mouseDiffX+"px"; //然后把原本距离左边的值加上应该移动的值就是盒子的x轴定位值
  144. box.style.top=boxY+mouseDiffY+"px"; //然后把原本距离上边的值加上应该移动的值就是盒子的y轴定位值
  145. }
  146. }
  147. }
  148. document.onmouseup=function(){//鼠标抬起事件,清除掉鼠标的移动事件
  149. this.onmousemove=null;
  150. }
  151. </script>
  152. </body>
  153. </html>

表格增删选择

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>TEST</title>
  6. <style>
  7. *{
  8. margin: 0px;
  9. padding: 0px;
  10. }
  11. div{
  12. text-align: center;
  13. }
  14. table{
  15. margin: 0px auto;
  16. border-collapse: collapse;
  17. width: 500px;
  18. height:70px;
  19. }
  20. tr,td,th{
  21. border: 1px solid black;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div>
  27. <p>请输入信息</p>
  28. 性别:<input type="text" id="sex"><br>
  29. 年龄:<input type="text" id="age"><br>
  30. 姓名:<input type="text" id="nam"><br>
  31. <input type="button" value="提交" id="input">
  32. <input type="button" value="全选" id="all">
  33. <input type="button" value="反选" id="reverse">
  34. <input type="button" value="删除选中的" id="deleteSome">
  35. <table id="table">
  36. <tr>
  37. <th>性别</th>
  38. <th>年龄</th>
  39. <th>姓名</th>
  40. <th>勾选</th>
  41. <th>删除</th>
  42. </tr>
  43. <tr>
  44. <td>男</td>
  45. <td>13</td>
  46. <td>呵呵大</td>
  47. <td><input type="checkbox" class="che"></td>
  48. <td class="deleteX" onclick="deleteTr.call(this)">X</td>
  49. </tr>
  50. </table>
  51. </div>
  52. <script>
  53. var input=document.getElementById("input"),
  54. sex=document.getElementById("sex"),
  55. age=document.getElementById("age"),
  56. nam=document.getElementById("nam"),
  57. all=document.getElementById("all"),
  58. reverse=document.getElementById("reverse"),
  59. deleteSome=document.getElementById("deleteSome"),
  60. deleteX=document.getElementById("deleteX");
  61. table=document.getElementById("table");
  62. tbody=table.children[0];
  63. input.onclick=function(){
  64. var newTr=document.createElement("tr"),
  65. newStd=document.createElement("td"),
  66. newAtd=document.createElement("td"),
  67. newNtd=document.createElement("td"),
  68. newItd=document.createElement("td"),
  69. newDtd=document.createElement("td"),
  70. newInput=document.createElement("input");
  71. newStd.innerHTML=sex.value;
  72. newAtd.innerHTML=age.value;
  73. console.log(nam.value);
  74. newNtd.innerHTML=nam.value;
  75. newInput.setAttribute("type","checkbox");
  76. newInput.className="che";
  77. newDtd.className="deleteX";
  78. newDtd.setAttribute("onclick","deleteTr.call(this)");
  79. newDtd.innerHTML="X";
  80. newItd.appendChild(newInput);
  81. newTr.appendChild(newStd);
  82. newTr.appendChild(newAtd);
  83. newTr.appendChild(newNtd);
  84. newTr.appendChild(newItd);
  85. newTr.appendChild(newDtd);
  86. tbody.appendChild(newTr);
  87. sex.value="";
  88. age.value="";
  89. nam.value="";
  90. }
  91. all.onclick=function(){
  92. var checkBox=document.getElementsByClassName("che");
  93. var cLength=checkBox.length;
  94. for(let i=0;i<cLength;i++){
  95. checkBox[i].checked="true";
  96. }
  97. }
  98. reverse.onclick=function(){
  99. var checkBox=document.getElementsByClassName("che");
  100. var cLength=checkBox.length;
  101. for(let i=0;i<cLength;i++){
  102. checkBox[i].checked=!checkBox[i].checked;
  103. }
  104. }
  105. deleteSome.onclick=function(){
  106. var checkBox=document.getElementsByClassName("che");
  107. for(let i=0;i<checkBox.length;i++){
  108. if(checkBox[i].checked){
  109. tbody.removeChild(checkBox[i].parentNode.parentNode);
  110. i-=1;
  111. }
  112. }
  113. }
  114. function deleteTr(){
  115. tbody.removeChild(this.parentNode);
  116. }
  117. </script>
  118. </body>
  119. </html>

画板

  1. <!DOCTYPE html>
  2. <html lang="en" ondragstart="return false">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>paint</title>
  6. <style>
  7. *{
  8. margin: 0px;
  9. padding: 0px;
  10. }
  11. #paint{
  12. border: 3px solid black;
  13. height: 600px;
  14. margin-top: 100px;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <input type="text" placeholder="size" id="size">
  20. <input type="color" id="background-color">
  21. <input type="color" id="pen-color">
  22. <input type="button" id="clear" value="清空">
  23. <input type="button" id="go-back" value="撤回">
  24. <div id="paint"></div>
  25. <script>
  26. var penColor=document.getElementById("pen-color"),
  27. boardColor=document.getElementById("background-color"),
  28. penSize=document.getElementById("size"),
  29. board=document.getElementById("paint"),
  30. clear=document.getElementById("clear"),
  31. goBack=document.getElementById("go-back"),
  32. penColorSave="black",
  33. penSizeSave=10,
  34. goBackArr=[],
  35. goBackSave=[];
  36. boardColor.onchange=function(){
  37. board.style.backgroundColor=this.value;
  38. }
  39. penColor.onchange=function(){
  40. penColorSave=this.value;
  41. }
  42. penSize.onchange=function(){
  43. penSizeSave=this.value;
  44. }
  45. clear.onclick=function(){
  46. var cLength=board.children.length;
  47. for(let i=0;i<cLength;i++){
  48. board.removeChild(board.children[0]);
  49. }
  50. }
  51. goBack.onclick=function(){
  52. var goBackArrLength=goBackArr.length;
  53. if(goBackArrLength===0){
  54. return;
  55. }else{
  56. var lastSave=goBackArr[goBackArrLength-1],
  57. lastSaveLength=lastSave.length;
  58. for(let i=0;i<lastSaveLength;i++){
  59. board.removeChild(lastSave[i]);
  60. }
  61. }
  62. goBackArr.pop();
  63. }
  64. board.onmousedown=function(e){
  65. e=e||window.event;
  66. var mouseDownX=e.clientX,
  67. mouseDownY=e.clientY,
  68. mouseDownDiv=document.createElement("div");
  69. mouseDownDiv.style.cssText="width:"+penSizeSave+"px;height:"+penSizeSave+"px;background-color:"+penColorSave+";position:absolute;top:"+(mouseDownY-penSizeSave*0.5)+"px;left:"+(mouseDownX-penSizeSave*0.5)+"px;border-radius:50%";
  70. this.appendChild(mouseDownDiv);
  71. goBackSave.push(mouseDownDiv);
  72. this.onmousemove=function(e){
  73. e=e||window.event;
  74. var mouseMoveX=e.clientX,
  75. mouseMoveY=e.clientY,
  76. mouseMoveDiv=document.createElement("div");
  77. mouseMoveDiv.style.cssText="width:"+penSizeSave+"px;height:"+penSizeSave+"px;background-color:"+penColorSave+";position:absolute;top:"+(mouseMoveY-penSizeSave*0.5)+"px;left:"+(mouseMoveX-penSizeSave*0.5)+"px;border-radius:50%";
  78. this.appendChild(mouseMoveDiv);
  79. goBackSave.push(mouseMoveDiv);
  80. }
  81. document.onmouseup=function(){
  82. goBackArr.push(goBackSave);
  83. goBackSave=[];
  84. board.onmousemove=null;
  85. this.onmouseup=null;
  86. }
  87. }
  88. </script>
  89. </body>
  90. </html>

JOSNP百度案例

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0px;
            padding: 0px;
            list-style: none;
        }

        html {
            min-width: 800px;
        }

        #box {
            cursor: default;
        }

        .nav {
            padding-top: 18px;
            padding-right: 12px;
        }

        .nav li {
            font-size: 14px;
            font-weight: bold;
            float: right;
            margin-left: 25px;
        }

        .nav a {
            color: black;
        }

        .more-product {
            background-color: #3388ff;
            padding: 4px;
            position: relative;
            top: -4px;
        }

        .more-product>a {
            text-decoration: none;
            color: white;
        }

        h1 {
            margin-top: 170px;
            text-align: center;
        }

        h1 img {
            width: 270px;
        }

        #search {
            width: 532px;
            height: 34px;
            padding-left: 10px;
            border: 1px solid #cccccc;
            border-right: 0px;
            outline: none;
        }

        #goBaidu {
            color: white;
            font-size: 15px;
            text-decoration: none;
            background-color: #3388ff;
            padding: 8px;
            margin-left: -6px;
            position: relative;
            top: 1px;
        }

        .center {
            position: relative;
            text-align: center;
        }

        .center i {
            display: inline-block;
            position: absolute;
            top: 8px;
            left: -26px;
            width: 18px;
            height: 22px;
            background: url(images/camera_new_5606e8f.png) no-repeat 0px 3px;
            cursor: pointer;
        }

        .two-code {
            text-align: center;
        }

        .two-code img {
            display: block;
            margin: 380px auto 5px auto;
        }

        .two-code span {
            font-size: 14px;
            font-weight: bold;
        }

        footer {
            text-align: center;
            margin-top: 20px;

            font-size: 12px;
        }

        .f-firstline a {
            margin-right: 20px;
            color: #a69999;
        }

        .f-firstline a:nth-of-type(4) {
            margin: 0px;
        }

        .lastline {
            position: relative;
            color: #a69999;
            margin-top: 10px;
        }

        .lastline a {
            color: #a69999;
        }

        #box {
            width: 541px;
            position: absolute;
            border: 1px solid #cccccc;
            border-top: 0px;
            left: 651px;
            text-align: left;
        }
        }
    </style>
</head>

<body>
    <ul class="nav">
        <li class="more-product">
            <a href="">更多产品</a>
        </li>
        <li>
            <a href="">设置</a>
        </li>
        <li>
            <a href="">登录</a>
        </li>
        <li>
            <a href="">学术</a>
        </li>
        <li>
            <a href="">贴吧</a>
        </li>
        <li>
            <a href="">视频</a>
        </li>
        <li>
            <a href="">地图</a>
        </li>
        <li>
            <a href="">hao123</a>
        </li>
        <li>
            <a href="">新闻</a>
        </li>
    </ul>
    <h1>
        <img src="images/bd_logo1.png" alt="">
    </h1>
    <div class="center">
        <input type="text" placeholder="" id="search">
        <a href="" id="goBaidu">
            <span id="btn">百度一下</span>
            <i id="icon"></i>
        </a>
        <ul id="box">
        </ul>
    </div>

    <div class="two-code">
        <img src="images/zbios_efde696.png" alt="">
        <span>百度</span>
    </div>
    <footer>
        <div class="f-firstline">
            <a href="">把百度设为主页</a>
            <a href="">关于百度</a>
            <a href="">About  Baidu</a>
            <a href="">百度推广</a>
        </div>
        <div class="lastline">

            ©2018  Baidu  
            <a href="">使用百度前必读</a>  
            <a href="">意见反馈</a>  京ICP证030173号        京公网安备11000002000001号
            <i class="last-icon-one"></i>
            <i class="last-icon-two"></i>
        </div>
    </footer>

    <script>

        var search = document.getElementById("search"),
            head = document.getElementsByTagName("head")[0],
            arr = [],
            box = document.getElementById("box"),
            btn = document.getElementById("btn"),
            goBaidu = document.getElementById("goBaidu"),
            icon = document.getElementById("icon");
        arrL = 0;

        search.oninput = function () {
            for (key in head.children) {
                if (head.children[key].name === "baidu") {
                    head.removeChild(head.children[key]);
                }
            }
            var searchFont = this.value,
                myScript = document.createElement("script");
            head.appendChild(myScript);
            myScript.src = "https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=" + searchFont + "&json=1&p=3&sid=1424_25809_21101_26350_27244_22159&req=2&csor=2&pwd=r&cb=fnShow";
            myScript.name = "baidu";
        }

        function fnShow(json) {
            box.innerHTML = "";
            arr = json.s;
            arrL = arr.length;
            console.log(arr);
            for (let i = 0; i < arrL; i++) {
                var li = document.createElement("li");
                li.innerHTML = "          " + arr[i];
                li.style.cssText = "height:30px;padding-left: 10px;line-height:30px";
                li.index = i;
                li.onmouseover = function () {
                    removeAll();
                    this.style.backgroundColor = "#e1e1e1";
                    arrL = this.index;
                }
                box.appendChild(li);
            }
        }
        function getStyle(ele) {
            return ele.currentStyle || getComputedStyle(ele);
        }
        function removeAll() {
            var liC = box.children;
            for (let i = 0; i < arr.length; i++) {
                liC[i].style.backgroundColor = "#ffffff";
            }
        }
        search.onkeyup = function (e) {
            var lis = box.children;
            if (e.keyCode === 38) {
                removeAll();
                --arrL < 0 ? (arrL = arr.length - 1) : arrL;
                lis[arrL].style.backgroundColor = "#e1e1e1";
            } else if (e.keyCode === 40) {
                removeAll();
                ++arrL > arr.length - 1 ? arrL = 0 : arrL;
                lis[arrL].style.backgroundColor = "#e1e1e1";
            } else if (e.keyCode === 13) {
                console.log(1111111111);
                for (let i = 0; i < arr.length; i++) {
                    console.log(lis[i]);
                    if (getStyle(lis[i]).backgroundColor === "rgb(225, 225, 225)") {
                        window.location.href = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=" + lis[i].innerHTML;
                    }
                }
                if (search.value.trim === "") {
                    window.location.href = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=" + search.value;
                }
            }
        }
        search.onfocus = function () {
            this.style.borderColor = "#4791ff";
        }
        search.onblur = function () {
            this.style.borderColor = "#cccccc";
        }
        btn.onclick = function () {
            var searchFont = search.value;
            if (searchFont != "") {
                goBaidu.href = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=" + searchFont;
            }
        }

        box.onclick = function (e) {
            window.location.href = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=" + e.target.innerHTML;
        }

        box.onmouseout = function () {
            removeAll();
        }
        icon.onmouseover = function () {
            this.style.backgroundPositionY = "-17px";
        }
        icon.onmouseout = function () {
            this.style.backgroundPositionY = "3px";
        }
    </script>
</body>
</html>