微信小程序圖標的角標實現(xiàn)代碼
微信小程序圖標的角標實現(xiàn)
先上效果圖
實現(xiàn)原理就是一個view控件設置成類似于圖中的樣式,然后再固定在圖標的右上方,原理比較簡單, 下面貼上需要的代碼。
wxml:
<form bindsubmit="這是圖標點擊事件" report-submit="true"> <button class="iconBtn" form-type="submit"> <view class="remind-num" "> 12 </view> <image class="icon-img" src="{{這是圖標圖片的url}}"> </image> <view class="icon-text-view">這是圖標</view> </button> </form>
wcss:
iconBtn{ width: 100%; height: 130rpx; background: rgba(155, 155, 155, 0); border: none; display: flex; justify-content: center; align-items: center; flex-direction: column; } .iconBtn::after{ border: none; } .remind-num{ border-radius: 40%; position: absolute; top: 2rpx; right: 2rpx; font-size: 20rpx; /*padding: 4rpx 15rpx;*/ /*align-items: center;*/ text-align: center; width: 50rpx; line-height: 30rpx; height: 30rpx; color: var(--white); z-index: 100; background-color: rgba(221, 81, 76, 0.77); } .icon-img{ margin-top: 10rpx; width: 65rpx; height: 65rpx; }
補充:微信小程序:實現(xiàn)可拖動懸浮圖標(包括按鈕角標的實現(xiàn))
微信小程序:實現(xiàn)可拖動懸浮圖標(包括按鈕角標的實現(xiàn))
微信小程序:實現(xiàn)可拖動懸浮圖標
在制作商城類微信小程序的過程中,我們經(jīng)常會碰到需要增加可拖動懸浮圖標的情況,本文簡單的介紹一下可拖動懸浮按鈕的實現(xiàn)。
運行截圖:
主要代碼:
var startPoint Page({ data: { //按鈕位置參數(shù) buttonTop: 0, buttonLeft: 0, windowHeight: '', windowWidth: '', //角標顯示數(shù)字 corner_data:0, }, onLoad:function(){ //定義角標數(shù)字 this.setData({ corner_data:3 }) // 獲取購物車控件適配參數(shù) var that =this; wx.getSystemInfo({ success: function (res) { console.log(res); // 屏幕寬度、高度 console.log('height=' + res.windowHeight); console.log('width=' + res.windowWidth); // 高度,寬度 單位為px that.setData({ windowHeight: res.windowHeight, windowWidth: res.windowWidth, buttonTop:res.windowHeight*0.70,//這里定義按鈕的初始位置 buttonLeft:res.windowWidth*0.70,//這里定義按鈕的初始位置 }) } }) }, //可拖動懸浮按鈕點擊事件 btn_Suspension_click:function(){ //這里是點擊購物車之后將要執(zhí)行的操作 wx.showToast({ title: '點擊成功', icon:'success', duration:1000 }) }, //以下是按鈕拖動事件 buttonStart: function (e) { startPoint = e.touches[0]//獲取拖動開始點 }, buttonMove: function (e) { var endPoint = e.touches[e.touches.length - 1]//獲取拖動結束點 //計算在X軸上拖動的距離和在Y軸上拖動的距離 var translateX = endPoint.clientX - startPoint.clientX var translateY = endPoint.clientY - startPoint.clientY startPoint = endPoint//重置開始位置 var buttonTop = this.data.buttonTop + translateY var buttonLeft = this.data.buttonLeft + translateX //判斷是移動否超出屏幕 if (buttonLeft+50 >= this.data.windowWidth){ buttonLeft = this.data.windowWidth-50; } if (buttonLeft<=0){ buttonLeft=0; } if (buttonTop<=0){ buttonTop=0 } if (buttonTop + 50 >= this.data.windowHeight){ buttonTop = this.data.windowHeight-50; } this.setData({ buttonTop: buttonTop, buttonLeft: buttonLeft }) }, buttonEnd: function (e) { } })
wxml:
<!--可拖動按鈕控件表--> <!--buttonStart和buttonEnd一定不能用catch事件,否則按鈕點擊事件會失效--> <view class="btn_Suspension" bindtap="btn_Suspension_click" catchtouchmove="buttonMove" bindtouchstart="buttonStart" bindtouchend="buttonEnd" style="top:{{buttonTop}}px;left:{{buttonLeft}}px;"> <image class="Suspension_logo" src="../images/Suspension.png"></image><!--這里是按鈕圖標,下載地址會在文章底部說明--> <view wx:if="{{corner_data==0}}"></view> <view wx:else> <view class="cornorMark"> <text>{{corner_data}}</text> </view> </view> </view>
wxss:
Page{ background: #f5f5f5; } /**可拖動懸浮按鈕樣式表**/ .btn_Suspension{ position: fixed; height: 100rpx; width: 100rpx; background-color: rgba(255, 255, 255, 0.755); border-radius: 100%; display: flex; align-items: center; justify-content: center; z-index: 99999; box-shadow: 1px 0px 1px 1px #ede7e7; } .Suspension_logo{ position:absolute; height:50%; width:50%; left:23%; top:27% } .cornorMark{ position:absolute; background: rgb(242, 109, 38); border:1px solid rgb(242, 109, 38); border-radius: 100px; width:30rpx; height:30rpx; top:-17rpx; right:3rpx; color:#fff; font-size: 12px; text-align: center; } .cornorMark text{ position:absolute; width:100%; left:0%; text-align: center; top:-1px; }
圖標素材下載地址:
Iconfont阿里巴巴矢量圖標庫:https://www.iconfont.cn/
到此這篇關于微信小程序圖標的角標實現(xiàn)的文章就介紹到這了,更多相關小程序角標內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
extjs中form與grid交互數(shù)據(jù)(record)的方法
這篇文章介紹了extjs中form與grid交互數(shù)據(jù)(record)的方法,有需要的朋友可以參考一下2013-08-08JS使用reduce()方法處理樹形結構數(shù)據(jù)
這篇文章主要介紹了JS使用reduce()方法處理樹形結構數(shù)據(jù),對樹形結構數(shù)據(jù)感興趣的同學,可以參考下2021-05-05詳解小程序開發(fā)經(jīng)驗:多頁面數(shù)據(jù)同步
這篇文章主要介紹了小程序開發(fā)經(jīng)驗:多頁面數(shù)據(jù)同步,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05詳解Bootstrap創(chuàng)建表單的三種格式(一)
在本章中,我們將學習如何使用 Bootstrap 創(chuàng)建表單。Bootstrap 通過一些簡單的 HTML 標簽和擴展的類即可創(chuàng)建出不同樣式的表單,對bootstrap 表單相關知識感興趣的朋友一起學習吧2016-01-01