原码笔记

原码笔记

css不常见属性之pointer-events的使用方法

小诸哥 0

MDN 上介绍为 css 属性指定在什么情况下 (如果有) 某个特定的图形元素可以成为鼠标事件的 target。

pointer-events 属性值有:

  1. /* Keyword values */
  2. pointer-events: auto;
  3. pointer-events: none;
  4. pointer-events: visiblePainted; /* SVG only */
  5. pointer-events: visibleFill; /* SVG only */
  6. pointer-events: visibleStroke; /* SVG only */
  7. pointer-events: visible; /* SVG only */
  8. pointer-events: painted; /* SVG only */
  9. pointer-events: fill; /* SVG only */
  10. pointer-events: stroke; /* SVG only */
  11. pointer-events: all; /* SVG only */
  12.  
  13. /* Global values */
  14. pointer-events: inherit;
  15. pointer-events: initial;
  16. pointer-events: unset;
  17.  

其中默认属性为 auto。 当值为none表示鼠标事件“穿透”该元素并且指定该元素“下面”的任何东西。

使用场景

抛却只适用于svg的值,说一说 none 的使用场景。 mdn上的解释不太好理解。 网友叙帝利 给出了一种使用场景。

我这里还有一中使用场景是 当用div元素通过css样式模拟按钮时,可以使用 pointer-event: none 模拟button禁止点击。

比如:

  1. // html
  2. <div class="point" onclick="alert('ok')提交申请</div>
  3. .point {
  4.      width: 1.8rem;
  5.      height: .44rem;
  6.      margin: 0 auto;
  7.      margin-top: 0.8rem;
  8.      text-align: center;
  9.      line-height: .44rem;
  10.      border-radius: 22px;
  11.      color: #fff;
  12.      background-color: rgba(67,76,94,.43);
  13.      pointer-events: none;
  14. }

此时该div的样子,如果不设置pointer-events: none; 只是样子不可点击,点击还是会触发事件响应的。 加上则不会响应click事件了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

标签: 常见 使用 方法