add-btn-fixed 111.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <view class="slot" @touchmove="buttonMove" @touchstart="buttonStart" @touchend="buttonEnd" :style="{ top: buttonTop + 'px', left: buttonLeft + 'px' }">
  3. <view class="btn" @click="click"><u-icon name="plus" size="54"></u-icon></view>
  4. </view>
  5. </template>
  6. <script>
  7. var startPoint;
  8. export default {
  9. data() {
  10. return {
  11. buttonTop: 0,
  12. buttonLeft: 0,
  13. windowHeight: '',
  14. windowWidth: ''
  15. };
  16. },
  17. mounted() {
  18. let $this = this;
  19. uni.getSystemInfo({
  20. success(res) {
  21. // 高度,宽度 单位为px
  22. $this.windowHeight = res.windowHeight;
  23. $this.windowWidth = res.windowWidth;
  24. //这里定义按钮的初始位置
  25. $this.buttonTop = res.windowHeight * 0.8;
  26. //这里定义按钮的初始位置
  27. $this.buttonLeft = res.windowWidth * 0.8;
  28. }
  29. });
  30. },
  31. methods: {
  32. // 点击事件
  33. click() {
  34. this.$emit('click');
  35. },
  36. //以下是按钮拖动事件
  37. buttonStart: function(e) {
  38. //获取拖动开始点
  39. startPoint = e.touches[0];
  40. },
  41. buttonMove: function(e) {
  42. //获取拖动结束点
  43. var endPoint = e.touches[e.touches.length - 1];
  44. //计算在X轴上拖动的距离和在Y轴上拖动的距离
  45. var translateX = endPoint.clientX - startPoint.clientX;
  46. var translateY = endPoint.clientY - startPoint.clientY;
  47. startPoint = endPoint; //重置开始位置
  48. var buttonTop = this.buttonTop + translateY;
  49. var buttonLeft = this.buttonLeft + translateX;
  50. //判断是移动否超出屏幕
  51. if (buttonLeft + 50 >= this.windowWidth) {
  52. buttonLeft = this.windowWidth - 50;
  53. }
  54. if (buttonLeft <= 0) {
  55. buttonLeft = 0;
  56. }
  57. if (buttonTop <= 0) {
  58. buttonTop = 0;
  59. }
  60. if (buttonTop + 50 >= this.windowHeight) {
  61. buttonTop = this.windowHeight - 50;
  62. }
  63. this.buttonTop = buttonTop;
  64. this.buttonLeft = buttonLeft;
  65. },
  66. buttonEnd: function(e) {}
  67. }
  68. };
  69. </script>
  70. <style lang="scss" scoped>
  71. .slot {
  72. position: fixed;
  73. z-index: $app-zIndex-fixed;
  74. box-shadow: $app-theme-shadow;
  75. overflow: hidden;
  76. border-radius: 50%;
  77. .btn {
  78. background-color: $app-theme-color;
  79. display: flex;
  80. justify-content: center;
  81. align-items: center;
  82. width: 100rpx;
  83. height: 100rpx;
  84. border-radius: 50%;
  85. color: $app-theme-text-white-color;
  86. overflow: hidden;
  87. }
  88. }
  89. </style>