0

I am completely new to the jquery scene, and so am having some trouble trying to figure this out. My problem is:
I am creating a website for a project, and this website is based of a square (700px by 700px) that has been divided into 4 smaller squares - all 350px by 350px. What I want to do is have each of these smaller squares have a panel covering them, that when clicked, move away to reveal the content behind them. The two panels on the left side would slide to the left and disappear, and the two on the right would slide to the right and disappear.
If possible I would like only one panel to disappear at a time. So as one would click a new panel, the previous one would slid back into position, hiding its content again.

Any help would be greatly appreciated!

faedince
  • 5
  • 1
  • 4
    StackOverflow is for getting help finding and fixing problems in your code; we will not write your code for you. – Blazemonger Apr 28 '14 at 17:52
  • Oh I'm sorry, I didn't realize. I did find a plugin but it's adapting it to my layout I'm having trouble with. Sorry again. – faedince Apr 28 '14 at 17:54
  • Look into CSS3 transitions and jQuery slide animation. Play around and set up a fiddle. – VtoCorleone Apr 28 '14 at 17:58
  • 1
    Im betting there are a lot of people who don't know what a "fiddle" is, especially if their current reputation is 1. – bsayegh Apr 28 '14 at 18:20

2 Answers2

0

There are many ways to do the sort of thing you're interested in. CSS transitions have already been mentioned. I've included a working example but it is very basic - it should get you moving in the right direction. Please take a look at the links below - they've got a lot of great information.

If you have four elements, and the main function of the covers is just to cover, you could use :after (or :before) CSS pseudo-elements. Here's what that HTML markup could look like if your panels were li elements:

<ul>
    <li id="panelOne">One</li>
    <li id="panelTwo">Two</li>
    <li id="panelThree">Three</li>
    <li id="panelFour">Four</li>
</ul>

Since the covers are only there to hide your four panels and they're pseudo-elements, you'd style them in your CSS. You could start by styling your HTML elements:

ul {
    display: block;
    background: black;
    width: 700px;
    height: 700px;
    margin: 0 auto;
}
li {
    float: left;
    position: relative;
    background: white;
    border: 1px solid black;
    width: 350px;
    height: 350px;
}

Then style your :after covers:

li:after {
    display: block;
    /*the background color is just to make the covers more visible */
    background: red;
    position: absolute;
    top: 0;
    left: 0;
    width: 350px;
    height: 350px;
    content: "";
}

If you use jQuery to add a class (for example, ".clicked") to one panel at a time, you'd style the .clicked pseudo-elements so they move to the left or right:

li#panelOne.clicked:after, li#panelThree.clicked:after {
  -webkit-transform: translateX(-350px);
  -moz-transform:    translateX(-350px);
  -ms-transform:     translateX(-350px);
  -o-transform:      translateX(-350px);
  transform:         translateX(-350px);
}
li#panelTwo.clicked:after, li#panelFour.clicked:after {
  -webkit-transform: translateX(350px);
  -moz-transform:    translateX(350px);
  -ms-transform:     translateX(350px);
  -o-transform:      translateX(350px);
  transform:         translateX(350px);
}

And use jQuery to add or remove the class:

$( 'li' ).click(function(){
    //first remove the class from ALL panels
    $( 'li' ).removeClass( 'clicked' );
    //then add to the panel that's been clicked
    $(this).addClass( 'clicked' );
});

And one more step: since you want the covers to slide to the left or right, you'll need to add CSS transitions to achieve that. You'd place them in your :after pseudo-element styles:

-webkit-transition: -webkit-transform 500ms ease-out 0s;
-moz-transition: -moz-transform 500ms ease-out 0s;
-o-transition: -o-transform 500ms ease-out 0s;
transition: transform 500ms ease-out 0s;

And here's how that would all look in a jsfiddle: http://jsfiddle.net/rt9d5/10/embedded/result/

Marcatectura
  • 1,721
  • 5
  • 30
  • 49
  • 1
    Thank you so much for your answer! I really appreciate you taking the time to write all this code out. It's just what I needed, a base to expand on through experimentation. Thank you again! – faedince Apr 28 '14 at 20:11
0

Try using the Jquery UI Accordion http://jqueryui.com/accordion

You can create multiple collapsible panels and set whether it to have one panel open at a time (or multiple if you want).

My Suggestion would be to look at this, do a little research, and if you have any more questions post a more specific question on stackoverflow.

bsayegh
  • 990
  • 6
  • 17
  • 1
    Thank you! I find this very helpful. I will definitely go on to do more research now that I know which direction to look in to. Thanks again. – faedince Apr 28 '14 at 20:13