当前位置:   article > 正文

每天几个js小案例,有手就能会

每天几个js小案例,有手就能会

目录

1.圆周运动

2.英雄归为

3.反弹的小球


1.圆周运动

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <style>
  10. .bdr1 {
  11. border-radius: 50%;
  12. width: 300px;
  13. height: 300px;
  14. border: 1px solid red;
  15. position: relative;
  16. }
  17. .bdr2 {
  18. border-radius: 50%;
  19. width: 200px;
  20. height: 200px;
  21. border: 1px solid green;
  22. position: absolute;
  23. left: 0;
  24. right: 0;
  25. top: 0;
  26. bottom: 0;
  27. margin: auto;
  28. }
  29. .bdr3 {
  30. border-radius: 50%;
  31. width: 100px;
  32. height: 100px;
  33. border: 1px solid rebeccapurple;
  34. position: absolute;
  35. left: 0;
  36. right: 0;
  37. top: 0;
  38. bottom: 0;
  39. margin: auto;
  40. }
  41. .bool {
  42. width: 50px;
  43. height: 50px;
  44. border-radius: 50%;
  45. background-color: skyblue;
  46. position: absolute;
  47. left: 0px;
  48. top: 125px;
  49. }
  50. .boolm {
  51. width: 50px;
  52. height: 50px;
  53. border-radius: 50%;
  54. background-color: rgb(226, 99, 99);
  55. position: absolute;
  56. left: 0px;
  57. top: 75px;
  58. }
  59. </style>
  60. <body>
  61. <div class="bdr1">
  62. <div class="bool" id="bool"></div>
  63. <div class="bdr2">
  64. <div class="boolm" id="boolm"></div>
  65. <div class="bdr3"></div>
  66. </div>
  67. </div>
  68. <button onclick="check()">开始</button>
  69. </body>
  70. <script>
  71. let bool = document.getElementById('bool')
  72. let boolm = document.getElementById('boolm')
  73. function check() {
  74. circleMove(bool, 0, 125, 125, 20)
  75. circleMove(boolm, 0, 75, 75, 20)
  76. }
  77. function circleMove(ball, x0, y0, r, t) {
  78. let x = 0, y
  79. let dsq1 = setInterval(() => {
  80. ball.style.left = x0 + x++ + 'px'
  81. y = -Math.sqrt(2 * x * r - Math.pow(x, 2))
  82. ball.style.top = y0 + y + 'px'
  83. if (x == 2 * r && y == 0) {
  84. clearInterval(dsq1)
  85. let dsq2 = setInterval(() => {
  86. ball.style.left = x0 + x-- + 'px'
  87. y = Math.sqrt(2 * x * r - Math.pow(x, 2))
  88. ball.style.top = y0 + y + 'px'
  89. if (x == 0 && y == 0) {
  90. clearInterval(dsq2)
  91. circleMove(ball, x0, y0, r, t)
  92. }
  93. }, t)
  94. }
  95. }, t)
  96. }
  97. </script>
  98. </html>

2.英雄归为

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <style>
  10. img {
  11. width: 200px;
  12. height: 200px;
  13. float: left;
  14. }
  15. .picture {
  16. width: 1600px;
  17. height: 200px;
  18. position: relative;
  19. }
  20. .picture>div {
  21. position: absolute;
  22. }
  23. .kuang {
  24. width: 1600px;
  25. height: 200px;
  26. }
  27. .a {
  28. width: 200px;
  29. height: 200px;
  30. float: left;
  31. margin-left: 40px;
  32. border: 1px solid black;
  33. text-align: center;
  34. line-height: 200px;
  35. }
  36. </style>
  37. <body>
  38. <div class="picture">
  39. <div style="left: 0px;" id="picture1"><img src="图片/关羽.webp" alt="" onclick="move1()"></div>
  40. <div style="left: 200px;" id="picture2"><img src="图片/貂蝉.webp" alt="" onclick="move2()"></div>
  41. <div style="left: 400px;" id="picture3"><img src="图片/刘协.webp" alt="" onclick="move3()"></div>
  42. <div style="left: 600px;" id="picture4"><img src="图片/孙权.webp" alt="" onclick="move4()"></div>
  43. <div style="left: 800px;" id="picture5"><img src="图片/曹操.jpg" alt="" onclick="move5()"></div>
  44. <div style="left: 1000px;" id="picture6"><img src="图片/刘备(1).jpg" alt="" onclick="move6()"></div>
  45. </div>
  46. <hr>
  47. <div class="kuang">
  48. <div class="a">刘协</div>
  49. <div class="a">刘备</div>
  50. <div class="a">关羽</div>
  51. <div class="a">曹操</div>
  52. <div class="a">孙权</div>
  53. <div class="a">貂蝉</div>
  54. </div>
  55. </body>
  56. <script>
  57. function change(img, x, y) {
  58. document.onkeydown = function (e) {
  59. switch (e.keyCode) {
  60. //
  61. case 37:
  62. x--
  63. img.style.left = x + 'px'
  64. console.log(1)
  65. break
  66. //
  67. case 39:
  68. x++
  69. img.style.left = x + 'px'
  70. console.log(2)
  71. break
  72. //
  73. case 38:
  74. y--
  75. img.style.top = y + 'px'
  76. break
  77. //
  78. case 40:
  79. y++
  80. img.style.top = y + 'px'
  81. break
  82. }
  83. }
  84. }
  85. function move1() {
  86. change(picture1, 0, 0)
  87. }
  88. function move2() {
  89. change(picture2, 200, 0)
  90. }
  91. function move3() {
  92. change(picture3, 400, 0)
  93. }
  94. function move4() {
  95. change(picture4, 600, 0)
  96. }
  97. function move5() {
  98. change(picture5, 800, 0)
  99. }
  100. function move6() {
  101. change(picture6, 1000, 0)
  102. }
  103. </script>
  104. </html>

3.反弹的小球

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Document</title>
  5. <style>
  6. .f {
  7. width: 1200px;
  8. height: 800px;
  9. border: 1px solid red;
  10. position: relative;
  11. }
  12. .z {
  13. width: 100px;
  14. height: 100px;
  15. border-radius: 50%;
  16. background-color: #6cf;
  17. position: absolute;
  18. /* 正中心 */
  19. left: 550px;
  20. top: 350px;
  21. }
  22. .kuai {
  23. position: relative;
  24. width: 400px;
  25. height: 20px;
  26. background-color: red;
  27. left: 0px;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div class="f" onclick="startgame()">
  33. <div id="ball" class="z">
  34. </div>
  35. </div>
  36. <div id="kuai" class="kuai"></div>
  37. </body>
  38. <script>
  39. //1、生成01200的随机整数
  40. let r = Math.floor(Math.random() * 1200)
  41. console.log(r)
  42. //小球球心着落的随机点坐标是(r,50
  43. //小球球心坐标(x,y)
  44. let x = 600, y = 400
  45. //计算x和y之间运动距离的倍数关系
  46. let x_y = (x - r) / (y - 50)
  47. let dy = 1//表示 单位时间 内沿着y轴向上运动1
  48. let dx = x_y * dy//表示 单位时间 沿着x轴的位移
  49. //获取小球的dom对象
  50. let ball = document.getElementById('ball')
  51. let kuai = document.getElementById('kuai')
  52. let x1 = 0
  53. let a = 30
  54. //点击父元素任意位置,开始游戏
  55. function startgame() {
  56. setInterval(() => {
  57. //上下碰撞
  58. if (y <= 50) {
  59. dy = -dy
  60. }
  61. if (y >= 750 && x1 < x && x < x1 + 400) {
  62. console.log(x)
  63. console.log(x1)
  64. dy = -dy
  65. }
  66. //左右碰撞
  67. if (x <= 50 || x >= 1150) {
  68. dx = -dx
  69. }
  70. //把单位时间的位移作用在坐标上
  71. x += dx
  72. y += dy
  73. //把坐标作用在实际的定位上
  74. ball.style.left = x - 50 + 'px'
  75. ball.style.top = y - 50 + 'px'
  76. }, 1)
  77. }
  78. document.onkeydown = function (e) {
  79. switch (e.keyCode) {
  80. //
  81. case 37:
  82. x1 -= a
  83. kuai.style.left = x1 + 'px'
  84. break
  85. //
  86. case 39:
  87. x1 += a
  88. kuai.style.left = x1 + 'px'
  89. break
  90. }
  91. }
  92. </script>
  93. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/717626
推荐阅读
相关标签
  

闽ICP备14008679号