事件穿透效果

本文介绍了在项目中遇到的UI设计问题,如何通过事件穿透技术解决底部地图和两侧面板遮罩导致的地图操作受限问题,同时展示了如何按图样进行组件化开发,以便于管理内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

讲述一下事件穿透的需求,大家可以根据实际情况来考虑是否采用这种方式来制作页面,(项目中遇到了底部是地图,两侧面板,但是UI在设计的时候为了好看,会有很大的遮罩阴影部分,如果按照时间制作会导致地图可点击的区域变得很小,这个时候就考虑了使用事件穿透,即按照效果图还原了页面,也不影响地图操作),我们来看案例

按图上样式来组件化,每次我们只需管左右两侧的内容就可以了

<template>
  <div class="body">
    <header class="animated fadeInDown"> 头部 </header>
    <article>
      <div class="leftMain">
        <div @click="handleModel">
          <slot name="left"></slot>
        </div>
      </div>
      <div class="centerMain">
        <slot name="center"></slot>
      </div>
      <div class="rightMain">
        <div @click="handleModel">
          <slot name="right"></slot>
        </div>
      </div>
    </article>
    <footer class="animated fadeInUp">底部</footer>
  </div>
</template>

<script>
export default {
  data() {
    return {}
  },
  methods: {
    handleModel() {
      console.log("点击左右两侧模版,不触发地图事件")
    }
  }
}
</script>

<style scoped>
.body {
  width: 100%;
  height: 100%;
  position: relative;
}
.body header,
.body footer {
  width: 100%;
  color: #ffffff;
  text-align: center;
  position: absolute;
  left: 0;
  z-index: 1;
}

.body header {
  height: 8vh;
  line-height: 8vh;
  top: 0;
  pointer-events: none; /* 事件穿透 */
}
.body header::before {
  content: "";
  position: relative;
}
.body footer {
  height: 2vh;
  line-height: 2vh;
  bottom: 0;
  pointer-events: none;
}
.body article {
  width: 100%;
  height: 100%;
  position: relative;
}
.body article .leftMain,
.body article .rightMain {
  width: 6rem;
  height: calc(100% - 10vh);
  position: absolute;
  top: 8vh; /* 8vh 是头部的高度 */
  background-color: rgba(137, 43, 226, 0.4);
  z-index: 1;
  pointer-events: none; /* 事件穿透 */
  display: flex;
}
.body article .rightMain {
  justify-content: flex-end;
}
.body article .leftMain > div,
.body article .rightMain > div {
  width: 4rem;
  height: 100%;
  pointer-events: auto; /* 禁止事件穿透 */
  color: #ffffff;
  background-color: rgba(43, 226, 58, 0.37);
}

.body article .leftMain {
  left: 0.1rem;
  animation: bounceLeft 1s;
}
@keyframes bounceLeft {
  0% {
    left: -4rem;
  }
  100% {
    left: 0.1rem;
  }
}

.body article .rightMain {
  right: 0.1rem;
  animation: bounceRight 1s;
}
@keyframes bounceRight {
  0% {
    right: -4rem;
  }
  100% {
    right: 0.1rem;
  }
}

.body article .centerMain {
  width: 100%;
  height: 100%;
  position: relative;
}
.cash {
  position: fixed;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: -1;
}
</style>

使用组件

<template>
  <panel-model class="demo6">
    <template #left>
        测试ui设计地图上面会加遮罩,不影响地图操作,变成小手说明是可以操作地图
    </template>
    <template #center>
      <div class="gis" id="map" @click="mapHandle">
        放置地图
      </div>

    </template>
    <template #right>

    </template>
  </panel-model>
</template>

<script>
import panelModel from "./common/panelModel.vue";
export default {
  components: {
    panelModel,

  },
  data() {
    return {

    }
  },
  methods: {
    mapHandle() {
      console.log("点击地图")
    }
  }
}
</script>

<style scoped>
.gis {
  width: 100%;
  height: 100%;
  cursor: pointer;
}
</style>

完成,具体效果可以拿到自己项目中去实验一下,通过点击事件的操作来观察具体细节!

### QML 中 MouseArea 的事件穿透方法 在 QML 中,`MouseArea` 组件可以配置为允许鼠标事件传递到其下方的其他 `MouseArea` 或者组件。这通过设置 `propagateComposedEvents` 属性为 `true` 来实现[^1]。 当设置了 `propagateComposedEvents: true` 后,在处理特定鼠标事件(如点击、按下或释放)时,可以通过将 `mouse.accepted = false` 设置来让当前 `MouseArea` 不接受该事件,从而使事件继续传播给下层的对象。 然而,如果上方的 `MouseArea` 占用了整个区域并阻止了底层控件接收任何输入,则即使启用了上述机制也可能出现问题[^2]。为了确保底层页面能够正常接收到鼠标交互信号而不被阻挡,需谨慎设计各层之间的布局关系以及各自响应逻辑。 #### 示例代码展示如何正确配置 MouseArea 进行事件穿透: ```qml Item { width: 300; height: 300 Rectangle { // 底层矩形 id: bottomRect color: "lightblue" anchors.fill: parent MouseArea { anchors.fill: parent onClicked: console.log("Bottom rectangle clicked!") propagateComposedEvents: true // 允许组合事件传播 } } Rectangle { // 上层透明矩形 id: topTransparentRect opacity: 0.5 color: "gray" anchors.centerIn: parent width: 200; height: 200 MouseArea { anchors.fill: parent onClicked: { mouse.accepted = false // 让点击事件向下一层传递 // 可在此处添加额外操作... console.log("Top transparent rect was clicked but event passed through.") } } } } ``` 此示例展示了两个重叠放置的矩形,其中上层矩形部分透明以便观察效果。当用户单击上层矩形时,由于设置了 `mouse.accepted = false` 和 `propagateComposedEvents: true`,因此点击事件会被转发到底部矩形对应的 `MouseArea` 处理程序中去执行相应动作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值