当前位置:网站首页>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>
边栏推荐
猜你喜欢

MySQL advanced part 2: MySQL architecture

Liunx starts redis

Simple selection sort of selection sort

Suppose a bank's ATM machine, which allows users to deposit and withdraw money. Now there is 200 yuan in an account, and both user a and user B have the right to deposit and withdraw money from this a
![[moviepy] unable to find a solution for exe](/img/0a/4841f53cedc1333654b9443e406f4c.jpg)
[moviepy] unable to find a solution for exe

Client use of Argo CD installation

MySQL advanced part 2: storage engine

P2575 master fight

区间问题 AcWing 906. 区间分组

20220213-CTF MISC-a_ good_ Idea (use of stegsolve tool) -2017_ Dating_ in_ Singapore
随机推荐
Regulations for network security events of vocational group in 2022 Guizhou Vocational College skill competition
求组合数 AcWing 889. 满足条件的01序列
MySQL advanced part 2: the use of indexes
AE tutorial - path growth animation
LeetCode-61
4. 对象映射 - Mapping.Mapster
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
Leetcode-3: Longest substring without repeated characters
MySQL advanced part 1: triggers
栈 AcWing 3302. 表达式求值
Redis-02.Redis命令
Doing SQL performance optimization is really eye-catching
2022-5-第四周日报
5.Oracle-表空间
Sum of three terms (construction)
11-gorm-v2-02-create data
Sword finger offer II 058: schedule
博弈论 AcWing 893. 集合-Nim游戏
How to make water ripple effect? This wave of water ripple effect pulls full of retro feeling
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