当前位置:网站首页>H5 模块悬浮拖动效果
H5 模块悬浮拖动效果
2022-07-05 06:23:00 【Heerey525】
问题:在移动端H5悬浮一个模块可能会出现遮挡到别的内容的情况,所以需要将这个悬浮的模块可拖拽。
方案:就是监听触摸事件,(绝对定位)改变模块的位置
预览需要浏览器在手机模式下
代码:
<template>
<div class="container">
<div class="floatBox" id="floatBox"></div>
</div>
</template>
<script>
export default {
name: 'touchMove',
data() {
return {
} },
mounted() {
this.touchMoveEvent()
},
methods: {
// 结束咨询按钮允许拖拽
touchMoveEvent() {
//获取元素
var div1 = document.getElementById('floatBox');
// var div1 = document.querySelector('#div1');
//限制最大宽高,不让滑块出去
var maxW = document.body.clientWidth - div1.offsetWidth;
var maxH = document.body.clientHeight - div1.offsetHeight;
var oL, oT;
//手指触摸开始,记录div的初始位置
div1.addEventListener('touchstart', function(e) {
var ev = e || window.event;
var touch = ev.targetTouches[0];
oL = touch.clientX - div1.offsetLeft;
oT = touch.clientY - div1.offsetTop;
document.addEventListener("touchmove", defaultEvent, false);
});
//触摸中的,位置记录
div1.addEventListener('touchmove', function(e) {
var ev = e || window.event;
var touch = ev.targetTouches[0];
var oLeft = touch.clientX - oL;
var oTop = touch.clientY - oT;
if(oLeft < 0) {
oLeft = 0;
} else if(oLeft >= maxW) {
oLeft = maxW;
}
if(oTop < 75) {
oTop = 75;
} else if(oTop >= maxH - 75) {
oTop = maxH - 75;
}
div1.style.left = oLeft + 'px';
div1.style.top = oTop + 'px';
});
//触摸结束时的处理
div1.addEventListener('touchend', function() {
document.removeEventListener("touchmove", defaultEvent);
});
//阻止默认事件
function defaultEvent(e) {
e.preventDefault();
}
}
}
}
</script>
<style>
.container {
width: 100vw;
height: 100vh;
background: black
}
.floatBox {
width: 100%;
position: fixed;
bottom: 140px;
right: 2%;
width: 80px;
height: 80px;
z-index: 11;
touch-action: none;
background: red;
}
</style>
边栏推荐
- 高斯消元 AcWing 884. 高斯消元解异或线性方程组
- [moviepy] unable to find a solution for exe
- Simple selection sort of selection sort
- Ffmpeg build download (including old version)
- Records of some tools 2022
- MySQL advanced part 1: index
- 【LeetCode】Day94-重塑矩阵
- [2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields
- 11-gorm-v2-03-basic query
- Game theory acwing 893 Set Nim game
猜你喜欢
MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
高斯消元 AcWing 884. 高斯消元解异或线性方程组
WordPress switches the page, and the domain name changes back to the IP address
Find the combination number acwing 889 01 sequence meeting conditions
4. Object mapping Mapster
Inclusion exclusion principle acwing 890 Divisible number
Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
[wustctf2020] plain_ WP
Game theory acwing 891 Nim games
Leetcode-6111: spiral matrix IV
随机推荐
[leetcode] day94 reshape matrix
博弈论 AcWing 894. 拆分-Nim游戏
2022-5-第四周日报
1.手动创建Oracle数据库
Leetcode-6108: decrypt messages
Nested method, calculation attribute is not applicable, use methods
How to set the drop-down arrow in the spinner- How to set dropdown arrow in spinner?
LeetCode 1200. Minimum absolute difference
【LeetCode】Easy | 20. Valid parentheses
Quickly use Amazon memorydb and build your own redis memory database
our solution
LeetCode-61
Chapter 6 relational database theory
P2575 master fight
3. Oracle control file management
Inclusion exclusion principle acwing 890 Divisible number
Records of some tools 2022
Find the combination number acwing 888 Find the combination number IV
SQL三种连接:内连接、外连接、交叉连接
Leetcode-556: the next larger element III