当用户将鼠标悬停在 div 上时,如何更改其 z-index?
我目前正在尝试创建松散堆叠的宝丽来照片形式的按钮,我想获取用户将鼠标悬停在哪一张宝丽来照片上以到达堆的顶部。
我试图通过一个简单的方法来实现这一点:
#polaroid a hover{
z-index:15;
}
我认为从理论上讲,这可以将宝丽来照片放在堆的顶部,因为当前“最高”宝丽来照片的 z-index:13;
然而,这根本不起作用,我认为我做错了什么,可能需要一些 javascript 来实现这一点。
我当前的代码是...
HTML
<a id="polaroid_home" href="index.html"></a>
<a id="polaroid_about" href="index.html"></a>
<a id="polaroid_portfolio" href="index.html"></a>
<a id="polaroid_contact" href="index.html"></a>
我已将所有这些设置为链接到index.html 只是为了测试。
CSS
#polaroid_home{
position:absolute;
width:175px;
height:215px;
left:670px;
top:140px;
background-image:url(../Images/polaroid-home.png);
background-repeat:no-repeat;
z-index:13;
}
#polaroid_home a:hover{
z-index:15;
}
#polaroid_about{
position:absolute;
width:175px;
height:215px;
left:765px;
top:175px;
background-image:url(../Images/polaroid-about.png);
background-repeat:no-repeat;
z-index:12;
}
#polaroid_about a:hover{
z-index:15;
}
#polaroid_portfolio{
position:absolute;
width:175px;
height:215px;
left:620px;
top:255px;
background-image:url(../Images/polaroid-portfolio.png);
background-repeat:no-repeat;
z-index:10;
}
#polaroid_portfolio a:hover{
z-index:15;
}
#polaroid_contact{
position:absolute;
width:210px;
height:235px;
left:740px;
top:310px;
background-image:url(../Images/polaroid-contact.png);
background-repeat:no-repeat;
z-index:11;
}
#polaroid_contact a:hover{
z-index:15;
}
我在页面的其他地方使用 JavaScript 并且正在使用这个:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
我不知道如何使用 JavaScript,所以我把它放在以防万一它很重要。
我是否做了一些明显错误的事情或者这个方法不起作用?
有什么方法可以通过 CSS 实现这一点吗?如果没有,那么 JavaScript 会是什么?
I'm currently trying to create buttons which are in the form of loosely stacked polaroid pictures and I would like to get which ever polaroid the user mouses over to come to the top of the pile.
I have tried to achieve this with a simple:
#polaroid a hover{
z-index:15;
}
I thought that this could, theoretically, bring the polaroid to the top of the pile, as the current "highest" polaroid is z-index:13;
However, this doesn't work at all, I think I'm doing something wrong and may require some javascript to achieve this.
My current code is...
HTML
<a id="polaroid_home" href="index.html"></a>
<a id="polaroid_about" href="index.html"></a>
<a id="polaroid_portfolio" href="index.html"></a>
<a id="polaroid_contact" href="index.html"></a>
I've set all these to link to index.html just to test.
CSS
#polaroid_home{
position:absolute;
width:175px;
height:215px;
left:670px;
top:140px;
background-image:url(../Images/polaroid-home.png);
background-repeat:no-repeat;
z-index:13;
}
#polaroid_home a:hover{
z-index:15;
}
#polaroid_about{
position:absolute;
width:175px;
height:215px;
left:765px;
top:175px;
background-image:url(../Images/polaroid-about.png);
background-repeat:no-repeat;
z-index:12;
}
#polaroid_about a:hover{
z-index:15;
}
#polaroid_portfolio{
position:absolute;
width:175px;
height:215px;
left:620px;
top:255px;
background-image:url(../Images/polaroid-portfolio.png);
background-repeat:no-repeat;
z-index:10;
}
#polaroid_portfolio a:hover{
z-index:15;
}
#polaroid_contact{
position:absolute;
width:210px;
height:235px;
left:740px;
top:310px;
background-image:url(../Images/polaroid-contact.png);
background-repeat:no-repeat;
z-index:11;
}
#polaroid_contact a:hover{
z-index:15;
}
I'm using JavaScript elsewhere on the page and am using this:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
I have no knowledge of how to use JavaScript, so I put that just incase it is important.
Have I done something obviously wrong or does this method just not work?
Are there any ways I can achieve this through CSS and if not, what would the JavaScript for it be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些想法:
position:relative
才能为position:absolute
和创建坐标系code>z-index
来使用。A couple of thoughts:
position: relative
in order to create a coordinate system for theposition: absolute
andz-index
to work with.删除
' a'
,例如,'#poloid_contact a:hover'
看起来像'#poloid_contact:hover'
。Remove
' a'
where that, for example,'#polaroid_contact a:hover'
would look like'#polaroid_contact:hover'
.