当前位置: 首页 > news >正文

uniapp封装movable-area+movable-view组件,实现悬浮按钮可拖动,自动吸附边缘效果,自动向两边靠拢

兼容H5、App、微信小程序

子组件

/components/ShopCar/ShopCar.vue

<template><view class="ShopCar"><movable-area class="movableArea" v-if="isShow"><movable-view class="movableView" :position="position" :x="x" :y="y" :direction="direction":damping="damping" @change="onChange" @touchend="onTouchend"><view class="icon-box" @click="handleIcon()"><u-icon name="arrow-upward" color="#ff6a00" size="20"></u-icon></view></movable-view></movable-area></view>
</template><script>export default {props: {damping: {type: Number,default: 10},direction: {type: String,default: "all"},position: {type: Number,default: 4},},data() {return {x: 0,y: 0,x1: 0,x2: 0,y1: 0,y2: 0,move: {x: 0,y: 0},isShow: false}},mounted() {uni.getSystemInfo({success: (res) => {this.x1 = 0;this.x2 = parseInt(res.windowWidth) - 50;this.y1 = 0;this.y2 = parseInt(res.windowHeight) - 20;this.$nextTick(() => {if (this.position == 1 || this.position == 2) this.y = parseInt(this.y2 * 0.2);if (this.position == 3 || this.position == 4) this.y = parseInt(this.y2 * 0.8);if (this.position == 1 || this.position == 3) this.x = parseInt(this.x1);if (this.position == 2 || this.position == 4) this.x = parseInt(this.x2);this.move.x = this.x;this.move.y = this.y;this.isShow = true})}})},methods: {onChange(e) {if (e.detail.source === "touch") {this.move.x = e.detail.x;this.move.y = e.detail.y;}},onTouchend() {this.x = this.move.x;this.y = this.move.y;this.$nextTick(() => {if (this.move.x < this.x2 / 2) this.x = this.x1;else this.x = this.x2;})},handleIcon() {uni.pageScrollTo({scrollTop: 0,duration: 0,});}},}
</script><style lang="scss" scoped>.ShopCar {.movableArea {position: fixed;top: 0;left: 0;width: 100%;height: 100%;pointer-events: none; //设置area元素不可点击,则事件便会下移至页面下层元素z-index: 999;.movableView {width: 70rpx;height: 70rpx;pointer-events: auto;padding: 0 30rpx;.icon-box {width: 100%;height: 100%;display: flex;align-items: center;justify-content: center;border: 1rpx solid #ff6a00;box-sizing: border-box;border-radius: 50%;background-color: #fff;animation: iconBox 5s linear infinite; //进行旋转}@keyframes iconBox {0% {-webkit-transform: rotate(0deg);}25% {-webkit-transform: rotate(90deg);}50% {-webkit-transform: rotate(180deg);}75% {-webkit-transform: rotate(270deg);}100% {-webkit-transform: rotate(360deg);}}}}}
</style>

父组件

<template><ShopCar v-if="scrollTop>200" />
</template><script>export default {data() {return {scrollTop: 0}},onPageScroll(e) {this.scrollTop = e.scrollTop},}
</script>

http://www.lryc.cn/news/472669.html

相关文章:

  • 音频重采样(libresample)
  • 使用Python来下一场雪
  • Pyspark中pyspark.sql.functions常用方法(4)
  • Nginx 配置基于IP 地址的 Web 服务器
  • 【TVM 教程】线性和递归核
  • 猫主福利大放送,双11猫奴们的购物狂欢节 养猫必备清单
  • Linux中gcc的使用
  • React 组件 API
  • 一个使用接口模式、工厂模式、模板方法模式的日志文件系统
  • openjdk17 C++源码是怎么给java字段赋值的
  • C++初阶(八)--内存管理
  • C# 企业微信机器人推送消息 windows服务应用程序的使用
  • 社区交流系统设计与实现
  • 【模型学习之路】手写+分析bert
  • Redis学习文档(常见面试题)
  • 【C++刷题】力扣-#594-最长和谐子序列
  • MoveIt 控制自己的真实机械臂【2】——编写 action server 端代码
  • C#制作学生管理系统
  • python Pandas合并(单元格、sheet、excel )
  • OJ在线编程常见输入输出练习【JavaScript】
  • 新能源汽车空调系统:绿色出行的舒适保障
  • Date工具类详细汇总-Date日期相关方法
  • TMUX1308PWR规格书 数据手册 具有注入电流控制功能的 5V 双向 8:1单通道和 4:1 双通道多路复用器芯片
  • 证件照怎么换底色?简单又快速!不看后悔
  • Rust 基础语法与常用特性
  • 一、开发环境的搭建
  • Docker:存储原理
  • ts:数组的常用方法(push、pop、shift、unshift、splice、slice)
  • 物联网网关确保设备安全
  • Vue学习笔记(五)