Liam Shalon
Liam Shalon

Reputation: 442

Hover over text jQuery

I'm running into a problem, and I'm not quite sure how to solve it.

Currently, when one hovers over this particular picture, the opacity drops down (revealing a background color) and type appears. The problem, is that I want it to continue to act as if it is hovered over, even when they hover over the type.

Should I try changing the z-index? For some reason that didn't work...

JSFIDDLE http://jsfiddle.net/4jrUp/

HTML

<a class="bg" href="">
            <h1 class="first_title">FrameMonkey</h1>
            <img class="portfolio-item" src="http://blog.gettyimages.com/wp-content/uploads/2013/01/Siberian-Tiger-Running-Through-Snow-Tom-Brakefield-Getty-Images-200353826-001.jpg">
            <h3 class="first_description">A project that I've been working on since June - check back soon to see it in my portfolio!</h3>
        </a>

CSS

.portfolio-item:hover {
    overflow: hidden;
    z-index: 1;
    opacity:0.1;
}
.portfolio-item, .bg {
    height: 200px;
    width: 200px;
    left: 0px;
    border-radius:25px;
    position:absolute;
}
.bg {
    background-color: rgba(48, 48, 48, 0.9);
    top: 100px;
    display:inline-block;
}
.first_title {
    position: absolute;
    z-index: 100;
    color:white;
    left: 10px;
    font-size: 15pt;
    opacity: 0;
}
.first_description {
    top: 50px;
    position: absolute;
    z-index: 100;
    color:white;
    left: 10px;
    font-size: 12pt;
    opacity: 0;
}

Javascript

$(".portfolio-item").hover(function () {
    $(".first_description").css("opacity", "1");
    $(".first_title").css("opacity", "1");
}, function () {
    $(".first_description").css("opacity", "0");
    $(".first_title").css("opacity", "0");
});

Thanks!

Upvotes: 1

Views: 899

Answers (2)

ACruz
ACruz

Reputation: 101

Try adding the hover event to the .bg instead of .portfolio item, that way you can remove the z-index.

The jQuery change

    $(".bg").hover(function () {
       $(this).children(".first_description").css("opacity", "1");
       $(this).children(".first_title").css("opacity", "1");
    }, function () {
       $(this).children(".first_description").css("opacity", "0");
       $(this).children(".first_title").css("opacity", "0");
    });

And the CSS change

.first_title {
    position: absolute;
    //z-index: 100;
    color:white;
    left: 10px;
    font-size: 15pt;
    opacity: 0;
}
.first_description {
    top: 50px;
    position: absolute;
    //z-index: 100;
    color:white;
    left: 10px;
    font-size: 12pt;
    opacity: 0;
}

See this fiddle

Upvotes: 1

Josh Crozier
Josh Crozier

Reputation: 241198

You actually don't need jQuery for this.

UPDATED EXAMPLE HERE - ( I added an optional transition in there too.. )

Make the parent element, .bg, block level, and set the border-radius and dimensions on it. The important part is that it's relatively positioned so that the children elements are absolutely positioned, relative to the parent.

.bg {
    background-color: rgba(48, 48, 48, 0.9);
    position:relative;
    height:200px;
    width:200px;
    display:block;
    border-radius:25px;
    overflow:hidden;
}

The :hover part is quite simple actually, all you do is change the opacity.

.bg:hover .first_title, .bg:hover .first_description {
    opacity:1;
}
.bg:hover .portfolio-item {
    opacity:.1;
}

And the rest of the CSS:

.portfolio-item {
    height: 200px;
    width: 200px;
    position: absolute;
}

.first_title, .first_description {
    position: absolute;
    color: white;
    padding: 10px;
    width: 100%;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    z-index: 1;
    opacity: 0;
}

.first_title {
    font-size: 15pt;
}

.first_description {
    top: 50px;
    font-size: 12pt;
}

Upvotes: 2

Related Questions