SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Threaded View

  1. #1
    Designer
    Join Date
    Jun 2006
    Location
    Manila
    Posts
    590
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Totally stumped with a array-like issue

    Hi guys,

    So I'm stumped with a JS problem: I need to take a list of images, wrap each in a li (don't ask, it's just one of the those things), and place a set number into ul's. The problem is the "set number".

    The idea is that the number of ul's created will be based on the container width. So if we have a div that's 200px, and I have 6 images, I have to divide those into rows of ul's.

    To illustrate:

    container div width is 200px
    images 1-2 = 186px
    wrap images 1&2 in ul
    images 3-5 = 199px
    wrap images 3-5 in ul
    image 6 = 40px
    wrap image 6 in ul

    I've tried doing for loops with if/else if in them, but because I've based them against the 200px (something like if total width < 200px, total width += width of this span, and push this into temp array; else if total width > 200px, push array into html, empty array, start over), the issue I have is that I never catch the last line of images, since they will always be under 200px, which means the "else if" no longer runs.

    Any ideas guys? I'm totally stumped.

    (Or if anyone has an alternative way of doing this without using arrays, it would be great - no CSS suggestions please. This needs to be strictly JS).
    Last edited by XLCowBoy; May 28, 2012 at 11:21. Reason: additional info


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •