uniapp封装movable-area+movable-view组件,实现悬浮按钮可拖动,主动吸附 ...

打印 上一主题 下一主题

主题 1658|帖子 1658|积分 4974

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
兼容H5、App、微信小程序
子组件
/components/ShopCar/ShopCar.vue
  1. <template>
  2.         <view class="ShopCar">
  3.                 <movable-area class="movableArea" v-if="isShow">
  4.                         <movable-view class="movableView" :position="position" :x="x" :y="y" :direction="direction"
  5.                                 :damping="damping" @change="onChange" @touchend="onTouchend">
  6.                                 <view class="icon-box" @click="handleIcon()">
  7.                                         <u-icon name="arrow-upward" color="#ff6a00" size="20"></u-icon>
  8.                                 </view>
  9.                         </movable-view>
  10.                 </movable-area>
  11.         </view>
  12. </template>
  13. <script>
  14.         export default {
  15.                 props: {
  16.                         damping: {
  17.                                 type: Number,
  18.                                 default: 10
  19.                         },
  20.                         direction: {
  21.                                 type: String,
  22.                                 default: "all"
  23.                         },
  24.                         position: {
  25.                                 type: Number,
  26.                                 default: 4
  27.                         },
  28.                 },
  29.                 data() {
  30.                         return {
  31.                                 x: 0,
  32.                                 y: 0,
  33.                                 x1: 0,
  34.                                 x2: 0,
  35.                                 y1: 0,
  36.                                 y2: 0,
  37.                                 move: {
  38.                                         x: 0,
  39.                                         y: 0
  40.                                 },
  41.                                 isShow: false
  42.                         }
  43.                 },
  44.                 mounted() {
  45.                         uni.getSystemInfo({
  46.                                 success: (res) => {
  47.                                         this.x1 = 0;
  48.                                         this.x2 = parseInt(res.windowWidth) - 50;
  49.                                         this.y1 = 0;
  50.                                         this.y2 = parseInt(res.windowHeight) - 20;
  51.                                         this.$nextTick(() => {
  52.                                                 if (this.position == 1 || this.position == 2) this.y = parseInt(this.y2 * 0.2);
  53.                                                 if (this.position == 3 || this.position == 4) this.y = parseInt(this.y2 * 0.8);
  54.                                                 if (this.position == 1 || this.position == 3) this.x = parseInt(this.x1);
  55.                                                 if (this.position == 2 || this.position == 4) this.x = parseInt(this.x2);
  56.                                                 this.move.x = this.x;
  57.                                                 this.move.y = this.y;
  58.                                                 this.isShow = true
  59.                                         })
  60.                                 }
  61.                         })
  62.                 },
  63.                 methods: {
  64.                         onChange(e) {
  65.                                 if (e.detail.source === "touch") {
  66.                                         this.move.x = e.detail.x;
  67.                                         this.move.y = e.detail.y;
  68.                                 }
  69.                         },
  70.                         onTouchend() {
  71.                                 this.x = this.move.x;
  72.                                 this.y = this.move.y;
  73.                                 this.$nextTick(() => {
  74.                                         if (this.move.x < this.x2 / 2) this.x = this.x1;
  75.                                         else this.x = this.x2;
  76.                                 })
  77.                         },
  78.                         handleIcon() {
  79.                                 uni.pageScrollTo({
  80.                                         scrollTop: 0,
  81.                                         duration: 0,
  82.                                 });
  83.                         }
  84.                 },
  85.         }
  86. </script>
  87. <style lang="scss" scoped>
  88.         .ShopCar {
  89.                 .movableArea {
  90.                         position: fixed;
  91.                         top: 0;
  92.                         left: 0;
  93.                         width: 100%;
  94.                         height: 100%;
  95.                         pointer-events: none; //设置area元素不可点击,则事件便会下移至页面下层元素
  96.                         z-index: 999;
  97.                         .movableView {
  98.                                 width: 70rpx;
  99.                                 height: 70rpx;
  100.                                 pointer-events: auto;
  101.                                 padding: 0 30rpx;
  102.                                 .icon-box {
  103.                                         width: 100%;
  104.                                         height: 100%;
  105.                                         display: flex;
  106.                                         align-items: center;
  107.                                         justify-content: center;
  108.                                         border: 1rpx solid #ff6a00;
  109.                                         box-sizing: border-box;
  110.                                         border-radius: 50%;
  111.                                         background-color: #fff;
  112.                                         animation: iconBox 5s linear infinite; //进行旋转
  113.                                 }
  114.                                 @keyframes iconBox {
  115.                                         0% {
  116.                                                 -webkit-transform: rotate(0deg);
  117.                                         }
  118.                                         25% {
  119.                                                 -webkit-transform: rotate(90deg);
  120.                                         }
  121.                                         50% {
  122.                                                 -webkit-transform: rotate(180deg);
  123.                                         }
  124.                                         75% {
  125.                                                 -webkit-transform: rotate(270deg);
  126.                                         }
  127.                                         100% {
  128.                                                 -webkit-transform: rotate(360deg);
  129.                                         }
  130.                                 }
  131.                         }
  132.                 }
  133.         }
  134. </style>
复制代码
父组件
  1. <template>
  2.    <ShopCar v-if="scrollTop>200" />
  3. </template>
  4. <script>
  5.         export default {
  6.                 data() {
  7.             return {
  8.                 scrollTop: 0
  9.             }
  10.         },
  11.         onPageScroll(e) {
  12.                     this.scrollTop = e.scrollTop
  13.         },
  14.     }
  15. </script>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

汕尾海湾

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表