当前位置:   article > 正文

Html+Css+JS实现悬浮球移动+吸附+菜单效果_html 悬浮球

html 悬浮球

Css

  1. :root {
  2. --item-translateX: -100px;
  3. }
  4. /* * {
  5. margin: 0;
  6. padding: 0;
  7. } */
  8. /* body,
  9. html {
  10. width: 100%;
  11. height: 100%;
  12. } */
  13. /* body {
  14. background: linear-gradient(rgb(35, 189, 236), rgb(45, 99, 248));
  15. } */
  16. .boxButton+.circular,
  17. .boxButton+.circular+.fork,
  18. .boxButton,
  19. .box-item {
  20. position: absolute;
  21. top: 200px;
  22. right: -20px;
  23. }
  24. .boxButton {
  25. display: block;
  26. width: 40px;
  27. height: 40px;
  28. z-index: 2;
  29. opacity: 0;
  30. cursor: pointer;
  31. }
  32. .boxButton+.circular {
  33. width: 30px;
  34. height: 30px;
  35. display: block;
  36. z-index: 1;
  37. border-radius: 50%;
  38. /* 控制中心圆圈 */
  39. border: 5px solid rgba(255, 255, 255, 0.7);
  40. /* 圆圈中心颜色 */
  41. /* background: rgba(255, 255, 255, 0.2); */
  42. background-color: blueviolet;
  43. /* 外围光圈阴影 */
  44. box-shadow: 0px 0px 0px 10px rgba(255, 255, 255, 0.2);
  45. margin: 10px;
  46. /* 过渡 */
  47. transition: all 0.5s;
  48. transform-origin: 50% 50%;
  49. }
  50. .boxButton:checked+.circular+.fork {
  51. width: 40px;
  52. height: 40px;
  53. display: block;
  54. z-index: 1;
  55. border-radius: 50%;
  56. /* 控制另一个交互颜色*/
  57. /* background: rgba(255, 255, 255, 0.2); */
  58. background-color: blue;
  59. /* 另外一个周围阴影颜色效果 */
  60. box-shadow: 0px 0px 0px 10px rgba(255, 255, 255, 0.2);
  61. margin: 10px;
  62. /* 过渡 */
  63. transition: all 0.5s;
  64. }
  65. /* 中心的X */
  66. .boxButton:checked+.circular+.fork::after,
  67. .boxButton:checked+.circular+.fork::before {
  68. content: "";
  69. width: 30px;
  70. height: 3px;
  71. display: block;
  72. z-index: 1;
  73. border-radius: 2.5px;
  74. background: rgba(255, 255, 255, 0.7);
  75. transition: all 0.5s;
  76. content: "";
  77. position: absolute;
  78. top: 50%;
  79. left: 50%;
  80. transform: translate(-50%, -50%);
  81. }
  82. /* 旋转效果 点击后里面按钮 */
  83. .boxButton:checked+.circular+.fork {
  84. transform: rotate(360deg);
  85. }
  86. /* 中间×的位置变化 */
  87. .boxButton:checked+.circular+.fork::after {
  88. transform: translate(-50%, -50%) rotate(45deg);
  89. }
  90. /* 中间×的位置变化 */
  91. .boxButton:checked+.circular+.fork::before {
  92. transform: translate(-50%, -50%) rotate(-45deg);
  93. }
  94. .boxButton:checked+.circular {
  95. width: 0;
  96. height: 0;
  97. border: 0px;
  98. box-shadow: none;
  99. }
  100. .boxButton:checked~ul .box-item {
  101. opacity: 1;
  102. }
  103. .box-item:nth-child(1) {
  104. transform: rotate(90deg);
  105. }
  106. .box-item:nth-child(2) {
  107. transform: rotate(45deg);
  108. }
  109. .box-item:nth-child(3) {
  110. transform: rotate(0deg);
  111. }
  112. .box-item:nth-child(4) {
  113. transform: rotate(315deg);
  114. }
  115. .box-item:nth-child(5) {
  116. transform: rotate(270deg);
  117. }
  118. .box-item:nth-child(1) a {
  119. transform: rotate(0deg);
  120. }
  121. .box-item:nth-child(2) a {
  122. transform: rotate(-45deg);
  123. }
  124. .box-item:nth-child(3) a {
  125. transform: rotate(0deg);
  126. }
  127. .box-item:nth-child(4) a {
  128. transform: rotate(45deg);
  129. }
  130. .box-item:nth-child(5) a {
  131. transform: rotate(90deg);
  132. }
  133. /* 可控制按钮分布 */
  134. .boxButton1:checked~ul .box-item:nth-child(1) {
  135. /* 更改rotate值控制按钮散波方向 */
  136. transform: rotate(45deg) translateX(var(--item-translateX));
  137. }
  138. .boxButton1:checked~ul .box-item:nth-child(2) {
  139. transform: rotate(0deg) translateX(var(--item-translateX));
  140. }
  141. .boxButton1:checked~ul .box-item:nth-child(3) {
  142. transform: rotate(-45deg) translateX(var(--item-translateX));
  143. }
  144. /* .boxButton:checked~ul .box-item:nth-child(4) {
  145. transform: rotate(315deg) translateX(var(--item-translateX));
  146. }
  147. .boxButton:checked~ul .box-item:nth-child(5) {
  148. transform: rotate(270deg) translateX(var(--item-translateX));
  149. } */
  150. .boxButton:checked~ul .box-item a {
  151. pointer-events: auto;
  152. }
  153. .boxButton+.circular::before {
  154. top: 10px;
  155. }
  156. .boxButton+.circular::after {
  157. top: -10px;
  158. }
  159. .box-item {
  160. display: block;
  161. width: 60px;
  162. height: 60px;
  163. opacity: 0;
  164. transition: 0.5s;
  165. }
  166. .box-item a {
  167. display: block;
  168. width: inherit;
  169. height: inherit;
  170. line-height: 60px;
  171. /* 过渡显示颜色 */
  172. /* color: rgba(255, 255, 255, 0.7); */
  173. color: brown;
  174. /* background: rgba(255, 255, 255, 0.2); */
  175. /* 标签背景颜色 */
  176. background-color: yellow;
  177. border-radius: 50%;
  178. text-align: center;
  179. text-decoration: none;
  180. font-size: 30px;
  181. pointer-events: none;
  182. transition: 0.2s;
  183. }
  184. .touming {
  185. opacity: 0.3;
  186. }
  187. .xuanfu {
  188. position: absolute;
  189. }
  190. .boxButton2:checked~ul .box-item:nth-child(1) {
  191. /* 更改rotate值控制按钮散波方向 */
  192. transform: rotate(135deg) translateX(var(--item-translateX));
  193. }
  194. .boxButton2:checked~ul .box-item:nth-child(2) {
  195. transform: rotate(180deg) translateX(var(--item-translateX));
  196. }
  197. .boxButton2:checked~ul .box-item:nth-child(3) {
  198. transform: rotate(225deg) translateX(var(--item-translateX));
  199. }
  200. /* .box-button1:checked~ul .box-item:nth-child(4) {
  201. transform: rotate(315deg) translateX(var(--item-translateX));
  202. }
  203. .box-button1:checked~ul .box-item:nth-child(5) {
  204. transform: rotate(270deg) translateX(var(--item-translateX));
  205. } */

Html

  1. <div class="box xuanfu touming">
  2. <input type="checkbox" class="boxButton " id="box-buttons" />
  3. <label class="circular" for="box-buttons"></label>
  4. <label class="fork" for="box-buttons"></label>
  5. <ul>
  6. <li class="box-item">
  7. <a href="#"><i class="fa fa-square-o"></i></a>
  8. </li>
  9. <li class="box-item">
  10. <a href="#"><i class="fa fa-bars"></i></a>
  11. </li>
  12. <li class="box-item">
  13. <a href="#"><i class="fa fa-lock"></i></a>
  14. </li>
  15. <!-- <li class="box-item">
  16. <a href="#"><i class="fa fa-crop"></i></a>
  17. </li> -->
  18. <!-- <li class="box-item">
  19. <a href="#"><i class="fa fa-commenting-o"></i></a>
  20. </li> -->
  21. </ul>
  22. </div>

JS部分

  1. // 获取元素的对象
  2. const xuanfu = document.querySelector('.xuanfu')
  3. const boxButton = document.querySelector('.boxButton')
  4. // console.log(xuanfu)
  5. // console.log(boxButton)
  6. //绑定事件触摸
  7. xuanfu.addEventListener('touchstart', function (e) {
  8. // 获取元素当前距离左侧的偏移量
  9. this.l = this.offsetLeft
  10. // 获取触摸开始时 触摸点距离左侧的偏移量
  11. this.x = e.targetTouches[0].clientX
  12. // 获取元素当前距离顶侧的偏移量
  13. this.t = this.offsetTop
  14. // 获取触摸开始时 触摸点距离顶侧的偏移量
  15. this.y = e.targetTouches[0].clientY
  16. // 使其不透明
  17. this.classList.remove('touming')
  18. // 清空定时器
  19. clearTimeout(this.timer)
  20. })
  21. xuanfu.addEventListener('touchmove', function (e) {
  22. // 获取触摸移动后,触摸点距离左侧的偏移
  23. this._x = e.targetTouches[0].clientX
  24. // 计算最终的left
  25. let newLeft = this._x - (this.x - this.l);
  26. // console.log(newLeft)
  27. // 判断
  28. if (newLeft <= 0) newLeft = 0
  29. if (newLeft >= document.documentElement.clientWidth - this.offsetWidth) {
  30. newLeft = document.documentElement.clientWidth - this.offsetWidth
  31. }
  32. // 获取触摸移动后,触摸点距离左侧的偏移
  33. this._y = e.targetTouches[0].clientY
  34. // 计算最终的left
  35. let newTop = this._y - (this.y - this.t);
  36. // console.log(newTop)
  37. // 判断
  38. if (newTop <= 0) newTop = 0
  39. if (newTop >= document.documentElement.clientWidth - this.offsetHeight) {
  40. newTop = document.documentElement.clientWidth - this.offsetHeight
  41. }
  42. // 设置样式
  43. this.style.left = newLeft + 'px'
  44. this.style.top = newTop + 'px'
  45. })
  46. xuanfu.addEventListener('touchend', function (e) {
  47. // 增加过渡效果
  48. this.style.transition = 'none'
  49. // 吸附效果
  50. // 计算居中状态下left值
  51. this.middle = (document.documentElement.clientWidth - this.offsetWidth) / 2
  52. // console.log(middle)
  53. //计算
  54. this.left = this.offsetLeft
  55. // console.log(left)
  56. // 声明变量 存放最终的left值
  57. let newLeft
  58. //判断
  59. if (this.left <= this.middle) {
  60. newLeft = 0
  61. } else {
  62. newLeft = document.documentElement.clientWidth - this.offsetWidth
  63. }
  64. // 设置
  65. this.style.left = newLeft + 'px'
  66. // 启动定时器,使其变成透明的
  67. this.timer = setTimeout(() => {
  68. this.classList.add('touming')
  69. }, 2000)
  70. })
  71. boxButton.addEventListener('click', function (e) {
  72. console.log(e)
  73. // console.log(this.Left)
  74. let res = (document.documentElement.clientWidth) / 2
  75. console.log(res)
  76. if (e.clientX <= res) {
  77. this.classList.add('boxButton2')
  78. } else {
  79. this.classList.remove('boxButton2')
  80. this.classList.add('boxButton1')
  81. }
  82. })

 

 

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

闽ICP备14008679号