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

Increase element width by percentage using jQuery

本文关键字:元素 增加 百分比 jQuery 使用      更新时间:2023-09-26

这个答案由Varinder工作得很好,但我想得到的元素宽度设置在像素和增加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.
    });
});

我想有几种方法可以做到这一点,试试;

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

尝试将currentWidth替换为var newWidth = currentWidth * 1.1;

你只需要设置新宽度为旧宽度+旧宽度的10%:

var newWidth = currentWidth + (currentWidth * 0.1);