微信小程序?qū)崿F(xiàn)拖拽 image 觸摸事件監(jiān)聽的實(shí)例
微信小程序?qū)崿F(xiàn)拖拽 image 觸摸事件監(jiān)聽的實(shí)例
需要做個(gè)浮在scroll-view之上的button.嘗試了一下.
實(shí)現(xiàn)效果圖:
Android中也會有類似移動控件的操作.思路差不多.獲取到位移的X Y 的變量,給控件設(shè)置坐標(biāo).
1.index.wxml
<image class="image-style" src="../../images/gundong.png" bindtap="ballClickEvent" style="bottom:{{ballBottom}}px;right:{{ballRight}}px;" bindtouchmove="ballMoveEvent"> </image>
簡單的設(shè)置一張圖片,添加觸摸事件監(jiān)聽.點(diǎn)擊事件監(jiān)聽.根據(jù)觸摸事件獲取X Y位移,設(shè)置為image的位置
2.index.js
//index.js //獲取應(yīng)用實(shí)例 var app = getApp() Page({ data: { ballBottom: 240, ballRight: 120, screenHeight: 0, screenWidth: 0, }, onLoad: function () { //獲取屏幕寬高 var _this = this; wx.getSystemInfo({ success: function (res) { _this.setData({ screenHeight: res.windowHeight, screenWidth: res.windowWidth, }); } }); }, ballMoveEvent: function (e) { console.log('我被拖動了....') var touchs = e.touches[0]; var pageX = touchs.pageX; var pageY = touchs.pageY; console.log('pageX: ' + pageX) console.log('pageY: ' + pageY) //防止坐標(biāo)越界,view寬高的一般 if (pageX < 30) return; if (pageX > this.data.screenWidth - 30) return; if (this.data.screenHeight - pageY <= 30) return; if (pageY <= 30) return; //這里用right和bottom.所以需要將pageX pageY轉(zhuǎn)換 var x = this.data.screenWidth - pageX - 30; var y = this.data.screenHeight - pageY - 30; console.log('x: ' + x) console.log('y: ' + y) this.setData({ ballBottom: y, ballRight: x }); }, //點(diǎn)擊事件 ballClickEvent: function () { console.log('點(diǎn)擊了....') } })
3.index.wxss
這里需要設(shè)置z-index
.image-style{ position: absolute; bottom: 240px; right: 100px; width: 60px; height: 60px; z-index: 100; }
如有疑問請留言或者到本站社區(qū)交流討論,本站關(guān)于小程序的文章還有很多,希望大家搜索查閱,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Javascript調(diào)用XML制作連動下拉列表框
Javascript調(diào)用XML制作連動下拉列表框...2006-06-06javascript實(shí)現(xiàn)字典Dictionary示例基礎(chǔ)
這篇文章主要為大家介紹了javascript實(shí)現(xiàn)字典Dictionary基礎(chǔ)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08JS實(shí)現(xiàn)刷新網(wǎng)頁后之前瀏覽位置保持不變示例詳解
這篇文章主要為大家介紹了JS實(shí)現(xiàn)刷新網(wǎng)頁后之前瀏覽位置保持不變示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08微信小程序 出現(xiàn)47001 data format error原因解決辦法
這篇文章主要介紹了微信小程序 出現(xiàn)47001 data format error原因解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03javascript數(shù)據(jù)類型之原始類型詳解
這篇文章主要為大家介紹了javascript數(shù)據(jù)類型之原始類型詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06微信小程序中頁面FOR循環(huán)和嵌套循環(huán)
這篇文章主要介紹了微信小程序中頁面FOR循環(huán)和嵌套循環(huán)的相關(guān)資料,需要的朋友可以參考下2017-06-06