Trying to condense my javascript (jquery) -
i'm relatively new javascript , jquery. rather using foundation or bootstrap i've decided roll , maintain own solution. i'm not worried individual components i've built of them before, responsive rows/columns bit of worry me.
this bit of code directly sets percentages col1-col-12 , mob1-mob12(12 columns, mob mobile only).
i have bit of css i'm playing goes hand in hand this. i'm relatively happy functionality of this, don't know how condense solutions further other datastructures or programmatic methods.
thanks in advance advice:
$(document).ready(function(){ var colelements = ".row, .col1, .col2, .col3, .col4, .col5, .col6, .col7, .col8, .col9, .col10, .col11, .col12"; var col1 = "8%"; var col2 = "16%"; var col3 = "25%"; var col4 = "33%"; var col5 = "41%"; var col6 = "50%"; var col7 = "58%"; var col8 = "66%"; var col9 = "75%"; var col10 = "83%"; var col11 = "91%"; var col12 = "100%"; function converttopercent(n){ var output = n.tostring(); output = output.concat("%"); return output } function responsiveadjust(){ $("body").find(colelements).each(function(){ if($(this).hasclass("row")){ $(this).width("100%");} if ($("body").width() > 499){ if($(this).hasclass("col1")){ $(this).width(col1); } else if($(this).hasclass("col2")){ $(this).width(col2); } else if($(this).hasclass("col3")){ $(this).width(col3); } else if($(this).hasclass("col4")){ $(this).width(col4); } else if($(this).hasclass("col5")){ $(this).width(col5); } else if($(this).hasclass("col6")){ $(this).width(col6); } else if($(this).hasclass("col7")){ $(this).width(col7); } else if($(this).hasclass("col8")){ $(this).width(col8); } else if($(this).hasclass("col9")){ $(this).width(col9); } else if($(this).hasclass("col10")){ $(this).width(col10); } else if($(this).hasclass("col11")){ $(this).width(col11); } else if($(this).hasclass("col12")){ $(this).width(col12); }} if($("body").width() < 500){ if($(this).hasclass("mob1")){ $(this).width(col1); } else if($(this).hasclass("mob2")){ $(this).width(col2); } else if($(this).hasclass("mob3")){ $(this).width(col3); } else if($(this).hasclass("mob4")){ $(this).width(col4); } else if($(this).hasclass("mob5")){ $(this).width(col5); } else if($(this).hasclass("mob6")){ $(this).width(col6); } else if($(this).hasclass("mob7")){ $(this).width(col7); } else if($(this).hasclass("mob8")){ $(this).width(col8); } else if($(this).hasclass("mob9")){ $(this).width(col9); } else if($(this).hasclass("mob10")){ $(this).width(col10); } else if($(this).hasclass("mob11")){ $(this).width(col11); } else if($(this).hasclass("mob12")){ $(this).width(col12); }else if($(this).hasclass("col1")){ $(this).width(col1); } else if($(this).hasclass("col2")){ $(this).width(col2); } else if($(this).hasclass("col3")){ $(this).width(col3); } else if($(this).hasclass("col4")){ $(this).width(col4); } else if($(this).hasclass("col5")){ $(this).width(col5); } else if($(this).hasclass("col6")){ $(this).width(col6); } else if($(this).hasclass("col7")){ $(this).width(col7); } else if($(this).hasclass("col8")){ $(this).width(col8); } else if($(this).hasclass("col9")){ $(this).width(col9); } else if($(this).hasclass("col10")){ $(this).width(col10); } else if($(this).hasclass("col11")){ $(this).width(col11); } else if($(this).hasclass("col12")){ $(this).width(col12); }} $(this).width( //reduce element's margin $(this).width() - (($(this).outerwidth(true) - $(this).width()) + //reduce percentage element of total parent padding ( //parent padding ($(this).parent().innerwidth() - $(this).parent().width()) * //percentage of parent ($(this).outerwidth(true)/$(this).parent().width()) )+2 )); }); } responsiveadjust(); window.onresize = function(event) { responsiveadjust(); } });
first of making things wrong. javascript not place such things. it's css should set sizes. that's because browser needs download javascript, evaluate , after render pages correctly. on mobile critical.
second, if still want things in javascript, should use this:
var sizes = { col1: "8%", col2: "16%", col3: "25%", col4: "33%", col5: "41%", col6: "50%", col7: "58%", col8: "66%", col9: "75%", col10: "83%", col11: "91%", col12: "100%", row: "100%" } for(var size in sizes) { var el = $("." + size); if(el.length > 0 && el.hasclass("mob11")){ el.width(sizes[size]); } }
just use arrays , interpolations. having bunch of if-else statements bad sign.
Comments
Post a Comment