将鼠标悬停在文本jQuery上

时间:2014-03-08 17:43:38

标签: javascript jquery html css hover

我遇到了问题,我不太确定如何解决它。

目前,当一个人悬停在这张特定的图片上时,不透明度会下降(显示背景颜色)并出现类型。问题在于,我希望它继续表现得好像它被徘徊,即使它们悬停在类型上。

我应该尝试更改z-index吗?出于某种原因,这些都无效......

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");
});

谢谢!

2 个答案:

答案 0 :(得分:2)

你实际上需要jQuery。

UPDATED EXAMPLE HERE - (我在那里添加了一个可选的过渡...)

创建父元素.bgblock级别,并在其上设置border-radius和维度。重要的是它相对定位,以便子元素相对于父元素绝对定位。

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

实际上:hover部分非常简单,您只需更改opacity

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

其余的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;
}

答案 1 :(得分:1)

尝试将悬停事件添加到.bg而不是.portfolio项目,这样就可以删除z-index。

jQuery更改

    $(".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");
    });

CSS改变

.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