как получить ширину лития + поле с помощью jQuery

мой код - что-то вроде этого

//css
li{display:inline-block}

//html #li margin and width are dynamic
<ul>
<li style='margin:30px;width:100px;'>Pic</li>
<li style='margin:40px;width:200px;'>Pic</li>
<li style='margin:10px;width:500px;'>Pic</li>
<li style='margin:50px;width:300px;'>Pic</li>
</ul>

как сделать функцию jQuery, которая возьмет индекс лития в качестве входа, и это возвратит ширину всего лития с начала к тому индексу + поля, 'оставленные + право'

Examples // something like this   
myFunction(1); //output 440 (( 30 margin-left [0] + 30 margin-right [0] + 40 margin-left [1] + 40 margin-right [1] + 100 width [0] + 200 width [1] )) 

myFunction(2); //output 960 (( 30 margin-left [0] + 30 margin-right [0] + 40 margin-left [1] + 40 margin-right [1] + 10 margin-left [2] + 10 margin-right [2] + 100 width [0] + 200 width [1] + 500 width [2] )) 
11
задан trrrrrrm 9 March 2010 в 07:14
поделиться

3 ответа

Вы ищете outerWidth (true)

, например

$ ('li: eq (0)'). externalWidth (true)

, если вам не нужны поля, а нужны только отступы и граница, просто выполните outerWidth ()

Так что вы можете просто сложить внешнюю ширину li, чтобы получить их сумму. .

32
ответ дан 3 December 2019 в 02:10
поделиться
//adapt selectors to get only the ul you want
function calculate(i)  {
  if(i >= $("ul").size()) {
    alert("This index doesn't exist");
    return;
  }
  var sum = 0;
  for(var j=0; j<=i; j++) {
    sum = sum + $("ul li:eq("+j+")").outerWidth(true);
  }
  alert("li's width for index "+i+" is "+sum)
}
0
ответ дан 3 December 2019 в 02:10
поделиться
$.fn.sumOuterWidth = function(useMargins) {
  var sum = 0;
  this.each(function() { 
    sum += $(this).outerWidth(useMargins);
  });
  return sum;
};

// example sum the width of the first 2 li's:
$('ul li').slice(0,2).sumOuterWidth(true) 

// Also gets the sum of the width of the first 2 li's:
$('ul li:eq(1)').prevAll().andSelf().sumOuterWidth(true);
2
ответ дан 3 December 2019 в 02:10
поделиться
Другие вопросы по тегам:

Похожие вопросы: