Reputation: 228
This is a bit of a math problem mixes with some JavaScript, I have this adjustable div
(https://i.sstatic.net/1Pwj9.jpg), when clicking on submit
it will be filled entirely by 10 by 10 smaller div's
.
But I don't quite know how to calculate how many row's of 10 by 10 div's
will fit into the parent
.
What I currently have is this code:
var w_items = Math.ceil(def_w / 10),
h_items = Math.ceil(def_h / 10);
That give's me back the rounded width
and height
of said parent
, don't know how to calculate how many would fit thought, do I add them together or something?
Upvotes: 1
Views: 133
Reputation: 164
Multiply the width and height of the parent div. You'll get its area.
Then, the area of a small div is 100 because of 10x10=100.
Then all you have to do is Math.floor(parentArea/smallDivArea)
and you'll get the number of small divs that can fit into the big div.
Upvotes: 1
Reputation:
If you floor (also known as integer division) the value instead of ceiling it, you will get the number of elements that can fit horizontally and vertically. The total count will be w_items * h_items.
Upvotes: 0