简体   繁体   English

更改jQuery的元素大小

[英]Change element size jQuery

I want to change element size by animate function. 我想通过动画功能更改元素大小。 But when I do this, all my page elements are slide down. 但是,当我这样做时,我所有的页面元素都向下滑动。 Is there any existing way to avoid sliding down elements of page? 是否有任何现有方法可以避免向下滑动页面元素? My code: 我的代码:

 $(".animated_blue_1").delay(800).animate({
                "height":230,
                "width":230
            },500, function () {
                $(".animated_blue_1").animate({
                    "height":200,
                    "width":200
                },500);
            });

If you have elements arranged like this: 如果您具有这样排列的元素:

+---+
| A |
+---+
+---+
| B |
+---+

...and you make A taller, what do you expect to happen to B? ...而您使A更高,您期望B发生什么? It needs to move down to make room, barring you telling it to do something else. 它需要向下移动以腾出空间,除非您告诉它做其他事情。

You could tell it to stay put by using absolute positioning: 您可以使用绝对定位来告诉它保持不变:

var b = $("B"); // Obviously make the selector something real
var pos = b.position();
b.css({
    position: "absolute",
    left: pos.left,
    top: pos.top
});

...but then of course A and B would overlap. ...但是当然,A和B会重叠。

Option A: They move down: 选项A:它们向下移动:

 var run = true; $("input[type=button]").click(function() { run = false; }); go(); function go() { $(".foo").animate({ width: 130, height: 130 }, 500).promise().then(function() { $(".foo").animate({ width: 100, height: 100 }, 500).promise().then(function() { if (run) { setTimeout(go, 200); } }); }); } 
 .foo { width: 100px; height: 100px; border: 1px solid black; } #a { background-color: #ee0; } 
 <input type="button" value="Stop"> <div id="a" class="foo">A</div> <div id="b" class="foo">B</div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

Option B: They overlap: 选项B:它们重叠:

 var run = true; $("input[type=button]").click(function() { run = false; }); var b = $("#b"); var pos = b.position(); b.css({ position: "absolute", left: pos.left, top: pos.top }); go(); function go() { $(".foo").animate({ width: 130, height: 130 }, 500).promise().then(function() { $(".foo").animate({ width: 100, height: 100 }, 500).promise().then(function() { if (run) { setTimeout(go, 200); } }); }); } 
 .foo { width: 100px; height: 100px; border: 1px solid black; } #a { background-color: #ee0; } 
 <input type="button" value="Stop"> <div id="a" class="foo">A</div> <div id="b" class="foo">B</div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

It means your selector is not specific to select that particular element. 这意味着选择器并不特定于选择该特定元素。 Check out which other elements have this animated_blue_1 class. 检查哪些其他元素具有此animated_blue_1类。 Try to provide an ID to the element so that you can use ID instead of Class to be more specific. 尝试为元素提供一个ID,以便您可以使用ID而不是Class来更具体。

If that is not the case you have to absolutely position your element, see TJ's answer. 如果不是这种情况,则必须绝对定位元素,请参阅TJ的答案。

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

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