4

My objective is to create an area that displays clickable text content, has hover that changes the whole area, and does NOT spaz out when the whole div or text is rolled over. I want to do this with css.

For example, I want a div, say 500 x 500 px, inside of this div are p's and a's that need to be clickable. It works normally. Something like:

<div style="width:500px; height:500px;">
    <p> xtext <a>xlink</a> </p> 
    <p> xtext <a>xlink</a> </p>
</div>

And then, I want a div on top, that has a rollover (hover) function that covers the whole first div, hazing out the whole div with a transparent color, until rolled over, when the transparency is set to 0.0, and appears gone. But I want the links from the first div to be clickable (to do this, I give this second div pointer-events:none). I place this code* inside the first div, so in total looks like this:

<div style="width:500px; height:500px;">
*<div class="divhover" style="width:500px; height:500px; position:absolute;">
</div>
    <p> xtext <a>xlink</a> </p> 
    <p> xtext <a>xlink</a> </p>
</div>

Styling with css class:

div.divhover { 
    background-color: hsla(0, 100%, 100%, 0.5); }
div.divhover:hover {
    background-color: hsla(0, 100%, 100%, 0.0); 
    pointer-events: none; }

Without pointer-events:none, this works like a normal hover function: if your mouse is not on the div area, there is a white layer of 0.5 transparency. If your mouse is on it, the transparency is 0.0 and looks as if it is uncovered. This, however, does not allow the text and links elements to be clicked.

With pointer-events:none, the text and links are clickable but it results in the hover to deactivate when a cursor is over the p or a elements. This makes the whole div spaz out and blink if the cursor moves around, and rapid blinking if the cursor is hovering on a link! I don't want this!

I hope all you with much more css/html know-how can help me (I don't know much). Before getting this far, I tried setting z-index:-1 on .divhover:hover to have the text and links in the first div be clickable. I also tried using position:absolute moving the second div off the page (left:2000px). These both resulted in the same situation, as they are only different ways to do the same thing with pointer-events.

Here is a jsfiddle where you can see the blinking:

http://jsfiddle.net/6wU8X/

Although it's not apparent here, if you take out pointer-events:none, the links and text would not be clickable.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
PLCcory
  • 153
  • 3
  • 12

2 Answers2

3

The flicker you're seeing is caused by setting pointer-events:none; on the hover property.

You're telling the browser to ignore all pointer events, even the ones that trigger hover states. So the moment you activate the hover css you deactivate it, causing the flicker (it's updated by mouse pixel movement).

CSS:

.divhover:hover {
  background-color: hsla(0, 100%, 100%, 0.0); 
}

Update: If your ultimate goal is to simply grey out the text until you hover over it, then you might try this:

Working Demo

HTML:

<div class="hover">    
    <p> xtext <a>xlink</a> </p> 
    <p> xtext <a>xlink</a> </p>
</div>

CSS:

.hover {
    width:500px; 
    height:500px;
    background-color:#ccc; 
    color:#333;
    opacity: .5;
}

.hover:hover{
    opacity: 1;
}

I think you may be going about this effect in an odd way. You can add a hover state on the containing element and toggle the opacity.

badAdviceGuy
  • 2,060
  • 1
  • 16
  • 12
  • Yeah, that works correctly, but it doesn't allow the text and link, p and a elements, to be clicked. The pointer-events:none is required to allow clicking past the "divhover" classified div. This is the problem I'm having =[. – PLCcory Feb 07 '14 at 02:15
  • 1
    Okay I think I understand the issue better, you want the text / bg to be grayed out until the user hovers over the text? – badAdviceGuy Feb 07 '14 at 02:27
  • Yes, sorry to be unclear. I want it to be hazed/grayed out until hovered, then to hide itself. Here is the example of how it spazzes out: http://jsfiddle.net/6wU8X/ . If the pointer-events:none is not set, the items in the div cannot be clicked on/higlighted – PLCcory Feb 07 '14 at 04:09
  • Ah! Yes, it seems yet again my lack of knowledge lead to a convoluted question. This is what I am looking for. Although it's stunning I seem to have made a blunder, I'm happy to know it's such an easy thing! – PLCcory Feb 07 '14 at 20:44
  • Actually, this works for what I want now, but it only changes the opacity. If the div on div thing worked, the color could be set for the overlayed div. An image could also work with the div thing, or even a div filled with stuff. – PLCcory Feb 07 '14 at 20:53
1

I can't come up with a way to do this with CSS. If you're open to JavaScript and jQuery, here is a fall back if no one comes up with a CSS solution.

Add a class to the parent div:

<div class="parent" style="width:500px; height:500px; background-color:#ccc; color:#333;z-index:10">

<div class="divhover" style="width:500px; height:500px; position:absolute;">
</div>

    <p> xtext <a>xlink</a> </p> 
    <p> xtext <a>xlink</a> </p>
</div>

Include jquery and add the following script:

 $(document).ready(function(){

    $(".divhover").mouseover(function(){
        console.log("Over");
        $(this).hide();
        }
    );

    $(".parent").mouseleave(function(){
        console.log("out");
       $(".divhover").show(); 
    });

});

See it in action here: http://jsfiddle.net/6wU8X/2/

Hopefully some one comes up with a CSS solution though!

Kara
  • 6,115
  • 16
  • 50
  • 57
Jon P
  • 19,442
  • 8
  • 49
  • 72
  • Thank you very much. I would still love a css solution, because I have a feeling it's possible. This, however; is a working solution I appreciate, and may use for something I need later. =] – PLCcory Feb 07 '14 at 05:48