簡體   English   中英

如何使用jQuery計算框的寬度?

[英]How to calculate the width of the box with jquery?

如何計算從初始框到要單擊的框的寬度?

因此,如果單擊黃色按鈕。那么將計算紅色,藍色,綠色,黃色框的寬度

200 + 150 + 180 + 120,結果為650px

在此處輸入圖片說明

如果我單擊綠色框,結果將為530px

在此處輸入圖片說明

如果您單擊紅色框,則僅顯示紅色框的寬度(200像素)

https://jsfiddle.net/wx05ng24/

 $('.div-click').click(function(e){ $(this).outerWidth(); alert($(this).outerWidth()); }); 
  .centerDiv { width: 100%; height:200px; margin: 0 auto; } .div-click { height:200px; float:left; } .A { width: 200px; background-color:#fe0000 ; } .B { width: 150px; background-color:#0036fe ; } .C { width: 180px; background-color:#00fe36 ; } .D { width: 120px; background-color:#fecb00 ; } .E { width: 130px; background-color:#fe00e3 ; } .F { width: 140px; background-color:#5a4c54 ; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="centerDiv"> <div class="div-click A"> </div> <div class="div-click B"> </div> <div class="div-click C"> </div> <div class="div-click D"> </div> <div class="div-click E"> </div> <div class="div-click F"> </div> </div> 

您可以使用prevAll()來獲取每個前面的div並將其寬度添加到總計中,如下所示:

 $('.div-click').click(function(e) { var totWidth = this.offsetWidth; $(this).prevAll().each(function(index) { totWidth += this.offsetWidth }); console.log(totWidth); }); 
 .centerDiv { width: 100%; height: 200px; margin: 0 auto; } .div-click { height: 200px; float: left; } .A { width: 200px; background-color: #fe0000; } .B { width: 150px; background-color: #0036fe; } .C { width: 180px; background-color: #00fe36; } .D { width: 120px; background-color: #fecb00; } .E { width: 130px; background-color: #fe00e3; } .F { width: 140px; background-color: #5a4c54; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="centerDiv"> <div class="div-click A"> </div> <div class="div-click B"> </div> <div class="div-click C"> </div> <div class="div-click D"> </div> <div class="div-click E"> </div> <div class="div-click F"> </div> </div> 

使用prevAll('.div-click')獲得clicked元素的所有先前元素。

 $('.div-click').click(function(e) { var $prev = $(this).prevAll('.div-click'); var width = $(this).width();; $.each($prev, function() { width += $(this).width(); }); alert("total width: " + width); }); 
 .centerDiv { width: 100%; height:200px; margin: 0 auto; } .div-click { height:200px; float:left; } .A { width: 200px; background-color:#fe0000 ; } .B { width: 150px; background-color:#0036fe ; } .C { width: 180px; background-color:#00fe36 ; } .D { width: 120px; background-color:#fecb00 ; } .E { width: 130px; background-color:#fe00e3 ; } .F { width: 140px; background-color:#5a4c54 ; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="centerDiv"> <div class="div-click A"> </div> <div class="div-click B"> </div> <div class="div-click C"> </div> <div class="div-click D"> </div> <div class="div-click E"> </div> <div class="div-click F"> </div> </div> 

您可以獲取元素的位置,然后添加其寬度:

$(this).position().left + $(this).outerWidth()

注意,您必須將包含div的position: relative;設為position: relative;
而且只有在div彼此相鄰且不中斷新行時才起作用。

跟蹤選定的div非常簡單。

因此,如果我們選擇div 3,則需要計算1,2和3。為此,我使用了.index()

通過下面的示例了解更多信息

 $(".centerDiv> div").click(function(){ console.clear(); var index = $(this).index() // know the index of the div which selected var widthSum =0; var sumText = ""; $(this).parent().children("div").each(function(itemIndex, item){ if (itemIndex <= index) // Keep track of the divs we want to calculate { widthSum += $(item).width(); sumText += $(item).attr("class").split(" ")[1] + " "; // just to show which div did we included } }); console.log("The total width of divs ["+ sumText+ "] is " + widthSum) }); 
  .centerDiv { width: 100%; height:200px; margin: 0 auto; } .div-click { height:200px; float:left; } .A { width: 200px; background-color:#fe0000 ; } .B { width: 150px; background-color:#0036fe ; } .C { width: 180px; background-color:#00fe36 ; } .D { width: 120px; background-color:#fecb00 ; } .E { width: 130px; background-color:#fe00e3 ; } .F { width: 140px; background-color:#5a4c54 ; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="centerDiv"> <div class="div-click A"> </div> <div class="div-click B"> </div> <div class="div-click C"> </div> <div class="div-click D"> </div> <div class="div-click E"> </div> <div class="div-click F"> </div> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM