移动端悬浮框可移动,可回弹,Vue and React

一,首先讲 React的悬浮框

示例,可参照链接

Demo文档,可参照链接

1. 安装

npm install suspend-button -S

2. 使用

import React, { Component } from ‘react‘
import ReactDOM from ‘react-dom‘
import SuspendButton from ‘suspend-button‘

class App extends Component {
  render() {
    return (
      <SuspendButton></SuspendButton>
    )
  }
}

ReactDOM.render(
  <App />,
  document.getElementById(‘container‘)
)

  

二,Vue的悬浮框 (可直接将代码进行拷贝到页面组件中)

<template>
  <div class="removeHome">
    <span class="t-suspend-button"
          @touchstart="onTouchStart"
          @touchmove="onTouchMove"
          @touchend="onTouchEnd"
          ref="remove"
          :style="`left: ${oLeft}px; top: ${oTop}px;`">

      <div class="yuanqiu"
           @click="$router.replace({path: ‘/‘})">
        <img :src="img" />
      </div>
    </span>
  </div>
</template>

<script>
export default {
  props: {
    img: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      oLeft: "",
      oTop: "",
      $vm: null, // 悬浮按钮
      moving: false, // 移动状态
      oW: null, // 悬钮距离
      oH: null,
      htmlWidth: null, // 页面宽度
      htmlHeight: null,
      bWidth: null, // 悬钮宽度
      bHeight: null,
      click: false // 是否是点击
    };
  },
  mounted() {
    this.$refs.remove.addEventListener(
      "touchmove",
      e => {
        if (e.cancelable) {
          e.preventDefault();
        }
      },
      { passive: false }
    );
  },
  methods: {
    // 移动触发
    onTouchStart(e) {
      e = e.touches[0];
      this.click = true;

      this.oW = e.clientX - this.$refs.remove.getBoundingClientRect().left;
      this.oH = e.clientY - this.$refs.remove.getBoundingClientRect().top;

      console.log("e.clientX宽", e.clientX, "e.clientY高", e.clientY);

      console.log(
        "移动宽",
        this.$refs.remove.getBoundingClientRect().left,
        "移动高",
        this.$refs.remove.getBoundingClientRect().top
      );

      this.htmlWidth = document.documentElement.clientWidth;
      this.htmlHeight = document.documentElement.clientHeight;

      console.log("body宽", this.htmlWidth, "body高", this.htmlHeight);

      this.bWidth = this.$refs.remove.offsetWidth;
      this.bHeight = this.$refs.remove.offsetHeight;

      console.log("a宽", this.oW, "a高", this.oH);

      let oLeft = e.clientX - this.oW;
      let oTop = e.clientY - this.oH;

      this.oLeft = oLeft;
      this.oTop = oTop;

      this.moving = true;
    },
    // 移动结束
    onTouchEnd(e) {
      this.moving = false;

      this.$refs.remove.class + " t-suspend-button-animate";

      // 左侧距离
      let oLeft = this.oLeft;
      if (oLeft < (this.htmlWidth - this.bWidth) / 2) {
        oLeft = 0;
      } else {
        oLeft = this.htmlWidth - this.bWidth;
      }

      // if (this.click) {
      //   this.props.onClick();
      // }
      // }
      // if (oTop < 0) {
      //   oTop = 0;
      // } else if (oTop > this.htmlHeight - this.bHeight) {
      //   oTop = this.htmlHeight - this.bHeight;
      // }

      this.oLeft = oLeft;
    },
    // 开始移动
    onTouchMove(e) {
      this.$refs.remove.class = "t-suspend-button";
      this.moving && this.onMove(e);
    },
    // 移动中
    onMove(e) {
      e = e.touches[0];
      this.click = false;

      // 左侧距离
      let oLeft = e.clientX - this.oW;
      let oTop = e.clientY - this.oH;
      console.log("移动左距离", oLeft, "移动上距离", oTop);
      if (oLeft < 0) {
        oLeft = 0;
      } else if (oLeft > this.htmlWidth - this.bWidth) {
        oLeft = this.htmlWidth - this.bWidth;
      }
      if (oTop < 0) {
        oTop = 0;
      } else if (oTop > this.htmlHeight - this.bHeight) {
        oTop = this.htmlHeight - this.bHeight;
      }

      this.oLeft = oLeft;
      this.oTop = oTop;
    }
  }
};
</script>

<style lang="scss">
.removeHome {
  .t-suspend-button {
    position: fixed;
    bottom: 105px;
    right: 9px;
    width: 3rem;
    height: 3rem;
    border-radius: 2rem;
    z-index: 99999;
  }

  .t-suspend-button img {
    width: 100%;
    height: 100%;
  }

  .t-suspend-button-animate {
    transition-duration: 0.4s;
  }
  .yuanqiu {
    width: 48px;
    height: 48px;
    img {
      width: 100%;
      height: 100%;
    }
  }
}
</style>

  

原文地址:https://www.cnblogs.com/gqx-html/p/12552810.html

时间: 2024-08-02 00:24:25

移动端悬浮框可移动,可回弹,Vue and React的相关文章

Js实现一键分享效果--悬浮框状态

一.html框架 /*整个分享区域*/    <div id="share">         /*分享区域的顶端文字描述*/              <h3><img src="../img/chaohao.png">分享到...</h3>         /*分享区域的的具体分享网站链接*/              <ul>                    <li><a hr

小米Adnroid默认禁止悬浮框的使用,导致开发的悬浮框无法接收事件

比如你建了一个悬浮框: WindowManager windowManager = getWindowManager(context); int screenWidth = windowManager.getDefaultDisplay().getWidth(); if (smallWindow == null) { smallWindow = new FloatWindowSmallView(context); if (smallWindowParams == null) { smallWin

js实现页面悬浮框

当滚动条下拉时,悬浮框位置不变,主要是 position:fixed;样式的作用. 当下拉到一定程度,接近footer时,我用js控制div消失,往上拉滚动条时又显示. <!DOCTYPE html> <html> <head> <title></title> <style type="text/css"> .div1 { height:2000px; } .div2 { width:100%; height:35

“右侧悬浮框”页面特效

功能描述: 悬浮框固定在页面右下方,随着滚动条的滚动,悬浮框运动到固定位置. 实现效果: 编码思路: 获取滚动条滚动时,方块需要移动的目标位置(可视区域的高度-物体高度-滚动的高度). 套用运动框架. 示例代码:

JavaScript侧边悬浮框

<script> window.onscroll=function(){ var oDiv=document.getElementById('div1'); var scrollTop=document.documentElement.scrollTop||document.body.scrollTop; oDiv.style.top=(document.documentElement.clientHeight-oDiv.offsetHeight)/2+scrollTop+'px'; star

android悬浮框的使用

实质就是开启一个界面透明的activity 注意:在startService 想在startActivity intent必须要设置addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 直接上Demo---myFloat http://pan.baidu.com/s/1gdIK4QR android悬浮框的使用

悬浮框

1. 右侧底部悬浮框 运行效果图: [http://runjs.cn/code/zvjocat0] 要点:oDiv.style.top=document.documentElement.clientHeight-oDiv.offsetHeight+scrollTop; 2. 右侧中部悬浮框 运行效果图:[http://runjs.cn/code/bws1gmne] 要点: oDiv.style.top=Math.ceil((document.documentElement.clientHeigh

使用悬浮框监听内存的使用状态 -- 附源码

源码下载地址:http://download.csdn.net/detail/hewence1/8176601 <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">先看一下效果:  显示当前内存使用率55% ,每一秒都刷新一次</span> 实现原理,在Service中创建一个悬浮框就可,在service中每秒钟访问计算一次单

Echarts ecomfe 触摸屏 touch 在IE10下无法显示悬浮框

问题描述: Windows 8 IE10浏览http://echarts.baidu.com/doc/example/line2.html 时,鼠标放置在数据点上时无法显示悬浮框. 正常情况为: 而现在情况为: ? 问题分析: 公司有各种型号电脑,X230,W530,X240,业务部门多使用x240, 这一款笔记本屏幕带有触摸屏功能,其他型号没有.所有操作系统都为win8,IE10小版本号也一致. 最初就将问题定位为触摸屏引起了这个问题,但一直无法找到具体原因.因为在chrome,firfox等