4

demo

jQuery:

$('#main:after').click(function(e){
    e.preventDefault();
    $('#main').hide();
});

css:

#main{
    background-color: red;
    width: 100%;
    height: 200px;
}
#main:after{
    position: absolute;
    bottom: 200px;
    background-color: blue;
    width: 20px;
    height: 20px;
    content: " ";
}

How to hide red div when clicked to blue div?

Community
  • 1
  • 1
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231

1 Answers1

4

UPDATE

I've had a little play and reckon I've come up with something that solves this issue for the original provided code.

Essentially I have assigned the same :after CSS to a "dummy" class and then create and destroy an element with the dummy class on the fly. Between the create and destroy we are able to get the necessary dimensions (width, height, positioning). Finally we can compare these values to the click co-ordinates...

DEMO: http://jsfiddle.net/gvee/gNDCV/6/

CSS

#main {
    background-color: red;
    width: 100%;
    height: 200px;
    position: relative;
}
#main:after, .mainafter {
    position: absolute;
    bottom: -100px;
    right: 50%;
    background-color: blue;
    width: 40%;
    height: 20px;
    content:" ";
}

JQuery

$('#main').click(function (e) {
    var pseudoElement = $('<div></div>').addClass('mainafter');
    $(this).append(pseudoElement);
    var w = pseudoElement.width();
    var h = pseudoElement.height();
    var x = pseudoElement.position().left;
    var y = pseudoElement.position().top;
    pseudoElement.remove();
    
    if (e.pageY - $(this).position().top  > y     &&
        e.pageY - $(this).position().top  < y + h &&
        e.pageX - $(this).position().left > x     &&
        e.pageX - $(this).position().left < x + w
       ) {
        alert('Not red');
    }

});

If statement illustrated:

Illustration of if statement

PageX is the horizontal co-ordinate of the cursor. X is the co-ordinate of the left-hand edge of the blue box. W is the width of the blue box.

Therefore we can work out the right-hand edge of the blue box by simple addition: X+W.

The same logic can be applied on the vertical co-ordinates (top=Y, bottom=Y+H).

The if statement in our JQuery above checks if PageX falls between the left and right-hand edges of the blue box and that PageY is between the top and bottom. i.e. is the cursor in the blue box!


There is a [kludgy] workaround to your provided code (where the :after content is positioned below it's container)...

Work out the co-ordinate of the mouse click and see if that exceeds the height of the #main div...

DEMO: http://jsfiddle.net/gvee/gNDCV/3/

CSS

#main {
    background-color: red;
    width: 100%;
    height: 200px;
    position: relative;
    margin-bottom: 20px; /* to clear content:after */
}
#main:after {
    position: absolute;
    bottom: -20px; /* -(this.height) */
    background-color: blue;
    width: 20px;
    height: 20px;
    content:" ";
}

JQuery

$('#main').click(function (e) {
    if (e.pageY - $(this).offset().top > $(this).height()) {
        alert('Not red');
    }
});
Community
  • 1
  • 1
gvee
  • 16,732
  • 35
  • 50
  • It's so difficult to find cursor pointer where the blue div may be positioned anywhere. – Bhojendra Rauniyar Sep 02 '13 at 12:08
  • @C-Link my answer is a workaround based on the code you provided. You omitted this key detail so I have not catered for it! – gvee Sep 02 '13 at 12:10
  • @C-Link Check this [**fiddle**](http://jsfiddle.net/gNDCV/4/). (_just an update to gvee's implementation_). – Mr_Green Sep 02 '13 at 12:21
  • @Mr_Green - I played around with something like that as well but I think the biggest issue is that you have to supply the `:after` position/offsets at run time as they cannot be derived (as we cannot access the pseudo-selector). – gvee Sep 02 '13 at 12:23
  • 1
    @C-Link see my updates... any good to you? – gvee Sep 02 '13 at 13:49
  • Very good. But could you please explain the if condition used in your code. – Bhojendra Rauniyar Sep 03 '13 at 02:44
  • @C-Link basically I generate an actual DOM element that shares the same CSS properties as the `:after` pseudo-selector. This allows us to grab the position (top and left co-ordinates) and size (width and height). We can then destroy the created object (as it has served its purpose). **The `if` condition checks if the mouse cursor is between the co-ordinates we grabbed earlier.** – gvee Sep 05 '13 at 08:24
  • I'm not understanding how the if condition check for that.. can you visualize the answer, please. – Bhojendra Rauniyar Sep 05 '13 at 08:28
  • @C-Link answer updated (again). – gvee Sep 05 '13 at 09:19
  • Thanks and +1 for visual illustration. – Bhojendra Rauniyar Sep 05 '13 at 10:04
  • How will this work when you have an element with a higher z-index that occupies the same plane as the pseudo element? Also, if we're creating an element dynamically just to find out the coordinates of the pseudo element, why use the pseudo element in the first place, why not simply use the dynamically created element (as they're essentially the same)? – designcise May 02 '16 at 11:59