• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    html5实现可拖拽移动的悬浮图标的示例代码

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

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

    但是往往按照设计稿是不会遮住主体区域的,但是实际上有时候偏偏会遮挡主体区域,但是为了更好的点击量,又不得不悬浮在页面上...

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

    实现的效果如下:

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

    话不多说,上代码了

    <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>
    // 代码直接在 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;        
            }      
        }
    } 
    

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

    到此这篇关于html5实现可拖拽移动的悬浮图标的示例代码的文章就介绍到这了,更多相关html5可拖拽悬浮图标内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

    上一篇:html+css实现图片扫描仪特效
    下一篇:前端Html5如何实现分享截图的示例代码
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

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