首页 > 编程语言 >实现可移动悬浮按钮(微信小程序/H5移动端html)

实现可移动悬浮按钮(微信小程序/H5移动端html)

时间:2023-01-03 14:35:02浏览次数:45  
标签:document 微信 oTop H5 height touch backToTop var 移动

微信小程序实现方案:

我们可以利用微信小程序的内置组件轻松实现!
1.将整个屏幕用movable-area组件覆盖,
2.在movable-area内部添加一个movable-view实现自由滑动。
3.重点:CSS属性 pointer-events 。
代码奉上:
wxml:

<movable-area class="movable-area">
    <movable-view class="movable-view" direction="all">点击</movable-view>
</movable-area>

wxss:

.movable-area{
	/* 这个属性设置为none,让所有事件穿透过去 */
    pointer-events:none;
    z-index: 100;
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
.movable-view{ 
	/* 重设为auto,覆盖父属性设置 */
	pointer-events:auto;
	height: 50px;
	width: 50px;
	background: rgba(0,0,0,.5);
	border-radius: 50%;
	display: flex;
	justify-content: center;
	align-items: center;
	color: #fff;
}

H5移动端(html)实现方案:

 html:

<a href="#" id="backToTop" class="backToTop">拖我</a>
<div>ooo</div>
<div class="oo">999</div>

css:

* {
       margin: 0;
       padding: 0;
   }
   html,
   body {
       width: 100%;
       height: 100%;
   }
   #backToTop {
       width: 50px;
       height: 50px;
       border-radius: 50%;
       border: 2px solid pink;
       position: fixed;   /*这里需要固定定位*/
       text-align: center;
       line-height: 50px;
       text-decoration: none;
       color: purple;
   }
   /*辅助测试*/
   div {
       width: 200px;
       height: 300px;
       background-color: deeppink;
   }
   .oo {
       width: 230px;
       height: 450px;
       background-color: deepblue;
   }

jsvascript:

var backToTop = document.getElementById("backToTop");
    //限制最大宽高,不让滑块出去
    var maxW = document.body.clientWidth - backToTop.offsetWidth;
    var maxH = document.body.clientHeight - backToTop.offsetHeight;

    backToTop.addEventListener('touchstart', touchStart,true);
    backToTop.addEventListener('touchmove', touchMove,true);
    backToTop.addEventListener('touchend', touchEnd,true);
    //1、手指触摸开始,记录div的初始位置
    function touchStart(e) {
        var ev = e || window.event;
        var touch = ev.targetTouches[0];
        oL = touch.clientX - backToTop.offsetLeft;
        oT = touch.clientY - backToTop.offsetTop;
        console.log('touch.clientX===',touch.clientX);
        console.log('oT===',oT);
        // document.addEventListener("touchmove", defaultEvent, false);
		// 拖动按钮时禁止滑动
        document.body.style.overflow = 'hidden';
    }
    //2、触摸过程,位置记录
    function touchMove(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 < 0) {
            oTop = 0;
        } else if (oTop >= maxH) {
            oTop = maxH;
        }
        backToTop.style.left = oLeft + 'px';
        backToTop.style.top = oTop + 'px';
        // 拖动按钮时禁止滑动
        document.body.style.overflow = 'hidden';
    }
    //3、触摸结束时的处理
    function touchEnd(e) {
        document.removeEventListener("touchmove", defaultEvent);
        console.log('e==>',e);
        // 结束拖动按钮时允许滑动
        document.body.style.overflow = 'auto';
    }
    //阻止默认事件
    function defaultEvent(e) {
        //e.defaultPrevented();   
        console.log('eee==>>',e);
    }

  

标签:document,微信,oTop,H5,height,touch,backToTop,var,移动
From: https://www.cnblogs.com/btsn/p/17022068.html

相关文章

  • 如何导出微信好友?通讯录好友保存
    现在微信已经是日常使用最多的一个APP之一,同时也是工作上与客户联系最多的APP,随着时间流逝,在微信中保存的联系人会越来越多,相应地会有很多重要的联系人在微信里,这个时候备份......
  • 不登录微信怎么查看电脑里的微信聊天DAT图片
    -3不知道大家有没有这样一种需求,需要在不登录微信的情况下查看微信的图片。做为一个做小工具的网管,我与很多网友沟通过,有不少人是有这种需求的,但是微信聊天收发的图片文件,保......
  • 微信联系人一键导出的方法在这里
    6-1对于做微信私域流量、做网络销售的公司,有时候需要对公司的客户或者运营微信做一下备份,防止出现意外丢失的情况,但是在网上找的别的工具要么使用比较不方便,要么速度比较慢,......
  • 安全可靠快速地导出微信聊天记录
    12-2现在微信上的消息非常多,不管是生活也好还是工作也好,大多数人基本上已不再使用QQ,并且即使使用QQ,它也自带了导出功能,而微信不带这个功能。如果想要导出微信的聊天记录该咋......
  • 2022最详细最快微信聊天记录备份&导出方案
     12-1在有些情况下,比如需要换电脑的时候,或者需要对某些重要的聊天对话做一些备份,就凭微信本身的功能,是不行的,微信根本不提供聊天记录导出功能。使用本文章的方法,可以自动地......
  • 电脑端微信dat文件怎么打开
    1-2电脑端的微信,在聊天过程中收发的图片都是以DAT文件保存在电脑里,加密的,一般是不能直接打开的。但是图片在电脑中占用的空间非常大,如果是平常工作的微信,会接收到很多图片,时......
  • 电脑版微信dat文件用什么软件打开
    1-4一般来说,凡是说到微信电脑版的DAT文件,指的都是聊天过程中收发的图片,加密保存在电脑里。这些文件正常情况下也只能在微信登录后,在微信里查看,因为微信加密的当然只有微信才......
  • 在微信公众号中插入数学公式
    1.说明:腾讯至今没有在微信公众号原生编辑器中提供公式插入功能。因为微信公众号不支持引入外部CSS和JS脚本,所以无法引入MathJax来支持数学公式显示。解决方案为:使......
  • 微信红包.....真的是抢的越晚越好吗
    微信红包.....真的是抢的越晚越好吗​​说了这么多,请注意一个问题,这一切的前提都是…..​​​​你能抢的到红包,否则一切都是废话​​​​1.首先先给出一个有意思的小测试…......
  • Spring Security 源码分析(四):Spring Social实现微信社交登录
    前言在上一章Spring-Security源码分析三-Spring-Social社交登录过程中,我们已经实现了使用​​SpringSocial​​​+​​Security​​的QQ社交登录。本章我们将实现微信的社......