便利贴--17{移动端滑动模块}

便利贴--17{移动端滑动模块}

id

请添加图片描述

动态绑定高度

<div id="mobileActivity" :style="{ height: height + 'px' }">

使用方法

 this.domMove = new moveWindow({
      id: "mobileActivity",
      minHeight: that.useHeight,
      critical: 100,
      fn: function (res) {
        that.height = res.height;
        // console.log(res.height);
        if (res.state != "on") {
          that.openValue = res.state;
        }
      },
    }).init();

class

class moveWindow {
  constructor(data) {
    this.id = data.id;
    this.start = 0;
    this.end = 0;
    this.domHeight = 0;
    this.domMove = null;
    this.openValue = false;
    this.useHeight = data.minHeight;
    this.height = data.minHeight;
    this.critical = data.critical;
    let that = this;
    this.fn =
      data.fn ||
      function (res) {
        console.log("未设置回调函数");
        console.log(res);
      };

    this.ListenerTouchmove = function (window) {
      let moseHeight = window.changedTouches[0].clientY;
      //   let windowHeight = document.body.clientHeight; //屏幕宽度
      let endOn = moseHeight;
      let cha = +that.start - +endOn;
      //   let cha = windowHeight - moseHeight;
      //   console.log(window);
      //   if (cha >= windowHeight / 2) {
      //     cha = windowHeight / 2;
      //   } else if (cha <= that.useHeight) {
      //     cha = that.useHeight;
      //   }
      let d = {
        state: "on",
        height: +that.domHeight + cha,
      };
      that.fn(d);
    };
    this.ListenerTouchstart = function (window) {
      let moseHeight = window.changedTouches[0].clientY;
      that.start = moseHeight;
      that.domHeight = document
        .getElementById(that.id)
        .style.cssText.split("px")[0]
        .split("height:")[1];
    };
    this.ListenerTouchend = function (window) {
      let moseHeight = window.changedTouches[0].clientY;
      that.end = moseHeight;
      let cha, states;
      if (that.end >= that.start) {
        states = false;
        cha = that.end - that.start;
      } else {
        states = true;
        cha = that.start - that.end;
      }
      //   console.log(that.start, that.end);
      //   console.log(cha);
      if (cha >= that.critical) {
        //高
        that.opens(states);
      } else if (cha < that.critical) {
        //低
        that.opens(states);
      }
    };
    return this;
  }
  //启动监听事件
  init() {
    this.domMove = document.getElementById(this.id);
    this.domMove.addEventListener("touchmove", this.ListenerTouchmove);
    // 结束位置靠滑动距离判断
    this.domMove.addEventListener("touchstart", this.ListenerTouchstart);
    this.domMove.addEventListener("touchend", this.ListenerTouchend);
  }
  //关闭监听事件
  closeInit() {
    this.domMove = document.getElementById(this.id);
    this.domMove.removeEventListener("touchmove", this.ListenerTouchmove);
    // 结束位置靠滑动距离判断
    this.domMove.removeEventListener("touchstart", this.ListenerTouchstart);
    this.domMove.removeEventListener("touchend", this.ListenerTouchend);
  }
  //输出事件
  opens(val) {
    this.openValue = val;
    if (val) {
      let windowHeight = document.body.clientHeight;
      this.height = windowHeight / 2;
    } else {
      this.height = this.useHeight;
    }
    let d = {
      state: this.openValue,
      height: this.height,
    };
    this.fn(d);
  }
  // 结束位置是一半判断
  //  domMove.addEventListener("touchend", function (window) {
  //   let moseHeight = window.changedTouches[0].clientY;
  //   let windowHeight = document.body.clientHeight;
  //   let cha = windowHeight - moseHeight;
  //   console.log(cha, "最后位置");
  //   let centerPoint =
  //     (windowHeight / 2 - +that.useHeight) / 2 + +that.useHeight;
  //   if (cha >= centerPoint) {
  //     cha = windowHeight / 2;
  //   } else if (cha < centerPoint) {
  //     cha = that.useHeight;
  //   }
  //   that.height = cha;
  // });
}

export default moveWindow;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轻动琴弦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值