myGuide.vue 11 KB

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