Distribute elements evenly using CSS

后端 未结 13 734
一生所求
一生所求 2020-12-09 02:55

A method to distribute elements evenly in a container using CSS appeared on Smashing Magazine today.

I recently had to use Javascript to achieve the same effect for

13条回答
  •  有刺的猬
    2020-12-09 03:33

    Here is the code in jQuery format for anyone who finds it useful

    function justifyClients() {
                            var menuItems  = $("#clients-wrapper ul li").get();                         
                            var menuWidth  = $("#clients-wrapper ul").width();                          
                            var totalWidth = 0;
    
                            $("#clients-wrapper ul li").each(function(i,e)
                                            {
                                                totalWidth += $(e).width();
                                            });
    
                            var margin = (menuWidth - 4 - totalWidth) / ($("#clients-wrapper ul li").length - 1);
                            margin = parseInt(margin);
    
                            $("#clients-wrapper ul li").each(function(i,e) {
    
                                if(i < $("#clients-wrapper ul li").length - 1)
                                {
                                    alert(i + " " + $("#clients-wrapper ul li").length);
                                    $(e).css('margin-right',  margin);
                                }
                            });
                        }
    
                        $(document).ready(function() {
                          justifyClients();
                        });
    

提交回复
热议问题