简体   繁体   English

如果另一个div的高度大于值,则移动div(使用jQuery)

[英]Move a div if the height of another div is greater than value (with jquery)

See the image below. 参见下图。 This illustrates what I am trying to achieve. 这说明了我正在努力实现的目标。 I am sure it is possible with jquery but do not have the expertise to write it. 我敢肯定,jquery是可能的,但没有编写它的专业知识。

What i want is that if 'DIV A'(shown in green) has a height lower than a set PX value (for example if div A's height is smaller than 300px) then 'DIV B' (shown in red) should move the the second position. 我想要的是,如果“ DIV A”(以绿色显示)的高度低于设置的PX值(例如,如果div A的高度小于300px),则“ DIV B”(以红色显示)应移动第二个位置。 If DIV A's height is greater then that value then DIV A should simply remain in place and not be moved. 如果DIV A的高度大于该值,则DIV A应该简单地保持在原位而不被移动。

我想实现的目标

I started a fiddle here: http://jsfiddle.net/qwcpokdL/ 我在这里开始提琴: http : //jsfiddle.net/qwcpokdL/

example code: B 示例代码:B

<div class="right">
<div class="B">A</div>   
</div>

example css: 示例CSS:

.left, .right {width:49%; border: 1px solid #000; margin:1px; min-height:600px}
.left {float:left;}
.right {float:right;}
.somediv, .somediv2 {width:98%; height:125px; border:1px solid #ccc; margin:2px}

.A {background:red; width:98%%; height:150px; margin:2px}
.B {background:green; width:98%; height:100px; margin:2px}

Thanks in advance for help! 在此先感谢您的帮助!

$(function() {
  if($('#A').height() < 300){
    $('#A').remove().appendTo("#right");  
  }
});

Take a look at this : http://jsfiddle.net/banhjx1x/1/ 看看这个: http : //jsfiddle.net/banhjx1x/1/

Please check with the below code 请检查以下代码

$(document).ready(function() {
    var divAheight = $('.B').height();
    if(divAheight >= 100) {
        $( ".A" ).insertAfter( ".B" );
    }
});

You can use the following jQuery: 您可以使用以下jQuery:

function checkA(){
    if($('.B').height()<300){
        var a = $('.A');
        $('.A').remove();
        $('.B').after (a);
    }
    else {
        var a = $('.A');
        $('.A').remove();
        $('.right').append(a);
    }
}
$(document).ready(function(){
    checkA();
});
$( window ).resize(function() {
    checkA();
});

Also don't forget to add script tag for jQuery in your code. 同样不要忘记在代码中为jQuery添加脚本标签。

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

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