京东6.18大促主会场领京享红包更优惠

 找回密码
 立即注册

QQ登录

只需一步,快速开始

html5实现可拖拽移动的悬浮图标的示例代码

2024-11-2 22:25| 发布者: 44f6fa4f5| 查看: 37| 评论: 0

摘要: h5 上经常会有这样的需求: 需要在页面上加上一个悬浮图标,大抵是如下图的终极实现 但是每每按照设计稿是不会遮住主体区域的,但是现实上偶然间偏偏会遮挡主体区域,但是为了更好的点击量,又不得不悬浮在页面上...

h5 上经常会有这样的需求:

需要在页面上加上一个悬浮图标,大抵是如下图的终极实现

但是每每按照设计稿是不会遮住主体区域的,但是现实上偶然间偏偏会遮挡主体区域,但是为了更好的点击量,又不得不悬浮在页面上...

如果能让图标可拖拽移动,这样在遮住主体区域之后,用户可自由移动,这种方案及可以兼顾了。

实现的结果如下:

(和微信的浮窗结果类似,左右位置靠边显示,上下位置随意放)

话不多说,上代码了

[code] <div class="ys-float-btn" :style="{'width': itemWidth+'px','height': itemHeight+'px','left': left+'px','top': top+'px'}" ref="div" @touchstart.prevent="(e) => {dragStart(e)}" @touchend.prevent="(e) => {dragEnd(e)}" @touchmove.prevent="(e) => {dragProgress(e)}" > <img src="./../assets/fc-icon.png" /> </div>[/code] [code] // 代码直接在 vue 项目里,可自行改为js/jquery 写法 data () { return { gapWidth: 10, itemWidth: 20, // 图标的宽度 itemHeight: 30 // 图标的高度 } }, created() { this.clientWidth = document.documentElement.clientWidth; this.clientHeight = document.documentElement.clientHeight; this.left = this.clientWidth - this.itemWidth - this.gapWidth; this.top = this.clientHeight*0.8; } methods: { dragStart(e) { this.$refs.div.style.transition = 'none'; }, dragEnd(e) { this.$refs.div.style.transition = 'all 0.3s'; if (this.left > this.clientWidth/2) { this.left = this.clientWidth - this.itemWidth - this.gapWidth; } else { this.left = this.gapWidth; } }, dragProgress(e) { if (e.targetTouches.length === 1) { let touch = event.targetTouches[0]; this.left = touch.clientX - this.itemWidth/2; this.top = touch.clientY - this.itemHeight/2; } } } [/code]

以上代码既可以上下也可以左右移动,如果只想让可上下移动,就去掉 left 相干的设置和计算。

到此这篇关于html5实现可拖拽移动的悬浮图标的示例代码的文章就介绍到这了,更多相干html5可拖拽悬浮图标内容请搜刮脚本之家从前的文章或继承欣赏下面的相干文章,渴望各人以后多多支持脚本之家!


来源:https://www.jb51.net/html5/768084.html
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
关闭

站长推荐上一条 /6 下一条

QQ|手机版|小黑屋|梦想之都-俊月星空 ( 粤ICP备18056059号 )|网站地图

GMT+8, 2025-7-2 08:41 , Processed in 0.031910 second(s), 18 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2025 Discuz! Team.

返回顶部