微信小程序懸浮框實現


最近在公司負責微信小程序,小程序相比html,JavaScript更加簡單。很多接口直接就給了,所以我們直接利用就好了。

下面說正題:

微信小程序懸浮框實現

效果圖如下:

      做了一個隨時撥打客服電話的懸浮框

1.第一種

目錄結構如下:


index.js

 1 Component({
 2   properties: {
 3     // 這里定義了innerText屬性,屬性值可以在組件使用時指定
 4     top: {
 5       type: String,
 6       value: '40%',
 7     },
 8     left: {
 9       type: String,
10       value: '89%',
11     }
12   },
13   data: {
14     // 這里是一些組件內部數據
15     // top: '80%',
16     // left: '89%',
17   },
18   lifetimes: {
19     // 生命周期函數,可以為函數,或一個在methods段中定義的方法名
20     attached: function() {
21       var res = wx.getSystemInfoSync();
22       this.setData({
23         systemInfo: res,
24       })
25     },
26     moved: function() {},
27     detached: function() {},
28   },
29 
30   methods: {
31     // 這里是一個自定義方法
32     //拖動不超過規定范圍
33     /**
34      * 計算拖動的位置
35      */
36     setTouchMove: function(e) {
37       var top = e.touches[0].clientY;
38       var left = e.touches[0].clientX;
39       var systemInfo = this.data.systemInfo;
40       var maxTop = systemInfo.windowHeight * 0.78;
41       var maxLeft = systemInfo.windowWidth * 0.89;
42       if(top < 0){
43         top = 0;
44       }
45       if (top > maxTop ){
46         top = maxTop;
47       }
48       if(left < 0){
49         left = 0;
50       }
51       if (left > maxLeft){
52         left = maxLeft;
53       }
54       this.triggerEvent('myevent', {
55         top: top + "px",
56         left: left + "px"
57       });
58     },
59     setCoordinates: function() {
60       var left = Number(this.data.left.replace("px",""));
61       if (left > (this.data.systemInfo.windowWidth / 2)) {
62         left = this.data.systemInfo.windowWidth*0.89;
63       } else {
64         left = 0;
65       }
66       this.triggerEvent('myevent', {
67         top: this.data.top,
68         left: left + "px",
69       });
70     },
71     /**
72      * 撥打客服電話
73      */
74     makephone: function() {
75       wx.makePhoneCall({
76         phoneNumber: "043184863311",
77         success: function() {
78           console.log("撥打電話成功")
79         }
80       })
81     }
82   }
83 })

index.json

{
  "component": true
}

index.wxml

<view class="firstView"  catchtouchmove="setTouchMove" catchtouchend='setCoordinates' style="left: {{left||'89%'}};top: {{top||'55%'}};" >
  <view>
    <image class='kehuImg' bindtap='makephone' src="/images/kefu.png"></image>
  </view>
</view>

index.wxss

.firstView {
  position: absolute;
  height: 70rpx;
  width: 70rpx;
  padding: 10rpx;
  background-color: #35d75d;
  border-radius: 50%;
}
.kehuImg {
  height: 70rpx;
  width: 70rpx;
}

現在寫完了自定義組件,接下來那個頁面需要就在該頁面引入

引入代碼:

 <!-- 以下是對一個自定義組件的引用  -->
<view>
  <component-tag-name top="{{component.top}}" left="{{component.left}}" bindmyevent='onMyEvent'></component-tag-name>
</view>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM