2015-08-20 48 views
2

This answerVarinder效果很好,但我想獲得以像素爲單位設置的元素寬度並將其增加10%。使用jQuery增加元素寬度百分比

$(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. 
    }); 
}); 

回答

2

我認爲有幾種方法可以做到這一點,試試;

var currentWidth = $this.width(); 

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

希望這會有所幫助。

1

嘗試var newWidth = currentWidth * 1.1;

2

更換currentWidth你只需要新的寬度設置爲舊的寬度舊寬度+ 10%:

var newWidth = currentWidth + (currentWidth * 0.1);