Make sure the height is divisible by 4, if not done so

For a bit about the problem, see the following code:

window.onload = function () {

    var imgHeight = $("#profile_img").height();

    var infoPanels = imgHeight - 6;
    //include borders and margin etc..
    var infoPanelsHeight = infoPanels / 4;
    $('.resize').css("height",infoPanelsHeight + "px"); 
    $('.resize2').css("height",infoPanelsHeight + "px");
}

What I'm trying to do is find the height of the image (floated: left), then divide it by 4 and use the result to set the height of 4 divs (floated: right) so that they are equal to the height of the whole image.

I use this in a resize project, but since the height of the image depends on the viewport (in this case the screen of a mobile phone), the number is very rarely rounded correctly, so divs always go 1-4 pixels.

So, to work around, I want to find the height of the image, then if the height cannot be assigned to 4, adjust it so that ... resize the image and then resize the sections using the new image height.

, , , 4, , ?

jquery javascript .

.

.

+3
2

:

 if (imgHeight % 4 != 0) { // checks if the imgHeight is not dividable by 4
    $("#profile_img").attr("height") = Math.floor(imgHeight / 4) * 4; // set lowest height that is dividable by 4
 }

:

  • , .
  • %
+10

mod%:

imgHeight % 4

"0", , imgHeight 4.

+2

Source: https://habr.com/ru/post/1760716/


All Articles