[英]Get current width and height of div
I have two div's that can re-size after clicking on the plus or minus buttons.我有两个 div 可以在单击加号或减号按钮后重新调整大小。 Not the current situation has the words 'Width' and 'Height' displayed in between.
目前的情况不是在中间显示“宽度”和“高度”这两个词。
Instead of the words I would like to show the current size.我想显示当前大小而不是单词。 So if I press twice on the plus (making the div 20px's bigger) I want to show that by visually seeing the number grow with 20px's.
因此,如果我在加号上按两次(使 div 增大 20 像素),我想通过直观地看到数字随 20 像素的增长来显示这一点。
$(".SmallerWidth").click(function () {
$("#div1").animate({
'width': '-=10px'
});
$("#div2").animate({
'width': '-=10px'
});
});
Please see the feedle: https://jsfiddle.net/p9x68vnf/2/请参阅提要: https ://jsfiddle.net/p9x68vnf/2/
$("#div1").animate({'width':'+=10px'}).html($('#div1').width());
$("#div2").animate({'width':'+=10px'}).html($('#div2').width());
UPD:更新:
There is what actually you want:实际上你想要的是:
https://jsfiddle.net/p9x68vnf/5/ https://jsfiddle.net/p9x68vnf/5/
You should put text between span or any other tag.您应该在跨度或任何其他标签之间放置文本。 And display it's width and height.
并显示它的宽度和高度。
$(".SmallerWidth").click(function(){ $("#div1").animate({'width':'-=10px'}); $("#div2").animate({'width':'-=10px'}); disp(); }); $(".BiggerWidth").click(function(){ $("#div1").animate({'width':'+=10px'}); $("#div2").animate({'width':'+=10px'}); disp(); }); $(".SmallerHeight").click(function(){ $("#div1").animate({'height':'-=10px'}); $("#div2").animate({'height':'-=10px'}); disp(); }); $(".BiggerHeight").click(function(){ $("#div1").animate({'height':'+=10px'}); $("#div2").animate({'height':'+=10px'}); disp(); }); function disp() { $(".wi").text("First Width " + $('#div1').width() + " Second Width " + $('#div2').width()); $(".he").text("First Height " + $('#div1').height() + " Second Height " + $('#div2').height()); }
#div1{ width:100px; height:100px; background:#39ACC7; } #div2{ width:50px; height:50px; background: #B2C2D1; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="div1">First </div> <div id="div2">Second </div> </br></br> <button class="SmallerWidth"> - </button> <span class="wi"> Width (this is where I want to show the current size) </span> <button class="BiggerWidth"> + </button></br> <button class="SmallerHeight"> - </button> <span class="he"> Height (this is where I want to show the current size) </span> <button class="BiggerHeight"> + </button>
If you want to know the current width and height of the element, you can use width() and height() functions:如果想知道元素当前的宽度和高度,可以使用 width() 和 height() 函数:
var width = $('#div1').width();
var height= $('#div1').height();
$(".SmallerWidth").click(function(){
$("#div1").animate({'width':'-=10px'});
$("#div2").animate({'width':'-=10px'});
resize();
});
$(".BiggerWidth").click(function(){
$("#div1").animate({'width':'+=10px'});
$("#div2").animate({'width':'+=10px'});
resize();
});
$(".SmallerHeight").click(function(){
$("#div1").animate({'height':'-=10px'});
$("#div2").animate({'height':'-=10px'});
resize();
});
$(".BiggerHeight").click(function(){
$("#div1").animate({'height':'+=10px'});
$("#div2").animate({'height':'+=10px'});
resize();
});
function resize(){
$('#width-total').empty().append("First Width:"+$("#div1").width()+"px Second Width:"+$("#div2").width()+"px");
$('#height-total').empty().append("First height:"+$("#div1").height()+"px Second height:"+$("#div2").height()+"px");
}
$(".SmallerWidth").click(function(){
$("#div1").animate({'width':'-=10px'}).html($("#div1").width());
$("#div2").animate({'width':'-=10px'}).html($("#div2").width());
});
$(".BiggerWidth").click(function(){
$("#div1").animate({'width':'+=10px'}).html($("#div1").width());
$("#div2").animate({'width':'+=10px'}).html($("#div2").width());
});
$(".SmallerHeight").click(function(){
$("#div1").animate({'height':'-=10px'}).html($("#div1").height());
$("#div2").animate({'height':'-=10px'}).html($("#div2").height());
});
$(".BiggerHeight").click(function(){
$("#div1").animate({'height':'+=10px'}).html($("#div1").height());
$("#div2").animate({'height':'+=10px'}).html($("#div2").height());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.