使用jQuery的图片标题

时间:2012-12-21 09:58:04

标签: jquery html css

我的图像上方有一个边框,我想要一个在悬停图像时在它们之间滑动的标题。我只是不能按照我想要的方式工作,我拥有的是:

<!DOCTYPE html>
<html>
    <head>
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">

        <link href="style.css" rel="stylesheet" type="text/css" />

        <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js " type="text/javascript"></script>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js" type="text/javascript"></script>
        <script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.js" type="text/javascript"></script>

        <script type="text/javascript">
            $(document).ready(function(){
                $('.boxGrid').hover(function(){
                    $('.boxCaption', this).stop().animate({bottom:'0px'},{queue:false,duration:300});
                }, function() {
                    $('.boxCaption', this).stop().animate({bottom:'-121px'},{queue:false,duration:300});
                });
            });
        </script>
    </head>
    <body>
        <div class="wrapper">
            <div class="boxGrid"></div>
            <div class="buttonBack blogImg">
                <div class="boxCaption">
                    <h3>Top-Blog</h3>
                </div>
            </div>
        </div>
    </body>
</html>

CSS

.wrapper
{
    position: relative;
    width: 226px;
    height: 246px;
}

.boxGrid
{
    width: 226px;
    height: 246px;
    background-image: url(http://s10.postimage.org/z2cp1i70p/button_Border.png);
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 100;
}

.buttonBack
{
    position: absolute;
    top: 12px;
    left: 13px;
    border: 0px;
    width: 200px;
    height: 223px;
    z-index: 50;
    overflow: hidden;
}

.blogImg
{
    background-image: url(http://s10.postimage.org/jflfo4t8p/blog_Button.png);
}

.boxCaption
{
    position: absolute;  
    background: url(http://s10.postimage.org/c2an2wykp/caption.png);
    height: 121px;
    width: 100%;
    bottom: -121px;
    text-align: center;
}

.boxCaption h3
{
    font-size: 30px;
    color: #fff;
}

但是当我像这样对待html和jQuery时,它可以工作:

<!DOCTYPE html>
<html>
    <head>
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">

        <link href="style.css" rel="stylesheet" type="text/css" />

        <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js " type="text/javascript"></script>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js" type="text/javascript"></script>
        <script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.js" type="text/javascript"></script>

        <script type="text/javascript">
            $(document).ready(function(){
                $('.buttonBack').hover(function(){
                    $('.boxCaption', this).stop().animate({bottom:'0px'},{queue:false,duration:300});
                }, function() {
                    $('.boxCaption', this).stop().animate({bottom:'-121px'},{queue:false,duration:300});
                });
            });
        </script>
    </head>
    <body>
        <div class="wrapper">
            <!--<div class="boxGrid"></div>-->
            <div class="buttonBack blogImg">
                <div class="boxCaption">
                    <h3>Top-Blog</h3>
                </div>
            </div>
        </div>
    </body>
</html>

我所做的是评论div.boxGrid并让jQuery函数在div.buttonBack的位置上执行而不是div.boxGrid。只有那不是我想要的,我希望div.boxGrid超越所有,所以我对悬停有this效果。

我已经制作了一个jsFiddle,here它不起作用,但like this它有效......

任何帮助都会非常感激,我会把头发拉出来。

1 个答案:

答案 0 :(得分:5)

您的boxGrid班级比z-index班级高buttonBack。修复它,然后尝试...

请参阅链接:http://jsfiddle.net/wBjT3/5/