使用 jQuery 按百分比增加元素宽度
Increase element width by percentage using jQuery
This answer by 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);
This answer by 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);