Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4555802
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:16:37+00:00 2026-05-21T17:16:37+00:00

This is more like a math related question. I’m trying to create a cute

  • 0

This is more like a math related question.

I’m trying to create a cute fading effect with jQuery, by splitting a element in a certain number of blocks, then fading each of them, but delay the fading effect based on another array.

So to create the blocks table I have two variables:

var rows = 4,
    cols = 10;

This will divide the element into blocks like:

              0   1   2   3   4   5   6   7   8   9
             10  11  12  13  14  15  16  17  18  19
             20  21  22  23  24  25  26  27  28  29
             30  31  32  33  34  35  36  37  38  39

Then I’m creating another array which decides how the blocks will animate. For example, for a left-to-right diagonal animation this array would look like:

order = [0, 10, 1, 20, 11, 2, 30, 21, 12, 3, 31, 22, 13, 4, 32, 23, 14, 5, 33, 24, 15, 6, 34, 25, 16, 7, 35, 26, 17, 8, 36, 27, 18, 9, 37, 28, 19, 38, 29, 39];

and for this specific case it works 😀

My question is how could I create the order array automatically, not manually, based on the number of blocks (rows x columns), which can change ?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:16:38+00:00Added an answer on May 21, 2026 at 5:16 pm

    This will do it:

    var arr = [];
    
    var rows = 4;
    var cols = 10;
    
    for(var i = 0; i < rows + cols - 1; i++){
    
        for(var j = Math.min(rows, i + 1) - 1; j >= Math.max(0, i - cols + 1); j--){
            arr.push((j * cols) + i - j);
        }  
    
    }
    

    fiddle: http://jsfiddle.net/BmXpy/

    EDIT: Here’s my attempt at explaining how I came up with this. IMPORTANT, use the table of numbers above to visualize and if needed, print it and draw out the diagonals.

    First, think about what we want, it’s basically diagonals. In the example above the first diagonal is 0, then 10, 1, then 20, 11, 2, then 30, 21, 12, 3, etc. Now if you think about how many of those diagonals there are, it is rows + cols - 1. That is where we get the first loop:

    for(var i = 0; i < rows + cols - 1; i++){
    

    Now, ignore for a second the boundries. In the general case (the whole center), each of these diagonals is “rows” long. And since we want to go bottom up, we want a reverse loop. That would look like this for the inner loop:

    for(var j = rows - 1; j >= 0; j--){
    

    Now, we must deal with both boundries (left and right).

    For the left boundry, if we look at the number of diagonals which are less than “rows” long, we will see that it is rows - 1. And for these diagonals we’ll see that the length of each is i + 1. The following inner loop will handle the general case and the left boundry:

    for(var j = Math.min(rows, i + 1) - 1; j >= 0; j--){
    

    You will see that for diagonal 0, this will run once, for 1 it will run twice, etc. And for the general case (i >= rows) it will run “rows” times.

    Now, the right boundry. If we look at which diagonals on the right are shorter than “rows”, we will see it is all diagonals greater than “cols” (in the example where cols is 10, 0 indexed, that is row 10 and beyond). Replacing j >= 0 with j >= Math.max(0, i - cols + 1) will run to 0 for the general case and the left boundry but shorten for the right boundry. We get this:

    for(var j = Math.min(rows, i + 1) - 1; j >= Math.max(0, i - cols + 1); j--){
    

    And finally, the actual calculation of the number in each location. i represents the diagonal and j represents the index of the number on the diagonal j = 0 is the top number if you’re looking at the posted table of numbers. For j = 0 (top row of numbers) the number is simply i, for each row below the top, we need to multiply the number by “cols” in order to get the number directly below the first row number, then the number needs to be adjusted to the left. This is done by subtracting j which is the row number. So for j = 1 (the 2nd row) we need to move the number left by one (subtract 1). So we have i for the horizontal position on the first row, + (j * cols) to move it down to the appropriate row and then -j to adjust it to diagonal (if you have drawn the diagonals, trace this out for one of them to get a good visual). We get this:

    (j * cols) + i - j
    

    Put it all together and you get my final code above. Hope that made some sense.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more of a syntax question I'm trying to write a store procedure
I did some HTTP monitoring with WireShark. Are there more tools like this that
I have string like this /c SomeText\MoreText Some Text\More Text\Lol SomeText I want to
I have an Excel Spreadsheet like this id | data for id | more
This is more of an academic inquiry than a practical question. Are there any
This is more of an generic XML Schema question, but if and how do
This is more an observation than a real question: MS-Access (and VBA in general)
This is more of a business-oriented programming question that I can't seem to figure
Ok this is more of a computer science question, than a question based on
This is more of a challenge question than something I urgently need, so don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.