国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Vue實(shí)現(xiàn)仿iPhone懸浮球的示例代碼

瀏覽:84日期:2023-01-31 13:18:10

懸浮球插件簡(jiǎn)單的效果圖:

很遺憾,沒(méi)找到太好的視頻轉(zhuǎn)gif的軟件,壓縮出來(lái)的大小超過(guò)了限制,就不放圖了

可以參考其他人的圖,效果一致:

Vue實(shí)現(xiàn)仿iPhone懸浮球的示例代碼

簡(jiǎn)單實(shí)用案例:

<!-- 給定一個(gè)初始位置position,插槽中填寫想滑動(dòng)的部分 --><xuanfuqiu :position='position'><d-add-button @click='addPigFarm' add-item='豬場(chǎng)'></d-add-button></xuanfuqiu>

原理示意圖

請(qǐng)結(jié)合代碼注釋來(lái)理解

Vue實(shí)現(xiàn)仿iPhone懸浮球的示例代碼

懸浮球插件代碼如下:

<template> <div> <div : @mousedown='down' @touchstart='down' @mousemove='move' @touchmove='move' @mouseup='end' @touchend='end'> <slot></slot> </div> </div></template><script>export default { name: '', components: {}, props: { // 通過(guò)position來(lái)設(shè)置初始定位 position: { type: Object, default: function() {return { top: '32.25rem', left: '18.34375rem'} } }, // 通過(guò)fixed來(lái)禁用自由移動(dòng) fixed: { type: Boolean, default: false } }, data() { return { flags: false, positionTemp: { x: 0, y: 0 }, // 記錄手指點(diǎn)擊的位置 nx: ’’, ny: ’’, dx: ’’, dy: ’’, xPum: ’’, yPum: ’’, } }, watch: {}, computed: {}, methods: { // 實(shí)現(xiàn)移動(dòng)端拖拽 down(){ if (this.fixed) {return } this.flags = true; var touch; // 該if判斷是用touch還是mouse來(lái)移動(dòng) if (event.touches) {touch = event.touches[0]; } else {touch = event; } this.positionTemp.x = touch.clientX; // 手指點(diǎn)擊后的位置 this.positionTemp.y = touch.clientY; this.dx = moveDiv.offsetLeft; // 移動(dòng)的div元素的位置 this.dy = moveDiv.offsetTop; // console.log('moveDiv.offsetLeft', moveDiv.offsetLeft) // console.log('touch.clientX', touch.clientX) }, move(){ if(this.flags) {var touch ;if(event.touches){ touch = event.touches[0];}else { touch = event;}this.nx = touch.clientX - this.positionTemp.x; // 手指移動(dòng)的變化量this.ny = touch.clientY - this.positionTemp.y;this.xPum = this.dx + this.nx; // 移動(dòng)后,div元素的位置this.yPum = this.dy + this.ny;let windowWidth = document.documentElement.clientWidthlet windowHeight = document.documentElement.clientHeight// console.log('window.clientWidth', windowWidth)// console.log(this.xPum)// console.log(' moveDiv.clientWidth', moveDiv.clientWidth)if (this.xPum > 0 && (this.xPum + moveDiv.clientWidth < windowWidth)) {// movediv的左右邊,未出界 moveDiv.style.left = this.xPum + 'px';} else if (this.xPum <= 0) { // 左邊出界,則左邊緣貼邊 moveDiv.style.left = 0 + 'px';} else if (this.xPum + moveDiv.clientWidth >= windowWidth) { // 右邊緣出界 moveDiv.style.left = (windowWidth - moveDiv.clientWidth) + 'px'; // console.log('dx', windowWidth - moveDiv.clientWidth)}// 上下未出界if (this.yPum > 0 && (this.yPum + moveDiv.clientHeight < windowHeight)) { moveDiv.style.top = this.yPum +'px';} else if (this.yPum <= 0) { // 上邊緣出界 moveDiv.style.top = 0 + 'px'} else if (this.yPum + moveDiv.clientHeight >= windowHeight) { // 下邊緣 // console.log('windowHeight:', windowHeight) // console.log('moveDiv.clientHeight:', moveDiv.clientHeight) // console.log(this.yPum + moveDiv.clientHeight) moveDiv.style.top = windowHeight - moveDiv.clientHeight + 'px'}// 阻止頁(yè)面的滑動(dòng)默認(rèn)事件,為了只讓懸浮球滑動(dòng),其他部分不滑動(dòng);如果碰到滑動(dòng)問(wèn)題,1.2 請(qǐng)注意是否獲取到 touchmove, 系統(tǒng)默認(rèn)passive: true,無(wú)法使用preventDefault// document.addEventListener('touchmove', function(){// event.preventDefault();// }, { passive: false });// document.addEventListener('mousemove', function(){// event.preventDefault();// }, { passive: false });document.addEventListener('touchmove', this.preventDefault, { passive: false })document.addEventListener('mousemove', this.preventDefault, { passive: false }) } }, //鼠標(biāo)釋放時(shí)候的函數(shù),鼠標(biāo)釋放,移除之前添加的偵聽事件,將passive設(shè)置為true,不然背景會(huì)滑動(dòng)不了 end(){ this.flags = false // 注意事項(xiàng),在添加和刪除監(jiān)聽事件時(shí),其function必須是同名的函數(shù),不能為匿名函數(shù)。 document.removeEventListener(’touchmove’,this.preventDefault, false) document.removeEventListener(’mousemove’,this.preventDefault, false) // 下面兩句是保證在移除監(jiān)聽事件后,除了懸浮球的部分還能夠滑動(dòng),如果不添加,則無(wú)法滑動(dòng) document.addEventListener('touchmove', function(e) {window.event.returnValue = true }) document.addEventListener('mousemove', function(e) {window.event.returnValue = true }) }, preventDefault(e) { e.preventDefault() } }, created() {}, mounted() {}}</script><style lang='scss' scoped>.xuanfu { /* 如果碰到滑動(dòng)問(wèn)題,1.3 請(qǐng)檢查 z-index。z-index需比web大一級(jí)*/ z-index: 999; position: fixed; // 這里的定位方式有待考量,fixed的話存在未知設(shè)置不合理,跑出屏幕不顯示的問(wèn)題}</style>

到此這篇關(guān)于Vue實(shí)現(xiàn)仿iPhone懸浮球的示例代碼的文章就介紹到這了,更多相關(guān)Vue 懸浮球內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 99精品国产在现线免费 | 真人一级毛片国产 | 国产美女精品三级在线观看 | 男女视频免费观看 | 国产亚洲高清视频 | 日本三级免费网站 | 国产成人在线视频 | 福利姬在线精品观看 | 99精品高清视频一区二区 | 香港三级日本三级三级人妇 | 亚洲精选在线观看 | 成人性毛片 | 国产精品1区2区3区在线播放 | 男女乱淫真视频免费观看 | 色熟 | 亚洲欧美视频一区 | 久久精品国产99久久6动漫欧 | 国产在线精品观看 | www.夜色.com | 久久九九色 | 黄色上床网站 | 很黄的网站在线观看 | 欧美国产亚洲一区 | 天天干夜夜怕 | 久久综合久美利坚合众国 | 欧美在线播放成人a | 美女视频一区二区三区在线 | 毛片视频在线免费观看 | 午夜专区 | 最新最好看免费毛片基地 | 欧美日韩精品一区二区三区视频 | 夜色伊人 | 欧美成人a级在线视频 | 国产欧美曰韩一区二区三区 | 看真人一级毛片 | 久久久久久毛片免费观看 | a国产成人免费视频 | 黑人边吃奶边扎下面激情视频 | 久久九九免费视频 | 欧美成人手机视频免费播放 | 久久久久久久网站 |