Click through div to underlying elements

后端 未结 16 2054
逝去的感伤
逝去的感伤 2020-11-21 06:19

I have a div that has background:transparent, along with border. Underneath this div, I have more elements.

Curre

相关标签:
16条回答
  • 2020-11-21 06:36

    it doesn't work that way. the work around is to manually check the coordinates of the mouse click against the area occupied by each element.

    area occupied by an element can found found by 1. getting the location of the element with respect to the top left of the page, and 2. the width and the height. a library like jQuery makes this pretty simple, although it can be done in plain js. adding an event handler for mousemove on the document object will provide continuous updates of the mouse position from the top and left of the page. deciding if the mouse is over any given object consists of checking if the mouse position is between the left, right, top and bottom edges of an element.

    0 讨论(0)
  • 2020-11-21 06:36

    Just wrap a tag around all the HTML extract, for example

    <a href="/categories/1">
      <img alt="test1" class="img-responsive" src="/assets/photo.jpg" />
      <div class="caption bg-orange">
        <h2>
          test1
        </h2>
      </div>
    </a>
    

    in my example my caption class has hover effects, that with pointer-events:none; you just will lose

    wrapping the content will keep your hover effects and you can click in all the picture, div included, regards!

    0 讨论(0)
  • 2020-11-21 06:39

    Allowing the user to click through a div to the underlying element depends on the browser. All modern browsers, including Firefox, Chrome, Safari, and Opera, understand pointer-events:none.

    For IE, it depends on the background. If the background is transparent, clickthrough works without you needing to do anything. On the other hand, for something like background:white; opacity:0; filter:Alpha(opacity=0);, IE needs manual event forwarding.

    See a JSFiddle test and CanIUse pointer events.

    0 讨论(0)
  • 2020-11-21 06:42

    You can place an AP overlay like...

    #overlay {
      position: absolute;
      top: -79px;
      left: -60px;
      height: 80px;
      width: 380px;
      z-index: 2;
      background: url(fake.gif);
    }
    
    <div id="overlay"></div>
    

    just put it over where you dont want ie cliked. Works in all.

    0 讨论(0)
  • 2020-11-21 06:46

    I currently work with canvas speech balloons. But because the balloon with the pointer is wrapped in a div, some links under it aren't click able anymore. I cant use extjs in this case. See basic example for my speech balloon tutorial requires HTML5

    So I decided to collect all link coordinates from inside the balloons in an array.

    var clickarray=[];
    function getcoo(thatdiv){
             thatdiv.find(".link").each(function(){
                     var offset=$(this).offset();           
                     clickarray.unshift([(offset.left),
                                         (offset.top),
                                         (offset.left+$(this).width()),
                                         (offset.top+$(this).height()),
                                         ($(this).attr('name')),
                                         1]);
                                         });
             }
    

    I call this function on each (new) balloon. It grabs the coordinates of the left/top and right/down corners of a link.class - additionally the name attribute for what to do if someone clicks in that coordinates and I loved to set a 1 which means that it wasn't clicked jet. And unshift this array to the clickarray. You could use push too.

    To work with that array:

    $("body").click(function(event){
              event.preventDefault();//if it is a a-tag
              var x=event.pageX;
              var y=event.pageY;
              var job="";
              for(var i in clickarray){
                  if(x>=clickarray[i][0] && x<=clickarray[i][2] && y>=clickarray[i][1] && y<=clickarray[i][3] && clickarray[i][5]==1){
                     job=clickarray[i][4];
                     clickarray[i][5]=0;//set to allready clicked
                     break;
                    }
                 }
              if(job.length>0){   
                 // --do some thing with the job --
                }
              });
    

    This function proofs the coordinates of a body click event or whether it was already clicked and returns the name attribute. I think it is not necessary to go deeper, but you see it is not that complicate. Hope in was enlish...

    0 讨论(0)
  • 2020-11-21 06:47

    Yes, you CAN force overlapping layers to pass through (ignore) click events.
    PLUS you CAN have specific children excluded from this behavior...

    You can do this, using pointer-events

    pointer-events influences the reaction to click-, tap-, scroll- und hover events.


    In a layer that should ignore / pass-through mentioned events you set

    pointer-events: none; 
    

    Children of that unresponsive layer that need to react mouse / tap events again need:

    pointer-events: auto; 
    

    That second part is very helpful if you work with multiple overlapping div layers (probably some parents being transparent), where you need to be able to click on child elements and only that child elements.

    Example usage:

        <style>
            .parent {
                pointer-events:none;        
            }
            .child {
                pointer-events:auto;
            }
        </style>
        
        <div class="parent">
          <a href="#">I'm unresponsive</a>
          <a href="#" class="child">I'm clickable again, wohoo !</a>
        </div>
    
    0 讨论(0)
提交回复
热议问题