Search code examples
jqueryjquery-selectorsjquery-effects

Having problems making a gallery with lists, jquery and a lot of toggling


So I've been googling around, but just havn't found any good solution for this layout yet.

What Im trying to do is like a gallery. First thumbs, then a large below the clicked thumb.

The problem is to separate different events.

  1. A click with no visible div. (Slide down selected div)
  2. A click with a visible div. (Transition to new div. Hide old, then show the new one or something)
  3. A click with an already selected div. (Slide up selected div)

Been struggling around with different solutions, but either it's glitchy or sometimes it doesn't react to clicks at first.

Basic structure so far:

<ul>
    <li id="case1">Thumb1</li>
    <li id="case2">Thumb2</li>
    <li id="case3">Thumb3</li>
</ul>

<div id="case1box">Content</div>
<div id="case2box">Content</div>
<div id="case3box">Content</div>

Then repeating that snippet. Any better solution, as it's UL(3xLI) then DIV, UL(3xLI) then DIV?

Any thoughts on the toggling and how to solve it?


Default state: Default state


When clicked: Clicked state


Solution

  • I'll respond to your behavior one by one and in pseudo code. i'll assume you know enough jquery until further notice :)

    A click with no visible div. (Slide down selected div) - easy, put a class and id on your lis. then bind the toggle function to that class:

     $(".classofli").toggle(function() {
       //get id of current li using attr('id'), parse the number, something like picture_1 or whatever
       //$("#"+idofdivtoslide).slideToggle();
     })
    

    that takes care of item 1 and 3 already(A click with an already selected div. (Slide up selected div)

    as for item 2: A click with a visible div. (Transition to new div. Hide old, then show the new one or something)

    have a variable that keeps your last selected div and a function that closes open divs(the ones with the larger image).

    var lastSelected;
    
    //in your toggle function, check if the id of your current thing was the last you clicked, if yes, just call slideToggle. if no, close all open divs, and call slide toggle on current one.
    //don't forget to set the lastSelected variable to the id of the item you clicked!
    

    Hope that points you to the right direction