jQuery animate 和 z-index/相对定位的问题

发布于 2024-11-02 17:02:27 字数 281 浏览 4 评论 0原文

我试图使用position:relative 来让一个div 包含鼠标悬停时的其他div。不幸的是它只适用于最后一个 div。如果您将鼠标悬停在其他 div 上,它们只会覆盖在它们之前声明的 div。

代码在这里: http://jsfiddle.net/YuKaR/3/

绝对定位工作正常,但不幸的是我不能对这个特定的应用程序使用绝对定位。

有人知道我做错了什么吗?感谢您抽出时间。

I'm trying to get a div to encompass the other divs on mouseover using position: relative. Unfortunately it only works for the last div. If you mouseover the others, they only cover the divs that are declared before them.

The code is here: http://jsfiddle.net/YuKaR/3/

Absolute positioning works fine, but unfortunately I cannot use absolute positioning for this particular app.

Anyone know what I'm doing wrong? Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸印温柔 2024-11-09 17:02:27

相对定位的问题在于,位置是相对于其正常位置的,这意味着如果您调整中间元素的大小,浏览器将移动并重新排列其后面的所有内容。

需要进行一些更改才能使其发挥作用。如果您想使用相对定位,则必须将调整大小的 div 包装在固定大小的容器中,这样当它们调整大小时就不会破坏元素流。你的div有150px的宽度和高度,固定大小的容器必须足够大才能容纳它,假设默认的盒子模型是150px + 10px*2 padding + 1px*2 border = 172px。由于元素流是由容器控制的,我将 margin 移到了 css 中的容器。

通过将它们包装在额外的固定大小的 div 中,元素流将永远不会改变,调整大小的 div 只会穿过容器的边缘,与其他容器重叠(溢出:可见)。

我还更改了您的 z 索引逻辑,因为您现在需要设置容器的 z 索引(这将应用于所有子元素)。默认情况下,所有内容的 z 索引均为 2。当 div 大小调整回其原始状态时,我在动画结束后使用 .animate() 上的回调函数将其容器的 z 索引设置回 2。当开始调整大小时,所有容器都将重置为 z-index 2,以防有一个容器仍在动画回到其原始状态,当前调整大小的 div 容器将设置为 z-index 3 以使其显示在所有其他容器之上。

http://jsfiddle.net/x34d3/

HTML 标记:

 <div id="main" style="position:relative;z-index:1;">

     <div class="container"><div id="lefttop" class="resizer">left top</div></div>
     <div class="container"><div id="righttop" class="resizer">right top</div></div>
     <p style="clear:both;"></p>
     <div class="container"><div id="leftbottom" class="resizer">left bottom</div></div>
     <div class="container"><div id="rightbottom" class="resizer">right bottom</div></div>

 </div>

CSS:

.resizer { position:relative; border: 1px solid #000000; padding:10px; margin:0px; width:150px; height:150px; }

.container { position:relative; padding:0px; margin:8px; float:left; z-index: 2; width:172px; height:172px; }

javascript:

$(function(){
     $(".resizer").mouseover(function() {
         $(".container").css('z-index' , '2');
         $(this).parent().css('z-index' , '3');
         if(this.id == "lefttop"){
             aoptions = {width: "340px", height: "340px", backgroundColor: "#CCCCCC", left: '0', top: '0'}
         }else if(this.id == "righttop"){
             aoptions = {width: "340px", height: "340px", backgroundColor: "#CCCCCC", left: '-=190', top: '0'}
         }else if(this.id == "leftbottom"){
             aoptions = {width: "340px", height: "340px", backgroundColor: "#CCCCCC", left: '0', top: '-=190'}
         }else if(this.id == "rightbottom"){
             aoptions = {width: "340px", height: "340px", backgroundColor: "#CCCCCC", left: '-=190', top: '-=190'}
         }
         $(this).css('z-index' , '99').animate(aoptions, 800);
     }).mouseout(function(){
         if(this.id == "lefttop"){
             aoptions = {width: "150px",    height: "150px", backgroundColor: "#FFFFFF", left: '0', top: '0'}
         }else if(this.id == "righttop"){
             aoptions = {width: "150px",    height: "150px", backgroundColor: "#FFFFFF", left: '+=190'}
         }else if(this.id == "leftbottom"){
             aoptions = {width: "150px",    height: "150px", backgroundColor: "#FFFFFF", left: '0', top: '+=190'}
         }else if(this.id == "rightbottom"){
             aoptions = {width: "150px",    height: "150px", backgroundColor: "#FFFFFF", left: '+=190', top: '+=190'}
         }
         $(this).animate(aoptions, 800, function(){
             $(this).parent().css('z-index' , '2');
         });
     });
 });

The problem with relative positioning is that positions are relative to their normal position, meaning if you resize an element in the middle, the browser will move and re-flow everything that comes after it.

A few changes needed to be made to make it work. If you want to use relative positioning, you have to wrap your resizing divs in fixed size containers, so when they're resizing they won't break the element flow. Your divs have 150px width and height, the fixed sized container must be big enough to hold it, assuming the default box-model it's 150px + 10px*2 padding + 1px*2 border = 172px. As the element flow is controled by the containers, I moved margin to the container in the css.

By wrapping them in an additional fixed sized div, element flow will never change, your resizing divs will just bleed through the edges of the container, overlapping the other containers (overflow:visible).

I also changed your z-index logic, as you need to set the z-index for the containers now (which will apply to all child elements). By default everything has z-index of 2. When the div is resized back to its original state, I set its container's z-index back to 2 after the animation, using a callback function on .animate(). When resizing starts, all containers are reset to z-index 2 in case there is one still animating back to its original state, the currently resizing div's container is set to z-index 3 to make it appear on top of all the others.

http://jsfiddle.net/x34d3/

HTML markup:

 <div id="main" style="position:relative;z-index:1;">

     <div class="container"><div id="lefttop" class="resizer">left top</div></div>
     <div class="container"><div id="righttop" class="resizer">right top</div></div>
     <p style="clear:both;"></p>
     <div class="container"><div id="leftbottom" class="resizer">left bottom</div></div>
     <div class="container"><div id="rightbottom" class="resizer">right bottom</div></div>

 </div>

CSS:

.resizer { position:relative; border: 1px solid #000000; padding:10px; margin:0px; width:150px; height:150px; }

.container { position:relative; padding:0px; margin:8px; float:left; z-index: 2; width:172px; height:172px; }

javascript:

$(function(){
     $(".resizer").mouseover(function() {
         $(".container").css('z-index' , '2');
         $(this).parent().css('z-index' , '3');
         if(this.id == "lefttop"){
             aoptions = {width: "340px", height: "340px", backgroundColor: "#CCCCCC", left: '0', top: '0'}
         }else if(this.id == "righttop"){
             aoptions = {width: "340px", height: "340px", backgroundColor: "#CCCCCC", left: '-=190', top: '0'}
         }else if(this.id == "leftbottom"){
             aoptions = {width: "340px", height: "340px", backgroundColor: "#CCCCCC", left: '0', top: '-=190'}
         }else if(this.id == "rightbottom"){
             aoptions = {width: "340px", height: "340px", backgroundColor: "#CCCCCC", left: '-=190', top: '-=190'}
         }
         $(this).css('z-index' , '99').animate(aoptions, 800);
     }).mouseout(function(){
         if(this.id == "lefttop"){
             aoptions = {width: "150px",    height: "150px", backgroundColor: "#FFFFFF", left: '0', top: '0'}
         }else if(this.id == "righttop"){
             aoptions = {width: "150px",    height: "150px", backgroundColor: "#FFFFFF", left: '+=190'}
         }else if(this.id == "leftbottom"){
             aoptions = {width: "150px",    height: "150px", backgroundColor: "#FFFFFF", left: '0', top: '+=190'}
         }else if(this.id == "rightbottom"){
             aoptions = {width: "150px",    height: "150px", backgroundColor: "#FFFFFF", left: '+=190', top: '+=190'}
         }
         $(this).animate(aoptions, 800, function(){
             $(this).parent().css('z-index' , '2');
         });
     });
 });
潜移默化 2024-11-09 17:02:27

我花了一段时间,但我终于弄清楚了。看看这个工作 jsFiddle 演示 :

HTML:

 <div id="main">

     <div class="overlay"><div id="lefttop">left top</div></div>
     <div class="overlay"><div id="righttop">right top</div></div>
     <p></p>
     <div class="overlay"><div id="leftbottom">left bottom</div></div>
     <div class="overlay"><div id="rightbottom">right bottom</div></div>

 </div>

CSS:

* {

    margin: 0px;
    padding: 0px;
    border: none;
    z-index: -1;
}

p { clear: both; }

#main {

    float: left;
    margin: 50px;
    background: black;

}

#main div {

    position: relative;
    float: left;

    height: 150px;
    width: 150px;

}

.overlay {

    height: 0;
    overflow: visible;
    position: relative;
    z-index: 99;

}

#lefttop { background: yellow; }
#righttop { background: green; }
#leftbottom { background: red; }
#rightbottom { background: blue; }

jQuery:

var $divs = $("div", ".overlay"),
    optionsOver = {

         width: "300px",
         height: "300px"

    },
    optionsOut = {

         width: "150px",
         height: "150px"

    };

$divs.hover(function() {

    $divs
        .stop(true,true)
        .parent()
        .css({"z-index" : "1"});

    if (this.id == "lefttop") {

        optionsOver.left = "0px";
        optionsOver.top = "0px";

    } else if (this.id == "righttop") {

        optionsOver.left = "-=150px";
        optionsOver.top = "0px";

    } else if (this.id == "leftbottom") {

        optionsOver.left = "0px";
        optionsOver.top= "-=150px";

    } else if (this.id == "rightbottom") {

        optionsOver.left = "-=150px";
        optionsOver.top= "-=150px";

    }

    var $this = $(this);
    $this.stop(true,true);
    $this.parent().css({"z-index" : "99"});
    $this.animate(optionsOver, 400);

 }, function() {

    if (this.id == "lefttop") {

        optionsOut.left = "0px";
        optionsOut.top = "0px";

    } else if (this.id == "righttop") {

        optionsOut.left = "+=150px";
        optionsOut.top = "0px";

    } else if (this.id == "leftbottom") {

        optionsOut.left = "0px";
        optionsOut.top= "+=150px";

    } else if (this.id == "rightbottom") {

        optionsOut.left= "+=150px";
        optionsOut.top = "+=150px";

    }

    $(this)
        .stop(true,true)
        .animate(optionsOut, 400, function() {
            $divs.parent().css({"z-index" : "1"});
        });

 });

Took me a while, but I finally figured it out. Take a look at this working jsFiddle demo:

HTML:

 <div id="main">

     <div class="overlay"><div id="lefttop">left top</div></div>
     <div class="overlay"><div id="righttop">right top</div></div>
     <p></p>
     <div class="overlay"><div id="leftbottom">left bottom</div></div>
     <div class="overlay"><div id="rightbottom">right bottom</div></div>

 </div>

CSS:

* {

    margin: 0px;
    padding: 0px;
    border: none;
    z-index: -1;
}

p { clear: both; }

#main {

    float: left;
    margin: 50px;
    background: black;

}

#main div {

    position: relative;
    float: left;

    height: 150px;
    width: 150px;

}

.overlay {

    height: 0;
    overflow: visible;
    position: relative;
    z-index: 99;

}

#lefttop { background: yellow; }
#righttop { background: green; }
#leftbottom { background: red; }
#rightbottom { background: blue; }

jQuery:

var $divs = $("div", ".overlay"),
    optionsOver = {

         width: "300px",
         height: "300px"

    },
    optionsOut = {

         width: "150px",
         height: "150px"

    };

$divs.hover(function() {

    $divs
        .stop(true,true)
        .parent()
        .css({"z-index" : "1"});

    if (this.id == "lefttop") {

        optionsOver.left = "0px";
        optionsOver.top = "0px";

    } else if (this.id == "righttop") {

        optionsOver.left = "-=150px";
        optionsOver.top = "0px";

    } else if (this.id == "leftbottom") {

        optionsOver.left = "0px";
        optionsOver.top= "-=150px";

    } else if (this.id == "rightbottom") {

        optionsOver.left = "-=150px";
        optionsOver.top= "-=150px";

    }

    var $this = $(this);
    $this.stop(true,true);
    $this.parent().css({"z-index" : "99"});
    $this.animate(optionsOver, 400);

 }, function() {

    if (this.id == "lefttop") {

        optionsOut.left = "0px";
        optionsOut.top = "0px";

    } else if (this.id == "righttop") {

        optionsOut.left = "+=150px";
        optionsOut.top = "0px";

    } else if (this.id == "leftbottom") {

        optionsOut.left = "0px";
        optionsOut.top= "+=150px";

    } else if (this.id == "rightbottom") {

        optionsOut.left= "+=150px";
        optionsOut.top = "+=150px";

    }

    $(this)
        .stop(true,true)
        .animate(optionsOut, 400, function() {
            $divs.parent().css({"z-index" : "1"});
        });

 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文