floatButton.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // myComponents/floatButton/floatButton.js
  2. const App = getApp()
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. count:{
  9. type:Number,
  10. value:0
  11. },
  12. icon:{
  13. type:String,
  14. value:'myIcon14'
  15. },
  16. position:{
  17. type:String,
  18. value:'left'
  19. },
  20. max:{
  21. type:Number,
  22. value:99
  23. }
  24. },
  25. /**
  26. * 组件的初始数据
  27. */
  28. data: {
  29. top:450,
  30. left:0,
  31. windowHeight:App.globalData.windowHeight,
  32. windowWidth:App.globalData.windowWidth,
  33. startX: 0, // 触摸开始时的 X 坐标
  34. startY: 0, // 触摸开始时的 Y 坐标
  35. isDragging: false, // 是否正在拖动
  36. },
  37. /**
  38. * 组件的方法列表
  39. */
  40. methods: {
  41. //改变位置
  42. changePosition(){
  43. var left = 0
  44. if(this.properties.position == 'left'){
  45. left = 0
  46. }else{
  47. left = parseInt(this.data.windowWidth) - 100
  48. }
  49. this.setData(
  50. {
  51. left:left
  52. }
  53. )
  54. },
  55. onButtonTouchStart: function(e) {
  56. this.setData({
  57. startX: e.touches[0].clientX,
  58. startY: e.touches[0].clientY,
  59. isDragging: true,
  60. });
  61. },
  62. onButtonMove: function(e) {
  63. if (!this.data.isDragging) return;
  64. // const deltaX = e.touches[0].clientX - this.data.startX;
  65. const deltaY = e.touches[0].clientY - this.data.startY;
  66. // let newLeft;
  67. let newTop;
  68. if(this.data.buttonStyle){
  69. // newLeft = parseInt(this.data.left) + deltaX;
  70. newTop = parseInt(this.data.top) + deltaY;
  71. }else{
  72. // newLeft = this.data.startX;
  73. newTop = this.data.startY;
  74. }
  75. newTop = newTop<50?50:newTop
  76. newTop = newTop>this.data.windowHeight?this.data.windowHeight:newTop
  77. // console.log(newLeft,newTop)
  78. this.setData({
  79. // buttonStyle: `left: ${newleft}px; top: ${newTop}px;`,
  80. top:newTop,
  81. // left:newLeft,
  82. startX: e.touches[0].clientX,
  83. startY: e.touches[0].clientY,
  84. });
  85. },
  86. onButtonTouchEnd: function() {
  87. this.setData({
  88. isDragging: false,
  89. });
  90. },
  91. click(e){
  92. this.triggerEvent('click')
  93. }
  94. },
  95. lifetimes: {
  96. // 在组件实例进入页面节点树时执行
  97. attached: function () {
  98. // 初始化操作
  99. // console.log('组件初始化');
  100. this.changePosition()
  101. // ...
  102. },
  103. // 在组件实例被移除出页面节点树时执行
  104. detached: function () {
  105. // 清理工作
  106. console.log('组件销毁');
  107. // ...
  108. },
  109. // ...
  110. }
  111. })