-1

Okay, so if you could go to;

http://jsfiddle.net/aled2305/UzM7U/4/

you will see a blue circle, when you take your mouse over a red square will appear to the right. Now that all works how I want, but I would like the red box to stay when the user then takes their mouse over it.

Now if you take your mouse over where the red square shows, it will show because of

.down:hover
{
    opacity:100;
}

So is there a way to get the red square to stay when a mouse is over it, but only when it is activated by hovering over the blue circle.

Thanks in advance

Aled

UPDATE

Sorry forgot to say I would like the red square to hide once the mouse has been taken off.

Thanks

aled2305
  • 43
  • 1
  • 10
  • 5
    I definitely don't see a 'red square' in the example. – Josh Crozier Nov 13 '13 at 22:17
  • 2
    Your Fiddle has no JavaScript, and no CSS `hover` selectors - I think you missed something there... – RichieHindle Nov 13 '13 at 22:19
  • Sorry guys, I forgot to update the code after editing...link updated – aled2305 Nov 13 '13 at 22:21
  • 2
    You will need javascript as far as I know on this. Here is a fiddle with a basic function to do this, http://jsfiddle.net/Josh_Powell/UzM7U/7/. – Josh Powell Nov 13 '13 at 22:28
  • please all, see this link on W3C http://www.w3.org/wiki/CSS/Properties/opacity, specially the part => "Any values outside the range 0.0 (fully transparent) to 1.0 (fully opaque) will be clamped to this range." There is no opacity: 100 – Mark Nov 13 '13 at 22:39
  • @Mark Good point! I didn't even notice that at first. You should be using 0/1 for hide/show. – Josh Powell Nov 13 '13 at 22:42

3 Answers3

3

My demo will fade-in the square upon hovering the circle. From there, when you hover over the square, it will stay opaque. After you move off the circle or square, the square will fade-out.

The trick to getting this to work is setting 2 different transitions for the opacity, height, and width properties of the square, one for hover ON and one for hover OFF, as well as adding a delay attribute to the transition. The reason for transitioning height and width is that it will prevent you from being able to hover over the square without first hovering over the circle.

Here are the default settings of the square: opacity: 0, height: 0, and width: 0.

For the hover ON transition, you want opacity to fade-in over 1 second, but to be able to see that, the height and width values need to be 40px prior to the fade-in transition. To make that happen, you need to set a delay of 0 seconds on the height and width transitions. This way, the square is immediately at its max dimensions, which allows the fade-in transition to be seen.

The hover OFF transition will revert back to the default settings. What you want to have happen is for the opacity to ease-out over 1 second while at the same time keeping the values of height and width at 40px. Otherwise, height and width would instantly revert back 0 and you would not be able to see the fade-out transition. To make that happen you need to set a delay of 1 second on the height and width transitions. In doing that, the opacity eases out over 1 second and because of the 1 second delay on height and width, at that point, height and width will revert back 0.

See the jsFiddle demo


HTML

<div id="gravatar">
    <div id="circle"></div>
    <div id="square"></div>
</div>

CSS

#gravatar
{
    float: left;
}

#circle
{
    background-color: blue;
    float: left;
    height: 40px; 
    width: 40px;
    border-radius: 20px;
}

#square
{
    background-color: red;
    float: left;
    margin-left: 10px;
    height: 0;
    width: 0;
    opacity: 0;

    /* hover OFF */
    -webkit-transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
    -moz-transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
    -o-transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
    -ms-transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
    transition: opacity 1s 0 ease-in-out, height 0s 1s ease, width 0s 1s ease;
}

#square:hover,
#circle:hover + #square
{
    height: 40px;
    width: 40px;
    opacity: 1;

    /* hover ON */
    -webkit-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
    -moz-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
    -o-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
    -ms-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
    transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
}


EDIT

The OP left a comment stating that adding contents to the square prevents the transitions from working correctly. I corrected it by adding overflow: hidden to the square.

I also added other styles to the CSS to account for the anchors the OP added.

See the jsFiddle demo


HTML

<div id="gravatar">
    <div id="circle"></div>
    <div id="square">
        <a href="http://techyoucation.com/?page_id=156">Profile Details</a> 
        <a href="http://techyoucation.com/?page_id=59">Account Details</a>
    </div>
</div>

CSS

#gravatar
{
    float: left;
}

#circle
{
    background-color: blue;
    float: left;
    height: 40px; 
    width: 40px;
    border-radius: 20px;
}

#square
{
    background-color: #2D3538;
    float:left;
    overflow: hidden;
    margin-left: 10px;
    height: 0;
    width: 0;
    opacity: 0;
        
    /* hover OFF */
    -webkit-transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
    -moz-transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
    -o-transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
    -ms-transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
    transition: opacity 1s 0 ease-in-out, height 0 1s ease, width 0 1s ease;
}

#square > a
{
    display: block;
    font: 15px Verdana;
    color: #FFF;
    text-decoration: none;
    height: 15px;
    line-height: 15px;
    margin: 10px;
}

#square > a:last-child
{
    margin-top: 0;
}

#square > a:hover
{
    text-decoration: underline;
}

#square:hover,
#circle:hover + #square
{
    height: 60px;
    width: 135px;
    opacity: 1;
    
    /* hover ON */
    -webkit-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
    -moz-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
    -o-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
    -ms-transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
    transition: opacity 1s 0 ease-in-out, height 0 0 ease, width 0 0 ease;
}
Community
  • 1
  • 1
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
  • 1+ .. better than the other answers. – Josh Crozier Nov 13 '13 at 23:42
  • 1
    @Scott Nearly perfect, just is there any change you can make it fade out like MathiasaurusRex ones? Thanks and sorry JoshC for replying to you instead of Scott! – aled2305 Nov 14 '13 at 00:12
  • @Scott Sorry I have read the post but can't get it to fade out? – aled2305 Nov 14 '13 at 07:34
  • @aled2305 - There you go! I got it all to work as you wanted. I've updated my answer with the full solution. Read my answer for the reasoning behind the changes. – Code Maverick Nov 14 '13 at 16:00
  • @Scott Thanks you so much... You wouldn't believe how grateful I am. Thanks :) – aled2305 Nov 14 '13 at 17:26
  • @Scott Right I understand I'm probably annoying you now. But adding link's to the square makes it not work. If you hover over the circle and see where the square appears. Now bring your mouse in to where the square will appear from the right. It appears without going over the circle. See http://jsfiddle.net/aled2305/twa7F/5/ – aled2305 Nov 14 '13 at 18:00
  • @aled2305 - The main thing you need is `overflow:hidden` on the square. I went ahead and reworked your CSS and came up with this: http://jsfiddle.net/8jqvN/. – Code Maverick Nov 14 '13 at 18:33
  • @JoshC Absolutely amazing, got it working perfectly on my site too... Thanks So much :) – aled2305 Nov 14 '13 at 22:50
  • @aled2305 - You're welcome. Glad you got it working just like you wanted! – Code Maverick Nov 15 '13 at 04:21
2

Here's a Fiddle Using JS that follows the following logic:

  1. Red Box shows when hovering on Blue Circle
  2. Red Box hides when mouse leaves Reds

You can get that effect by adding a little JQuery and modifying your CSS:

JQuery:

$(".gravatar").hover(
  function () {
    $(".down").addClass('hoverDown');
  }
);

$(".down").mouseleave(
  function () {
    $(".down").removeClass('hoverDown');
  }
);

Here's the CSS:

.gravatar {
    background-color:blue;
    float: left;
    margin-top: 2px;
    margin-right: 10px;
    margin-left:  2px;
    border-radius: 20px;
    width: 40px;
    height: 40px; 
}

.down
{
    float:left;
    width: 40px;
    height: 40px;
    background-color:Red;
    opacity:0;
    -webkit-transition: all 1s ease-in-out;
    -moz-transition: all 1s ease-in-out;
    -o-transition: all 1s ease-in-out;
    -ms-transition: all 1s ease-in-out;
    transition: all 1s ease-in-out;


    }
.hoverDown
{
    opacity:1;

}
Mathias Rechtzigel
  • 3,596
  • 1
  • 18
  • 37
  • 1
    This works, but I don't see where the OP mentioned he wanted jQuery. – Code Maverick Nov 13 '13 at 22:31
  • @Scott Your right scott I was hopping to just use CSS and HTML... Is this possible? – aled2305 Nov 13 '13 at 22:34
  • And Sorry forgot to say I would like the red square to hide once the mouse has been taken off. – aled2305 Nov 13 '13 at 22:36
  • one does not simply assume that opacity 100 excists – Mark Nov 13 '13 at 22:41
  • @aled2305 Wait just a second, if you wanted the effect to go away when the mouse is off of the circle then what is the matter with the `:hover`? – Josh Powell Nov 13 '13 at 22:43
  • @MathiasaurusRex Yeah I am a little lost now. – Josh Powell Nov 13 '13 at 22:45
  • @aled2305 I updated my answer so that the following logic takes place: Red Box shows when hovering on Blue Circle, Red Box hides when mouse leaves Red Box – Mathias Rechtzigel Nov 13 '13 at 23:02
  • Okay so in the code I uploaded. You would hover over the blue circle and the red box would show. If you hovered over where the red box WILL show it appeared. I would like you to have to hover over the blue circle, and if there was a delay before it want, you could get your mouse to the red square before it hid, so that you could click on the links that will be in it. So lie this http://jsfiddle.net/aled2305/caaxA/1/ if you could get the mouse to the red box in time it should stay (but with face in and fade out) – aled2305 Nov 13 '13 at 23:03
  • @MathiasaurusRex that is nearly perfect, but could you have it so that if I leave the blue circle and don't move the mouse into the red square within 3 seconds it would hide anyway? - Thanks – aled2305 Nov 13 '13 at 23:06
  • @MathiasaurusRex Yes but is there any way it could be `
    ` instead of `
    ` or is that not possible? (I need it to be like that) sorry :(
    – aled2305 Nov 13 '13 at 23:14
  • @MathiasaurusRex Don't worry got it. Thanks for all your work :) – aled2305 Nov 13 '13 at 23:45
0

well for mouse in and mouse out action you can use the mousenter mouseleave jquery function

$(".gravatar").mouseenter(
  function () {
    $(".down").addClass('hoverDown');
  }
);
$(".gravatar").mouseleave(
  function () {
    $(".down").removeClass('hoverDown');
  }
);

working fiddle:

http://jsfiddle.net/UzM7U/9/