I am trying to make my own framework using flexbox. The biggest headache of flexbox when there is an odd number of elements in a line: 3, 5, 7. Therefore, I want to solve it with js / jq. For example, if the width of an element is 300 pixels or less, the element becomes 100% of the width. I am using jq code:
function check() { var window = $(document).outerWidth(); var width = $('.one-third').outerWidth(); if ( width < 300 ) { $('.one-third').addClass('one-third-full') } else { $('.one-third').removeClass('one-third-full') } $('.one-third').text(width) }
But the problem is that the function sets the width of the element to 100% according to the CSS rule, the script recounts according to its IF statement, and the elements start to blink. Can someone help me solve this problem? The code is in the snippet.
function check() { var window = $(document).outerWidth(); var width = $('.one-third').outerWidth(); if ( width < 300 ) { $('.one-third').addClass('one-third-full') } else { $('.one-third').removeClass('one-third-full') } $('.one-third').text(width) } $(document).ready(function() { check() }); $(window).resize(function() { check() });
body { background: #C38D94; font-family: 'Arvo', serif; } .fbox { display: flex; flex-flow: row wrap; } .one-half:after, .one-third:after, .one-four:after, .one-five:after, .one-six:after { position: absolute; top: 0; right: 0; } .one-half:after { content: '2'; } .one-third:after { content: '3'; } .one-four:after { content: '4'; } .one-five:after { content: '5'; } .one-six:after { content: '6'; } .one-half, .one-third, .one-four, .one-five, .one-six{ position: relative; padding: 30px; background: #565676; margin: 1px; text-align: center; color: #fff; box-sizing: border-box; } .one-half { flex: 1 calc(50% - 4px); min-width: 300px; } .one-third { flex: 1 calc(30% - 4px); } .one-four { flex: 1 calc(25% - 4px); } .one-five { flex: 1 calc(20% - 4px); } .one-six { flex: 1 calc(15% - 4px); } .one-third-full { flex: 1 100%; } .to-column { flex-flow: column } @media only screen and (max-width : 640px) { }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="fbox dns"> <div class="one-third"></div> <div class="one-third"></div> <div class="one-third"></div> </div> <div class="fbox"> <div class="one-half"></div> <div class="one-half"></div> </div> <div class="fbox"> <div class="one-third"></div> <div class="one-third"></div> <div class="one-third"></div> </div> <div class="fbox"> <div class="one-four"></div> <div class="one-four"></div> <div class="one-four"></div> <div class="one-four"></div> </div> <div class="fbox"> <div class="one-five"></div> <div class="one-five"></div> <div class="one-five"></div> <div class="one-five"></div> <div class="one-five"></div> </div> <div class="fbox"> <div class="one-half"></div> <div class="one-five"></div> <div class="one-third"></div> </div> <div class="fbox"> <div class="one-four"></div> <div class="one-third"></div> <div class="one-half"></div> </div> <div class="fbox"> <div class="one-four"></div> <div class="one-four"></div> <div class="one-four"></div> <div class="one-four"></div> </div> <div class="fbox"> <div class="one-six"></div> <div class="one-six"></div> <div class="one-six"></div> <div class="one-six"></div> <div class="one-six"></div> <div class="one-six"></div> </div> </div> <div class="truth"></div>
source share