myGuide.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <template>
  2. <view class="guide" v-if="showGuide" @touchmove.stop.prevent>
  3. <view :style="guideStyle" class="guide-box">
  4. <template v-if="guideInfo&&guideInfo.el">
  5. <view class="tips guide-step-tips" id="guidetips" :style="tipPosition">
  6. <!-- tips content -->
  7. <view class="guide1">
  8. <view class="guide1-title">
  9. <image class="qinghe_icon" src="@/static/images/guide/qinghe_icon.png" mode="aspectFill"></image>
  10. <text>欢迎使用芳华未来APP</text>
  11. </view>
  12. <view>{{guideInfo.tips}}</view>
  13. </view>
  14. <!-- tips footer -->
  15. <view class="tool-btn">
  16. <text @click="skip">跳过({{index+1}}/{{guideList.length}})</text>
  17. <view class="next" style="" @click="next">{{ guideInfo.next }}</view>
  18. </view>
  19. <view class="arrow" :style="arrowTop"></view>
  20. </view>
  21. </template>
  22. <view v-else class="tips" id="guidetips" :style="tipPosition" @click="next">
  23. <slot>
  24. <view class="guide2">
  25. <image class="vector" src="@/static/images/guide/Vector.png" mode="aspectFill"></image>
  26. <image class="Frame2613" src="@/static/images/guide/Frame2613.png" mode="aspectFill"></image>
  27. <view class="guide2-tips">
  28. {{guideInfo&&guideInfo.tips}}
  29. </view>
  30. <image class="vector vector-bottom" src="@/static/images/guide/Vector.png" mode="aspectFill"></image>
  31. </view>
  32. </slot>
  33. </view>
  34. </view>
  35. <!-- 遮罩层,防止点击 -->
  36. <view class="v-model" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd"></view>
  37. </view>
  38. </template>
  39. <script>
  40. export default {
  41. // name: "novice-guidance",
  42. props: {
  43. step: {
  44. type: Object,
  45. default: () => {
  46. return {}
  47. },
  48. }
  49. },
  50. data() {
  51. return {
  52. statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
  53. stepName: 'step', //该提示步骤的名称,用于不在重复展示
  54. guideList: [{
  55. el: '',
  56. tips: '',
  57. next: '',
  58. style: '',
  59. }],
  60. index: 0, // 当前展示的索引
  61. showGuide: true, // 是否显示引导
  62. guideStyle: '', // 默认样式
  63. arrowTop: '', //步骤提示三角形的定位
  64. tipPosition: '', //步骤提示的定位
  65. systemInfo: '', //屏幕宽度高度等信息
  66. tipWidth: 200 ,//步骤提示默认的宽度,
  67. startX: 0,
  68. startY: 0
  69. }
  70. },
  71. computed: {
  72. guideInfo() {
  73. return this.guideList[this.index];
  74. }
  75. },
  76. mounted() {
  77. this.init()
  78. },
  79. methods: {
  80. init() {
  81. this.guideList = this.step.guideList;
  82. this.stepName = this.step.name;
  83. const systemInfo = uni.getSystemInfoSync();
  84. this.systemInfo = systemInfo;
  85. const guide = uni.getStorageSync(this.stepName);
  86. if (!guide) {
  87. this.getDomInfo();
  88. } else {
  89. this.showGuide = false;
  90. }
  91. },
  92. // 展示新手提示
  93. viewTips(data, scrollTop,scrollHeight) {
  94. if (data) {
  95. // 如果dom宽度大于或者等于窗口宽度,需要重新调整dom展示宽度
  96. let newWidth = this.systemInfo.windowWidth - 20;
  97. if (data.width >= newWidth) {
  98. data.width = newWidth;
  99. }
  100. // 如果距离左边为0,自动增加一点左边距
  101. if (data.left == 0) {
  102. data.left = 10;
  103. }
  104. let domRW = this.systemInfo.windowWidth - data.left;
  105. let left = 0;
  106. // 如果dom距离右边没有tips的宽度大的话,就要让tips向左便宜
  107. if (domRW < this.tipWidth) {
  108. left = domRW - this.tipWidth - 30;
  109. }
  110. const index = this.index;
  111. // 步骤条展示的高度需要加上屏幕滚动的高度
  112. data.top += scrollTop;
  113. // 根据实际情况需要滚动到展示区域
  114. // uni.pageScrollTo({
  115. // scrollTop: data.top > 88 ? data.top - 100 : 0,
  116. // duration: 100,
  117. // })
  118. uni.pageScrollTo({
  119. scrollTop: data.top > 44 ? data.top - 44 - this.statusBarHeight : 0,
  120. duration: 100,
  121. })
  122. let obj = Object.assign(this.guideList[index], data);
  123. // 设置三角形高度
  124. let arrowTop = -6;
  125. let top = data.height + 20;
  126. //如果dom在屏幕底部的话,重新调整提示框和三角形的定位
  127. let newHeight = scrollHeight - data.top - data.height
  128. if(this.guideInfo && this.guideInfo.childEl) {
  129. const query = uni.createSelectorQuery().in(this.$parent);
  130. query.select(this.guideInfo.childEl).boundingClientRect((child) => {
  131. top = data.height + child.height + 20;
  132. newHeight = newHeight - child.height
  133. uni.createSelectorQuery().in(this).select('#guidetips').boundingClientRect(res => {
  134. if (newHeight > (res.height + 5)) {
  135. this.arrowTop = "top:" + arrowTop + 'px;';
  136. // 设置提示框定位
  137. this.tipPosition = "top:" + top + 'px;left:' + left + "px;";
  138. } else {
  139. this.arrowTop = 'top: auto;bottom: -6px;';
  140. // 设置提示框定位
  141. this.tipPosition = 'top: -14px;left:' + left +
  142. "px;transform: translateY(-100%);";
  143. }
  144. }).exec();
  145. // 重新设置guideList的值
  146. this.guideList.splice(index, 1, obj);
  147. this.guideStyle = this.getStyle(child.height);
  148. }).exec();
  149. } else {
  150. this.$nextTick(()=>{
  151. uni.createSelectorQuery().in(this).select('#guidetips').boundingClientRect(res => {
  152. if (newHeight > (res.height + 5)) {
  153. this.arrowTop = "top:" + arrowTop + 'px;';
  154. // 设置提示框定位
  155. this.tipPosition = "top:" + top + 'px;left:' + left + "px;";
  156. } else {
  157. this.arrowTop = 'top: auto;bottom: -6px;';
  158. // 设置提示框定位
  159. this.tipPosition = 'top: -14px;left:' + left +
  160. "px;transform: translateY(-100%);";
  161. }
  162. }).exec();
  163. })
  164. // 重新设置guideList的值
  165. this.guideList.splice(index, 1, obj);
  166. this.guideStyle = this.getStyle();
  167. }
  168. } else {
  169. this.index += 1;
  170. this.getDomInfo();
  171. }
  172. },
  173. // 获取步骤提示的主要样式
  174. getStyle(childHeight) {
  175. const {
  176. width,
  177. height,
  178. left,
  179. top,
  180. style = 'border-radius: 12rpx;margin: 0'
  181. } = this.guideInfo;
  182. const relHeight = height + Number(childHeight || 0)
  183. let newstyle = "width:" + width + "px;";
  184. newstyle += "height:" + relHeight + "px;";
  185. newstyle += "left:" + left + "px;";
  186. newstyle += "top:" + top + "px;";
  187. newstyle += "box-shadow: rgb(33 33 33 / 80%) 0px 0px 0px 0px, rgb(33 33 33 / 50%) 0px 0px 0px 5000px;";
  188. newstyle += style;
  189. return newstyle;
  190. },
  191. // 获取dom信息
  192. getDomInfo() {
  193. const {
  194. el
  195. } = this.guideInfo;
  196. if(el) {
  197. const query = uni.createSelectorQuery().in(this.$parent);
  198. this.$nextTick(()=> {
  199. query.select(el).boundingClientRect()
  200. query.selectViewport().scrollOffset()
  201. var _this = this;
  202. query.exec(function(res) {
  203. // console.log('打印dom的元素的信息', res);
  204. let data = res[0] // #the-id节点的上边界坐标
  205. let scrollTop = res[1].scrollTop // 显示区域的竖直滚动位置
  206. _this.viewTips(data, scrollTop,res[1].scrollHeight)
  207. })
  208. });
  209. } else {
  210. // 没有节点信息
  211. uni.pageScrollTo({
  212. scrollTop: 0,
  213. duration: 100
  214. })
  215. this.arrowTop = "display:none";
  216. // 设置提示框定位
  217. this.tipPosition = "top:0;left:0;bottom:0;width: 100vw;"
  218. const index = this.index;
  219. let obj = Object.assign(this.guideList[index], {
  220. width: this.systemInfo.windowHeight,
  221. height: 0,
  222. left: 0,
  223. top: 0,
  224. });
  225. // 重新设置guideList的值
  226. this.guideList.splice(index, 1, obj);
  227. this.guideStyle = this.getStyle();
  228. }
  229. },
  230. // 跳过新手提示
  231. skip() {
  232. this.showGuide = false;
  233. uni.setStorageSync(this.stepName, true);
  234. uni.pageScrollTo({
  235. scrollTop: 0,
  236. duration: 100
  237. })
  238. },
  239. // 下一步
  240. next() {
  241. if (this.index === this.guideList.length - 1) {
  242. this.showGuide = false;
  243. uni.setStorageSync(this.stepName, true);
  244. uni.pageScrollTo({
  245. scrollTop: 0,
  246. duration: 100
  247. })
  248. } else {
  249. this.index += 1;
  250. this.getDomInfo();
  251. }
  252. },
  253. onTouchStart(event) {
  254. // 记录触摸开始的位置
  255. this.startX = event.touches[0].clientX;
  256. this.startY = event.touches[0].clientY;
  257. },
  258. onTouchMove(event) {
  259. // 获取当前触摸的位置
  260. // const currentX = event.touches[0].clientX;
  261. // const currentY = event.touches[0].clientY;
  262. // // 计算滑动的距离
  263. // const deltaX = currentX - this.startX;
  264. // const deltaY = currentY - this.startY;
  265. },
  266. onTouchEnd(event) {
  267. // 触摸结束,可以在这里做一些清理工作
  268. const deltaX = event.changedTouches[0].clientX;
  269. const deltaY = event.changedTouches[0].clientY;
  270. // 触发 swipeGuide 方法
  271. this.swipeGuide(deltaX, deltaY);
  272. },
  273. swipeGuide(deltaX, deltaY) {
  274. // console.log(`滑动的距离: deltaX = ${deltaX}, deltaY = ${deltaY}`);
  275. if(Math.abs(deltaY)>10){
  276. this.next();
  277. }
  278. }
  279. },
  280. }
  281. </script>
  282. <style lang="scss" scoped>
  283. @mixin u-flex($flexD, $alignI, $justifyC) {
  284. display: flex;
  285. flex-direction: $flexD;
  286. align-items: $alignI;
  287. justify-content: $justifyC;
  288. }
  289. .guide1 {
  290. font-family: PingFang SC, PingFang SC;
  291. font-weight: 400;
  292. font-size: 26rpx;
  293. color: #757575;
  294. .qinghe_icon {
  295. width: 48rpx;
  296. height: 48rpx;
  297. }
  298. .guide1-title {
  299. margin-top: 10rpx;
  300. margin-bottom: 20rpx;
  301. font-weight: 500;
  302. font-size: 32rpx;
  303. color: #222222;
  304. @include u-flex(row, flex-end, flex-start);
  305. }
  306. }
  307. .guide2 {
  308. width: 100%;
  309. margin-top: 40%;
  310. @include u-flex(column, center, center);
  311. .vector {
  312. width: 51rpx;
  313. height: 147rpx;
  314. }
  315. .Frame2613 {
  316. width: 112rpx;
  317. height: 112rpx;
  318. margin-top: 20rpx;
  319. }
  320. .vector-bottom {
  321. transform: rotate(180deg);
  322. }
  323. .guide2-tips {
  324. padding: 54rpx 44rpx 40rpx 44rpx;
  325. box-sizing: border-box;
  326. margin-top: -30rpx;
  327. margin-bottom: 36rpx;
  328. border-radius: 24rpx;
  329. text-align: center;
  330. font-family: PingFang SC, PingFang SC;
  331. font-weight: 400;
  332. font-size: 26rpx;
  333. color: #FFFFFF;
  334. background: linear-gradient(90deg, #FCAC34, #F9622F);
  335. }
  336. }
  337. .v-model {
  338. position: fixed;
  339. left: 0;
  340. top: 0;
  341. width: 100%;
  342. height: 100%;
  343. z-index: 1000;
  344. }
  345. .guide {
  346. z-index: 10001;
  347. .guide-box {
  348. position: absolute;
  349. z-index: 10001;
  350. // transition: all 0.2s;
  351. .arrow {
  352. width: 28rpx;
  353. height: 28rpx;
  354. background: #fff;
  355. position: absolute;
  356. top: 0;
  357. left: 48rpx;
  358. z-index: 10000;
  359. transform: rotate(45deg);
  360. }
  361. .guide-step-tips {
  362. width: 424rpx;
  363. padding: 0 24rpx 24rpx 24rpx;
  364. box-sizing: border-box;
  365. border-radius: 12rpx;
  366. background-color: #fff;
  367. font-family: PingFang SC, PingFang SC;
  368. font-weight: 400;
  369. font-size: 26rpx;
  370. color: #757575;
  371. position: relative;
  372. }
  373. .tips {
  374. position: absolute;
  375. top: 152rpx;
  376. left: -50%;
  377. z-index: 10001;
  378. .text {}
  379. .tool-btn {
  380. margin-top: 20rpx;
  381. @include u-flex(row, center, space-between);
  382. font-weight: 400;
  383. font-size: 26rpx;
  384. color: #757575;
  385. .next {
  386. width: 128rpx;
  387. height: 68rpx;
  388. line-height: 68rpx;
  389. text-align: center;
  390. background: #FF5C03;
  391. border-radius: 8rpx 8rpx 8rpx 8rpx;
  392. font-family: PingFang SC, PingFang SC;
  393. font-weight: 400;
  394. font-size: 26rpx;
  395. color: #FFFFFF;
  396. }
  397. }
  398. }
  399. }
  400. }
  401. </style>