使用jQuery增加元素宽度百分比

时间:2015-08-20 22:10:27

标签: javascript jquery width percentage

This answerVarinder效果很好,但我希望获得以像素为单位的元素宽度并将其增加10%。

$(window).load(function() {
    $(".ml").each(function() { // this is kindof sugary way of doing for loop over array of elements - which is $(".ml")
         var $this = $(this); // the current jQueried .ml element
         var currentWidth = $this.width(); // get width of current element, width is a jQuery method: https://api.jquery.com/width/
         var newWidth = currentWidth + 5; // increment the width
         $this.width(newWidth); // pass in the new width as the parameter.
    });
});

3 个答案:

答案 0 :(得分:2)

我认为有几种方法可以做到,试试;

 var currentWidth = $this.width();

 var newWidth = currentWidth * 1.1; //increment by 10%
 //OR
 var newWidth = currentWidth + ( currentWidth * 10) / 100; //increment by 10%

希望这有帮助。

答案 1 :(得分:2)

您只需将新宽度设置为旧宽度+旧宽度的10%:

var newWidth = currentWidth + (currentWidth * 0.1);

答案 2 :(得分:1)

尝试用var newWidth = currentWidth * 1.1;

替换currentWidth