Only detect click event on pseudo-element

前端 未结 10 1851
长情又很酷
长情又很酷 2020-11-22 08:00

My code is:

p {
    position: relative;
    background-color: blue;
}

p:before {
    content: \'\';
    position: absolute;
    left:100%;
    width: 10px;
         


        
10条回答
  •  感情败类
    2020-11-22 08:48

    Short Answer:

    I did it. I wrote a function for dynamic usage for all the little people out there...

    Working example which displays on the page

    Working example logging to the console

    Long Answer:

    ...Still did it.

    It took me awhile to do it, since a psuedo element is not really on the page. While some of the answers above work in SOME scenarios, they ALL fail to be both dynamic and work in a scenario in which an element is both unexpected in size and position(such as absolute positioned elements overlaying a portion of the parent element). Mine does not.

    Usage:

    //some element selector and a click event...plain js works here too
    $("div").click(function() {
        //returns an object {before: true/false, after: true/false}
        psuedoClick(this);
    
        //returns true/false
        psuedoClick(this).before;
    
        //returns true/false
        psuedoClick(this).after;
    
    });
    

    How it works:

    It grabs the height, width, top, and left positions(based on the position away from the edge of the window) of the parent element and grabs the height, width, top, and left positions(based on the edge of the parent container) and compares those values to determine where the psuedo element is on the screen.

    It then compares where the mouse is. As long as the mouse is in the newly created variable range then it returns true.

    Note:

    It is wise to make the parent element RELATIVE positioned. If you have an absolute positioned psuedo element, this function will only work if it is positioned based on the parent's dimensions(so the parent has to be relative...maybe sticky or fixed would work too....I dont know).

    Code:

    function psuedoClick(parentElem) {
    
        var beforeClicked,
          afterClicked;
    
      var parentLeft = parseInt(parentElem.getBoundingClientRect().left, 10),
          parentTop = parseInt(parentElem.getBoundingClientRect().top, 10);
    
      var parentWidth = parseInt(window.getComputedStyle(parentElem).width, 10),
          parentHeight = parseInt(window.getComputedStyle(parentElem).height, 10);
    
      var before = window.getComputedStyle(parentElem, ':before');
    
      var beforeStart = parentLeft + (parseInt(before.getPropertyValue("left"), 10)),
          beforeEnd = beforeStart + parseInt(before.width, 10);
    
      var beforeYStart = parentTop + (parseInt(before.getPropertyValue("top"), 10)),
          beforeYEnd = beforeYStart + parseInt(before.height, 10);
    
      var after = window.getComputedStyle(parentElem, ':after');
    
      var afterStart = parentLeft + (parseInt(after.getPropertyValue("left"), 10)),
          afterEnd = afterStart + parseInt(after.width, 10);
    
      var afterYStart = parentTop + (parseInt(after.getPropertyValue("top"), 10)),
          afterYEnd = afterYStart + parseInt(after.height, 10);
    
      var mouseX = event.clientX,
          mouseY = event.clientY;
    
      beforeClicked = (mouseX >= beforeStart && mouseX <= beforeEnd && mouseY >= beforeYStart && mouseY <= beforeYEnd ? true : false);
    
      afterClicked = (mouseX >= afterStart && mouseX <= afterEnd && mouseY >= afterYStart && mouseY <= afterYEnd ? true : false);
    
      return {
        "before" : beforeClicked,
        "after"  : afterClicked
    
      };      
    
    }
    

    Support:

    I dont know....it looks like ie is dumb and likes to return auto as a computed value sometimes. IT SEEMS TO WORK WELL IN ALL BROWSERS IF DIMENSIONS ARE SET IN CSS. So...set your height and width on your psuedo elements and only move them with top and left. I recommend using it on things that you are okay with it not working on. Like an animation or something. Chrome works...as usual.

提交回复
热议问题