Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用jQuery向div添加x%宽度_Php - Fatal编程技术网

Php 如何使用jQuery向div添加x%宽度

Php 如何使用jQuery向div添加x%宽度,php,Php,我有一种“加载条”,初始宽度为10% <div id="loading_bar"></div> 我如何写,使它增加20%,而不是20像素 该值不是一个常数,每次都会改变。所以每次我都需要根据指定的值增加宽度 有人能帮忙吗。。。谢谢您需要添加包含元素宽度的20%,然后计算出它是什么。根据您的HTML结构,以下函数应该可以工作: var newWidth = $('#loading_bar').parent().width(); $("#loading_bar").css(

我有一种“加载条”,初始宽度为10%

<div id="loading_bar"></div>
我如何写,使它增加20%,而不是20像素

该值不是一个常数,每次都会改变。所以每次我都需要根据指定的值增加宽度

有人能帮忙吗。。。谢谢

您需要添加包含元素宽度的20%,然后计算出它是什么。根据您的HTML结构,以下函数应该可以工作:

var newWidth = $('#loading_bar').parent().width();
$("#loading_bar").css("width", "+="+(Math.round(newWidth * 0.2)));

这里,
0.2
代表20%。您可以使用
20/100

轻松地显式设置百分比值。您可以将加载的值存储在某些属性中,如rel

 $("#loading_bar").css("width", +=20+"%");
var loaded = $('#loading_bar').attr('rel');
var width = loaded + 20;
$("#loading_bar").css('width', width+'%');
$('#loading_bar').attr('rel',width);
像这样的

function increasePercentage($element, percentage) {

    $element.css("width", "+="+(Math.round($element.parent().width() * (percentage / 100))));

}

increasePercentage($("#loading_bar"), 20);

使用宇宙的胶水。数学。
rel
不是
元素的有效属性。最好推荐使用
data-
attributes。在#loading_bar元素中添加属性。例如,当您想要更改宽度时,可以从“数据宽度”属性获取宽度。ex$('u#load_bar').attr('data-width')。添加宽度后,必须更新数据宽度属性alsoas BenM说不要使用rel属性。请使用数据宽度属性。谢谢你,BenMits也给宽度增加了px
function increasePercentage($element, percentage) {

    $element.css("width", "+="+(Math.round($element.parent().width() * (percentage / 100))));

}

increasePercentage($("#loading_bar"), 20);