简体   繁体   English

jQuery animate在滑动之前将div推到下面

[英]jquery animate pushes div below before sliding

I am using some jquery to slide a legend in and out next to a map. 我正在使用一些jquery在地图旁边滑动图例。 I have used this code before but now I am using it within a responsive framework so I am changing some things to percentages rather than pixels widths. 我以前使用过此代码,但现在在响应式框架中使用它,因此我将某些内容更改为百分比而不是像素宽度。 Perhaps I have some things out of order in my script but the div containing the legend drops below the map while it animates back and forth. 也许我的脚本中有些东西乱了,但是包含图例的div在来回动画时落在地图下方。

Here's my script: 这是我的脚本:

$(".hideLegendRight").click(function () {
    $(this).hide();
    $(".label").hide();
    $(".zoomTo").hide();
    $(".legendMenu").hide();
    $("#legendMap").animate({
        width: "0%"
    }, 500);
    $(".buttonsMap").animate({
        left: "25"
    }, 500);
    $("#wrapperMap").animate({
        width: "100%"
    }, 500, function () {
        $(".showLegendRight").show();
    });
    google.maps.event.trigger(map, 'resize');
});

$(".showLegendRight").click(function () {
    $(this).hide();
    $(".buttonsMap").animate({
        left: "0"
    }, 500);
    $("#legendMap").animate({
        width: "35%"
    }, 500);
    $("#wrapperMap").animate({
        width: "65%"
    }, 500, function () {
        $(".hideLegendRight").show();
        $(".legendMenu").show();
        $(".zoomTo").show();
        $(".label").show();
    });
    google.maps.event.trigger(map, 'resize');
});

And here's my jsfiddle 这是我的jsfiddle

You are seeing this issue because of how html styling works. 由于html样式的工作原理,您正在看到此问题。 You won't fix this problem by merely changing some script value. 您不会仅通过更改一些脚本值来解决此问题。 Quite literally your issue is that as your #wrapperMap div grows, it doesn't leave enough room for you #legendMap div to be displayed. 从字面上看,您的问题完全是随着#wrapperMap div的增长,它没有足够的空间显示#legendMap div。 Not only that, but everything is automatically defaulting to a relative position. 不仅如此,所有内容都会自动默认为相对位置。 So when the #wrapperMap grows it displaces the #legendMap so that it is below it. 因此,当#wrapperMap增大时,它会移动#legendMap使其位于其下方。 Here's a good StackOverflow answer that outlines your problem. 这是一个很好的StackOverflow答案,概述了您的问题。

Make div stay in top of parent 使div停留在父项的顶部

Pretty much you want to make the parent relative and the child absolute with a couple nuances to make it show up in the right spot. 您几乎想通过一些细微差别使父母亲和孩子成为绝对亲戚,使它出现在正确的位置。 Here's the css I either added or altered to fix your problem. 这是我为解决您的问题而添加或更改的CSS。

.grid_12 {
  position:  relative;
}

#legendMap {
  position:  absolute;
  top:       0px;
  right:     0px;
  width:     35%;
  float:right;
  height:458px;
  background-color:#404040;
  z-index: -1;
  margin-bottom:20px;
  overflow:hidden;

} }

fixed JSfiddle 固定JSfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM