h5_push.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <template>
  2. <view>
  3. <transition name="slide-fade">
  4. <view class="h5_push" @touchstart.stop.prevent="touchstart" @touchmove.stop.prevent="touchmove" @touchend.stop.prevent="touchend" v-if="show" :style="style">
  5. <view class="push-title">
  6. <view class="push-type">
  7. <image src="/static/push/message-icon.png"></image>
  8. {{messageType}}
  9. </view>
  10. {{messageTime}}
  11. </view>
  12. <view class="push-body">
  13. <view class="push-content">
  14. <view class="push-content-title">{{messageTitle}}</view>
  15. <view class="push-content-text">{{messageContent}}</view>
  16. </view>
  17. <image :src="messageImage" class="push-img" mode="aspectFill"></image>
  18. </view>
  19. </view>
  20. </transition>
  21. </view>
  22. </template>
  23. <script>
  24. export default{
  25. data() {
  26. return {
  27. show: false,
  28. // 关闭时间
  29. closeTime: 3000,
  30. // app内横幅提醒
  31. inApp: false,
  32. // 声音提醒
  33. voice: true,
  34. // 振动提醒
  35. vibration: false,
  36. // 消息分类
  37. messageType: '',
  38. // 通知标题
  39. messageTitle: '',
  40. // 时间
  41. messageTime: '现在',
  42. // 通知文案
  43. messageContent: '',
  44. // 缩略图
  45. messageImage: '',
  46. top: 20,
  47. left: 20,
  48. cur: {
  49. x: 0,
  50. y: 0,
  51. pageX: 0,
  52. pageY: 0
  53. }
  54. }
  55. },
  56. computed:{
  57. style() {
  58. let system = uni.getSystemInfoSync()
  59. let statusBarHeight = system.statusBarHeight
  60. return `top: calc(${statusBarHeight}px + ${this.top}rpx);left: ${this.left}rpx`
  61. }
  62. },
  63. created() {
  64. setTimeout(() => {
  65. this.show = false
  66. }, this.closeTime)
  67. },
  68. methods:{
  69. touchstart(event) {
  70. console.log(event)
  71. var touch;
  72. if (event.touches) {
  73. touch = event.touches[0];
  74. } else {
  75. touch = event;
  76. }
  77. this.cur.x = touch.clientX;
  78. this.cur.y = touch.clientY;
  79. this.cur.pageX = touch.pageX;
  80. this.cur.pageY = touch.pageY;
  81. console.log(this.cur)
  82. },
  83. touchmove(event) {
  84. var touch;
  85. if (event.touches) {
  86. touch = event.touches[0];
  87. } else {
  88. touch = event;
  89. }
  90. let moveX = touch.pageX - this.cur.x;
  91. let moveY = touch.pageY - this.cur.y;
  92. let x = moveX;
  93. let y = moveY;
  94. // console.log(x, y)
  95. // console.log(this.cur, touch)
  96. let system = uni.getSystemInfoSync()
  97. if (y >= (uni.upx2px(20) + system.statusBarHeight)) {
  98. y = (uni.upx2px(20) + system.statusBarHeight);
  99. }
  100. this.top = y / (uni.upx2px(y) / y)
  101. this.left = x / (uni.upx2px(x) / x)
  102. // this.body.setStyle({
  103. // top: y + 'px',
  104. // left: x + 'px'
  105. // });
  106. },
  107. touchend(event) {
  108. console.log(event)
  109. var touch;
  110. if (event.touches.length) {
  111. touch = event.touches[0];
  112. } else {
  113. touch = event.changedTouches[0];
  114. }
  115. console.log(this.cur, touch)
  116. let differX = Math.abs(this.cur.pageX) - Math.abs(touch.pageX)
  117. let differY = Math.abs(this.cur.pageY) - Math.abs(touch.pageY)
  118. // console.log(differX, differY)
  119. if(Math.abs(differX) > 5 || Math.abs(differY) > 5) { // 上下移动或左右移动超过5px则关闭弹窗
  120. this.show = false
  121. } else { // 否则当作单击事件
  122. console.log('-------------------')
  123. this.show = false
  124. }
  125. }
  126. }
  127. }
  128. </script>
  129. <style lang="scss" scoped>
  130. .h5_push{
  131. width: 710rpx;
  132. height: 192rpx;
  133. background: #FFFFFF;
  134. box-shadow: 0px 3rpx 18rpx 0px rgba(54, 58, 68, 0.08);
  135. border-radius: 20rpx;
  136. position: fixed;
  137. z-index: 9999999;
  138. .push-title{
  139. padding: 30rpx 30rpx 15rpx;
  140. display: flex;
  141. align-items: center;
  142. justify-content: space-between;
  143. font-size: 24rpx;
  144. font-weight: 400;
  145. color: #4F555B;
  146. .push-type{
  147. display: flex;
  148. align-items: center;
  149. font-size: 24rpx;
  150. font-weight: 400;
  151. color: #4F555B;
  152. image{
  153. width: 24rpx;
  154. height: 24rpx;
  155. margin-right: 10rpx;
  156. }
  157. }
  158. }
  159. .push-body{
  160. display: flex;
  161. align-items: center;
  162. justify-content: space-between;
  163. padding: 0 24rpx 0 30rpx;
  164. .push-content{
  165. width: calc(100% - 150rpx);
  166. .push-content-title{
  167. font-size: 30rpx;
  168. font-weight: 500;
  169. color: #202123;
  170. margin-bottom: 20rpx;
  171. overflow: hidden;
  172. text-overflow: ellipsis;
  173. white-space: nowrap;
  174. }
  175. .push-content-text{
  176. font-size: 24rpx;
  177. font-weight: 400;
  178. color: #4F555B;
  179. overflow: hidden;
  180. text-overflow: ellipsis;
  181. white-space: nowrap;
  182. }
  183. }
  184. .push-img{
  185. width: 100rpx;
  186. height: 100rpx;
  187. background: #F2F2F3;
  188. border: 0.5px solid #E9E9E9;
  189. border-radius: 10rpx;
  190. margin-left: 50rpx;
  191. }
  192. }
  193. }
  194. .slide-fade-enter-active {
  195. transition: all .3s ease;
  196. }
  197. .slide-fade-leave-active {
  198. transition: all .3s cubic-bezier(1.0, 0.5, 0.8, 1.0);
  199. }
  200. .slide-fade-enter, .slide-fade-leave-to
  201. /* .slide-fade-leave-active for below version 2.1.8 */ {
  202. transform: translateX(10px);
  203. opacity: 0;
  204. }
  205. </style>