7
\$\begingroup\$

I made this bit of code that uses jQuery in a simple HTML file to see if my son would think it is fun/interesting and to help me with my jQuery skills. The thing that was hard was the selectors towards the bottom of the script block. I am wondering if there is a better way to do this. Warning: it auto-refreshes to 're-pixel' the screen (for lack of a better word).

Also I am building tables but I am sure they are not right, it was my intention to grab the innerwidth and innerheight and create a table that is divided into fractional squares that perfectly fill the view-able browser screen. I had trouble getting the logic right.

Also, I had to place a span tag in side the <td> blocks so the wouldn't collapse on me. I wanted to do this in divs but I wasn't sure how to get the same effect.

$(document).ready(function () {
// find random colors
function colorMe() {
    var newColor = '#' + (0x1000000 + (Math.random()) * 0xffffff).toString(16).substr(1, 6);
    return newColor;
}
// not sure about this but i got it to work in some wierd way.
var wb = Math.floor(window.innerWidth / 200);
var hb = Math.floor(window.innerHeight / 200);

// table buildout append cells and rows to the table. 
// Note the yucky span tag to prevent td collapse 
var table = $('<table border=1 bordercolor=white cellpadding=0 cellspacing=0 width="100%" height="100%"></table>');
for (i = 0; i < 100; i++) {
    row = $('<tr></tr>');
    for (j = 0; j < 200; j++) {
        var row1 = $('<td bgcolor="' + colorMe() + '" width="' + wb + '"></td>').html('<div style="display:inline-block;height:' + hb + 'px;"></div>');
        table.append(row);
        row.append(row1);
    }
}
// table to body append
$('body').append(table);

// mouse event to emulate a pen commenting out the last two give it a fun calligraphy look
$("td").hover(function () {
    var whoIsMe = $(this).index();

    $(this).stop()
        .animate({
        "opacity": "0"
    }, "fast");

    $(this).next("td")
        .stop()
        .animate({
        "opacity": "0"
    }, "fast");

    $(this).prev("td")
        .stop()
        .animate({
        "opacity": "0"
    }, "fast");

    $(this).parent()
        .next()
        .children("td")
        .eq(whoIsMe)
        .stop()
        .animate({
        "opacity": "0"
    }, "fast");

    $(this).parent()
        .prev()
        .children("td")
        .eq(whoIsMe)
        .stop()
        .animate({
        "opacity": "0"
    }, "fast");
});
// eventually reload for a fresh palette of colored squares
setTimeout(function () {
    window.location.reload(1);
}, 50000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

Here is a link to a jsfiddle.

\$\endgroup\$
2
  • \$\begingroup\$ What your code is suppose to do ? Could you make a jsfiddle and link to it ? \$\endgroup\$
    – Marc-Andre
    Mar 27, 2014 at 16:14
  • \$\begingroup\$ Sure thing... jsfiddle.net/ch7MW \$\endgroup\$ Mar 27, 2014 at 16:19

2 Answers 2

3
\$\begingroup\$

To make them square:

var size = Math.max(window.innerWidth,window.innerHeight)/200;
var xbound = Math.floor(window.innerWidth/size);
var ybound = Math.floor(window.innerHeight/size);

Simply use xbound and ybound as the upper limits in the for loops.

Try to use CSS where you can.

Additionally, your script loads very slowly because of the excessive DOM manipulation. Rather than adding each element to the page one by one, create a single object and then paste that in one go. Just add up the html code with the += operator before inserting it into the page.

\$\endgroup\$
4
  • \$\begingroup\$ That gets my for loops working perfect. But the heavy lifting on the DOM? Ok, I have a few ideas maybe simplifying it and do a document write while I am looping (unless that is doing the same thing). Hmmm. I will have to consider that. Thank you for answering my question. :) \$\endgroup\$ Mar 28, 2014 at 2:26
  • \$\begingroup\$ @FrankTudor Your for loop only iterates one time, right? Or am I missing something? \$\endgroup\$
    – Kvothe
    Mar 28, 2014 at 2:27
  • \$\begingroup\$ I have two for loops one for the <TR> 'i' that is called once and spins for my innerheight about 95 times. Then there is 'j' for <TD> my innerwidth (on my screen) it spins about 200 times. That makes 'j' being called 95 times. So with 'i' and 'j' that is 19,000 iterations to build the table for my particular screen width and height. \$\endgroup\$ Mar 28, 2014 at 2:44
  • 1
    \$\begingroup\$ += yes I know what you are talking about here. Build a variable appending it as I loop. Instead of the almost reverse <tr> spins out a bunch of <td> cells and then appends back the empty tr and appends that back to the table. That is fancy but not practical nor would it make any sense to an 11 year old mind. \$\endgroup\$ Mar 28, 2014 at 2:48
1
\$\begingroup\$

Some quick advice since this already has an accepted answer - you will make things quickly less confusing if you use a few other javascript libraries.

  • At the very least you should always be using lodash, underscore, or surgarjs. Either one of these brings so much to the table as far as making loops and the idioms of javascript make actual sense.
  • If you want to stick with outputting html, at least use a template system like handlebars. It will clear all that javascript-mixed-with-html mess right up.
  • Beyond that you get into mvvm and routing libraries like knockout or sammyjs.
  • And if you want some real structure, a full-on framework like AngularJs or EmeberJs is almost certainly overkill for such a project but many novices find that it makes their learning javascript significantly easier.
\$\endgroup\$
2
  • \$\begingroup\$ Hey thanks! I will look into those. Lo-Dash looks interesting. I have heard of _Underscore.js but I have not used either. I will make a list of these tool-kits you suggest and start looking at how I can leverage them. \$\endgroup\$ Mar 30, 2014 at 21:58
  • \$\begingroup\$ @FrankTudor lodash and underscore are virtually identical just with a slightly different approach \$\endgroup\$ Mar 30, 2014 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.