简体   繁体   English

指针事件:无但捕获点击

[英]pointer-events:none but capture click

is it possible to allow only click and disable all other pointer-events是否可以只允许单击并禁用所有其他指针事件

top-layer has a search bar顶层有一个搜索栏

bottom-layer has a word cloud底层有词云

I have set pointer-events:none on top-layer, so all words in the word cloud can be hovered over even if they are below the search bar.我在顶层设置了pointer-events:none ,所以词云中的所有词都可以悬停在搜索栏下方。

but I want the click event on the input text to be enabled, so that when the user wants to type in something, he can.但我希望启用输入文本上的单击事件,以便当用户想要输入内容时,他可以。

在此处输入图像描述

Here is a related fiddle这是一个相关的小提琴

The text is behind the input, but it should be hoverable, the input is above the text, but it should be focusable using mouse, to allow the user to type in.文本在输入后面,但它应该是可悬停的,输入在文本上方,但它应该可以使用鼠标聚焦,以允许用户输入。

Note: it looks like a placeholder thing, but it is not.注意:它看起来像一个占位符的东西,但它不是。 please see the original image to see what i am trying to achieve.请查看原始图像以了解我想要实现的目标。

Because pointer-events is blocking interactieve events(click, hover, mouseenter etc.) it would be only accessible with javascript (through focus for example).因为pointer-events正在阻止交互事件(单击、悬停、鼠标输入等),它只能通过 javascript 访问(例如通过焦点)。

It's maybe not the best solution but it will do I guess in your case?这可能不是最好的解决方案,但我猜你的情况会是这样吗?

(function($) {
  var focus = false;
  $(document).on('click', function(e) {
    console.log(focus);
    e.preventDefault();
    if (focus) {
      focus = false;
      $('.cl1').trigger('blur');
    } else {
      focus = true;
      $('.cl1').focus();
    }
  });
})(jQuery);

a fiddle with this working solution: https://jsfiddle.net/cob02bpv/1/摆弄这个工作解决方案: https ://jsfiddle.net/cob02bpv/1/

Edit: you could check on which element was clicked, only elements under the input will be tricky.编辑:您可以检查单击了哪个元素,只有输入下的元素会很棘手。

If its not the solution the only one would be to calculate the coordinates from the input box and check where the click event was triggered.如果它不是解决方案,唯一的解决方案是从输入框中计算坐标并检查触发click事件的位置。 But still you would have problems for your elements under the input box.但是您仍然会在输入框下的元素遇到问题。

I think this should work.我认为这应该有效。 Listening to the click event on the parent container, getting the event.clientX and event.clientY values to check if they are within the bounds of the input element.监听父容器上的点击事件,获取event.clientXevent.clientY值以检查它们是否在input元素的范围内。 If so, you can then set the focus to the input element.如果是这样,您可以将焦点设置到input元素。 You can still determine if one of the random words underneath the input element has been clicked.您仍然可以确定是否单击了input元素下方的随机单词之一。

 var d = document, c = d.getElementsByClassName('container').item(0), inp = d.createElement('input'), a = 50, i = 0; /* | get the clientBoundingRect of the input element | and if the mouse x and mouse y positions are within | the bounds set the focus on to the input element. ------------------------------------------------------------- */ function inpClickHndl (evt) { var inpRect = inp.getBoundingClientRect(), x = evt.clientX, y = evt.clientY, l = inpRect.left, w = l + inpRect.width, t = inpRect.top, h = t + inpRect.height; if (x >= l && x <= w && y >= t && y <= h) { inp.focus(); } } /* | ignore this, it's just to create the random words. ------------------------------------------------------------- */ function wordClickHndl (evt) { this.style.color = "yellow"; } for (i; i < a; i++) { var p = d.createElement('p'), t = d.createTextNode('Random Word'); p.appendChild(t); p.addEventListener('click', wordClickHndl, false); p.style.position = 'absolute'; p.style.top = Math.floor(Math.random() * (window.innerHeight - 80)) + -40 + 'px'; p.style.left = Math.floor(Math.random() * (window.innerWidth - 80)) + -40 + 'px'; p.style.fontSize = Math.floor(Math.random() * (38 - 8)) + 8 + 'px'; p.style.fontWeight = 'bold'; c.appendChild(p); } inp.setAttribute('type', 'text'); c.appendChild(inp); /*------------------------------------------------------------- */ // add a click handler to your parent element. c.addEventListener('click', inpClickHndl, false);
 body { margin: 0; font-family: sans-serif; } .container { position: relative; height: 100vh; width: 100vw; background-color: #1a1a1a; } .container p { color: green; } .container p:hover { color: red; cursor: pointer; } .container input { position: absolute; top: 50%; left: calc(50% - 85px); pointer-events:none; opacity: .75; }
 <div class="container"></div>

Just add the pointer-events CSS3 property, setting as initial to the text box.只需添加pointer-events CSS3 属性,设置为文本框的initial值。 Using !important is recommended also, so because another property can pass that added.还建议使用!important ,因为另一个属性可以传递添加的内容。

In CSS3 :CSS3中:

pointer-events:initial !important

In JavaScript :JavaScript 中

document.querySelector('input[type=text]').style.pointerEvents="initial"

If layout permits it you could use the adjacent sibling combinator, given that you re-order the elements:如果布局允许,您可以使用相邻的兄弟组合器,前提是您重新排序元素:

Tested on FireFox and Chrome.在 FireFox 和 Chrome 上测试。

 .backText:hover { color : red; } .cl1 { opacity : 0.7; position : absolute; } /* adjacent sibling combinator */ .wrap-input:hover + div { color : red; } .cl1:focus { opacity : 1; }
 <div> <div class="wrap-input"> <input type="text" class="cl1" value="aa" /> </div> <div class="backText"> Some text to be hovered even if input is above </div> </div>


Other options其他选项

The following only works on FireFox.以下仅适用于 FireFox。 Tested on Chrome and it flickers when pointer is moved, - but could perhaps give some ideas.在 Chrome 上测试过,当指针移动时它会闪烁,但也许可以给出一些想法。

Instead of setting pointer-events on the input-element directly, set it using the :hover pseudo-class.不要直接在输入元素上设置pointer-events ,而是使用:hover伪类设置它。

Example, based on your fiddle:例如,根据您的小提琴:

 .cl1 { position : absolute; top : 0px; opacity : 0.7; height : 30px; } /* Move pointer-events here */ .cl1:hover { pointer-events : none; } .cl1:focus { opacity : 1; } .backText:hover { color : red; }
 <div> <div class="backText"> Some text to be hovered even if input is above </div> <div> <input type="text" class="cl1" /> </div> </div>

I had the same problem in the past.我过去也有同样的问题。 I managed to solve it in a bit tricky way, just added parent division with button inside and applied click event on parent element and pointer-events: none to child button element.我设法以一种有点棘手的方式解决了它,只是在里面添加了带有button的父分区,并在父元素和指针事件上应用了click event pointer-events: none到子按钮元素。

将输入的z-index设置为比带有pointer-events: 'none'的元素更高的值不是最简单的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM