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

   2023-03-08 学习力0
核心提示:一,首先讲 React的悬浮框示例,可参照链接Demo文档,可参照链接 1. 安装npm install suspend-button -S2. 使用import React, { Component } from 'react'import ReactDOM from 'react-dom'import SuspendButton from 'suspend-button'class App extends Com

一,首先讲 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>

  

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • React实现基于Antd密码强度校验组件示例详解
    React实现基于Antd密码强度校验组件示例详解
    目录引言效果预览组件思想组件开发引言最近在开发 Nest 和 Umi 技术栈的个人项目,在用户管理模块需要用到一个密码强度校验组件,在网上寻找一方资料,没有找到自己想要的,特此自己造轮子!效果预览组件思想既然是密码强度校验,那么强度就必须有个梯度,这
    03-16
  • 03 React快速入门(三)——实现从一个输入框中添加完数据后此输入框内容清除的功能
    03 React快速入门(三)——实现从一个输入框中
    功能描述:      我们在一个输入框输入内容,然后点击添加按钮,此输入框的内容就会添加到页面上,但是此输入框中还存在上次输入的内容,我们想在每次输入添加完成之后,此输入框中的内容就会清除,如图:   实现思路:      我们可以先在输入框上定
    03-08
  • react编译器jsxTransformer,babel
    1.JSX是什么JSX其实是JavaScript的扩展,React为了代码的可读性更方便地创建虚拟DOM等原因,加入了一些类似XML的语法的扩展。2.编译器——jsxTransformerJSX代码并不能直接运行,需要将它编译成正常的JavaScript表达式才能运行,jsxTransformer.js就是这一编
    03-08
  • G2( bizCharts ) React 绘制混合图例
    G2( bizCharts ) React 绘制混合图例
     G2( bizCharts ) React 绘制混合图例,// data-set 可以按需引入,除此之外不要引入别的包import React from 'react';import { Chart, Axis, Tooltip, Geom, Legend, Label } from 'bizcharts';import DataSet from '@antv/data-set';// 下面的代码会被作为
    03-08
  • React-多页面应用 react怎么写页面
    React-多页面应用 react怎么写页面
    1初始化项目npm init create-react-app my-app2.修改indeximport React from 'react';import ReactDOM from 'react-dom';import './index.css';import App from './App';ReactDOM.render(App /, document.getElementById('root')
    03-08
  • react-native start 启动错误解决方法
    ERRORError watching file for changes: EMFILE{"code":"EMFILE","errno":"EMFILE","syscall":"Error watching file for changes:","filename":null}Error: Error watching file for changes: EMFILE
    03-08
  • React兄弟组件通信(发布者-订阅者模式)
    // eventProxy.js'use strict';const eventProxy = {onObj: {},oneObj: {},on: function(key, fn) {if(this.onObj[key] === undefined) {this.onObj[key] = [];}this.onObj[key].push(fn);},one: function(key, fn) {if(this.oneObj[key] === undefined) {thi
    03-08
  • React笔记_(7)_react路由 react路由配置
    路由路由(routing)是指分组从源到目的地时,决定端到端路径的网络范围的进程。路由器当然是作为一个转发设备出现的,主要是转发数据包来实现网络互联。那么react的路由到底指的是什么呢?举个栗子~~~在网页中点击后,从A页面跳到B页面,跳转过程中url发生变
    03-08
  • react-native关闭所有黄色警告 react native st
     将以下这两句话加在index.js(入口文件)中,放在AppRegistry.registerComponent('App', () = App)之前即可1 console.ignoredYellowBox = ['Warning: BackAndroid is deprecated. Please use BackHandler instead.','source.uri should not be an empty str
    03-08
  • react中style的写法 react styles
    div style={{width: 20px; height=30px}}style的写法/div 
    03-08
点击排行