Set height of an element on multiples of a number?

前端 未结 3 1885
清酒与你
清酒与你 2021-01-16 03:13

Is there any way via jquery or javascript to make an element stretch its height to a certain set of numbers? I mean, as it accommodates more content, its height would only f

3条回答
  •  爱一瞬间的悲伤
    2021-01-16 03:18

    This function will take the current height and the unit number you want it to snap to, rounding to the nearest multipleOf.

    function snapHeight(height, multipleOf) {
        return multipleOf * Math.round(height / multipleOf);
    }
    

    Examples:

    snapHeight(930, 100); // returns 900
    snapHeight(930, 50); // returns 950
    $('#element').height(snapHeight($('#element').height(), 100)); // in jQuery
    

提交回复
热议问题