H5页面新增鼠标右击和长按触发事件

该文章展示了如何在H5页面中添加鼠标右击和长按事件,同时确保这些事件不会影响原有的点击事件。通过Van-Cell组件,结合Vue的事件绑定(@touchstart,@touchend,@mousedown),以及定义相应的事件处理函数(longPress,removePress,rightClick),可以在满足长按时间和鼠标右键点击时触发特定功能,同时阻止默认的上下文菜单弹出。
摘要由CSDN通过智能技术生成

H5页面新增鼠标右击和长按触发事件,不影响点击事件。

示例如下:

1.新增触发事件

<van-cell
  class="list-item"
  v-for="item in dataSource.list"
  :key="item.id"
  @touchstart="longPress(item, index)"
  @touchend="removePress(item, index)"
  @touchmove="touchmove(item, index)"
  @mousedown.native="(e) => rightClick(item, index, e)"
  @click="goDetail(item)"
  is-link
>
  <template #default>
    <div class="blue-bar"></div>
    <span class="item-title">{{ item.workConfName }}</span>
    <div>
      <div>
        创建时间:{{
          item.createTime ? parseTime(item.createTime) : ""
        }}
      </div>
    </div>
  </template>
</van-cell>

2.定义触发事件

const isLongpress = ref(false);
const touchstartTime = ref("");
const touchendTime = ref("");

const longPress = (item, index, e) => {
  touchstartTime.value = new Date().getTime();
  isLongpress.value = true;
};
const removePress = (item, index) => {
  curItem.value = item;
  touchendTime.value = new Date().getTime();
  let duration = touchendTime.value - touchstartTime.value;
  if (isLongpress.value && duration >= 600) {
    showActionSheet.value = true;
  }
};

const rightClick = (item, index, e) => {
  if (e.button == 2) {
    curItem.value = item;
    showActionSheet.value = true;
  }
};

const touchmove = (item, index) => {
  // isLongpress.value = false;
};

3.不影响原点击触发事件

const goDetail = (record) => {
  sessionStorage.workConf = JSON.stringify(record);
  router.push({
    path: "/collectResult",
  });
};

4.禁止弹出右击默认菜单

document.oncontextmenu = function () { return false; }

即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值