Jquery,交换所有元素的z-index,除了在动作上选择一个

时间:2010-08-11 10:47:13

标签: jquery

我有一个问题,因为主题名称说:让我说我在网站上有4张照片的画廊,他们为这个div中的所有图像设置了z-index。无论如何,我想做出放大点击图像的动作,强制其他图像的z-index下降(因为我在黑色背景中使用淡入淡出作为图像视图的一部分)

提前致谢

<div id="main">
    <div id="masthead">
        <a href="#">click</a> </div>
    <div id="top_nav">
        <a href="http://www.google.pl">Start</a> <a href="http://www.google.pl">
        Opis</a> <a href="http://www.google.pl">Projekty</a>
        <a href="http://www.google.pl">Zdjecia</a>
        <a href="http://www.google.pl">Linki</a> <a href="http://www.google.pl">
        Kontakt</a> </div>
    <div id="content">
    <div id="textfield"> <p>Profesjonalny web design. </p>
    <span id="left"><a href="#">&#8592;</a></span>
    <span id="right"><a href="#">&#8594;</a></span>
    </div>
    <img id="second" src="model.jpg" alt="model" />
    <img id="third" src="model.jpg" alt="model" />
    <img id="fourth" src="model.jpg" alt="model" />
    <img id="first" src="model.jpg" alt="model" />
    </div>
    <div id="box"></div>
</div>
<div id="blackb"></div>

CSS

body {
    margin: 0;
    padding: 0;
    font-family: Verdana;
    background: silver url(bodygrad2.jpg) fixed;;
}
#main {
    width: 1280px;
    margin: 0 auto;
    padding-bottom: 10px;
}
#masthead {
    height: 40px;
    background: -webkit-gradient(linear, 0 0, 0 100%, from(gray), to(black));
    opacity: 0.3;
    margin-bottom: 4px;
    color: white;
    text-align: center;
    padding-top: 20px;
}
#masthead a{
    color: white;
    display: block;
    text-decoration: none;
}
#masthead a:hover{
    color: #FFFF66;
    display: block;
    text-decoration: none;
}

#top_nav {
    width: 100%;
    background: transparent;
    border-bottom: 1px solid #BFBB04;
}
#top_nav a {
    text-decoration: none;
    color: #a7a236;
    font-family: GraublauWeb; /* "Brush Script Std"; */
    font-size: 120%;
    padding: 0 5px;
}
#top_nav a:hover {
    color: #cfc944;
}
#content {
    height: 20px;
    width: 1px;
    background: gray url(content.jpg) 0 0 no-repeat;
}
#content div#textfield{
    display: none;
    position: absolute;
    top: 15%;
    right: 104px;
    width: 310px;
    height: 200px;
    border-bottom: 1px solid black;
    border-right: 1px solid black;
    background: #484B55;
    color: #CCCCCC;
    opacity: 0.5;
    z-index: 9;
}
#content div#textfield p{
    margin: 10px;
}
#content span#left{
    display: block;
    position: absolute;
    bottom: 0;
    left: 0;
}
#content span#right{
    display: block;
    position: absolute;
    bottom: 0;
    right: 0;
}
#content span a{
    text-decoration: none;
    color: #CCCCCC;
}
#content img {
    position: absolute;
    top: 50%;
    right: 50%;
    display: none;
    width: 160px;
    height: 116px;
    border: 2px solid white;
    z-index: 10;
}
#content img:hover {
    width: 164px;
    height: 120px;
    opacity: 0.9;
}
#content img#second {
    right: 35%;
}
#content img#third {
    right: 20%;
}
#content img#fourth {
    right: 5%;
}
#footer {
    position: fixed;
    bottom: 0;
    text-align: center;
    width: 100%;
    background: #ECE7E6;
    border-top: 1px solid #BBBFC8;
    font: 80% italic Arial,Verdana, sans-serif;
    color: silver;
    line-height: 1em;
    padding-bottom: 0.1em;
    z-index: 15;
}
#box {
    position: absolute;
    top: 40px;
    left: 0;
    width: 60px;
    height: 60px;
    background: red;
    opacity: 0.5;
    z-index: 6;
}
#blackb{
    display: none;
    position: absolute;
    top: 0;
    width: 1280px;
    height: 888px;
    background: black;
    opacity: 0.7;
    z-index: 9;
}

1 个答案:

答案 0 :(得分:0)

我们需要2个索引级别才能看起来正确,悬停始终位于上方并且点击其间,如下所示:

#content img:hover {
    z-index: 12;
}
#content img.clicked {
    z-index: 11;
}

然后点击即可应用此课程,并使用.addClass().removeClass()将其从以前的元素中移除,如下所示:

$(function() {
  $("#content img").click(function() {
    $(this).addClass('clicked').siblings('img').removeClass('clicked');
  });​​​​
});

You can give it a try here