How to create a Rotated Gallery
May 6, 2014
Today i will teach you how to create a tilted gallery using jQuery and using the CSS3 transform property. Each gallery features controls and popup for each picture.

This tutorial is best suited for portfolio galleries or for creating a professional photo gallery. This tutorial is made so that it will work with unlimited number of sliders, photos and on any screen size.
A special thanks goes to David Hauserman for allowing me to use his photos. Some of the icons where created using icomoon.

This tutorial will only work on browsers that supports the most recent CSS3 features and this tutorial is not ready for production.


The HTML starts with id="popup" which will display the image popup once you click on an image and the id="wrapper" that will hold all the galleries. id="popup" will be used to fill the entire screen with a transparent background.

The main element that hold the gallery is id="wrapper", you will find out later why is important to use this element. id="gallery" is the element that hold all the photo galleries. In the code presented here you will find only one class="gallery-tab" but you can add as many as you want because the javascript is created to work with an unlimited number of galleries. If you want to add multiple galleries you just have to copy the entire class="gallery-tab" element's content and replace the images inside. If you want to add more images you will have to copy class="gallery-tab-images-slider-tab" multiple times because that is the element that holds the images.


Lets start out by styling the element #wrapper. We will make it float to the left because we want the height to be adjusted automatically and also we make it 'overflow: hidden;' due to the incline of the galleries it will make the width of the element bigger than the overall browser window and we don't want to see a horizontal scrolling bar.

The popup will have a fixed position and we will also have to set the display to none because we will make it visible only when a user clicks on a photo. #popup-box will have the position relative because we have to position #popup-box-close outside the image.

Lets move on to the main element of this tutorial, #gallery. The sliders inside #gallery will be bigger than the gallery div because of the incline and that's why we need to hide any horizontal overflow in this element. We will do that with 'overflow-x: hidden;'.  Another important point is to make the #gallery taller than it really is because the vertical scrolling bar won't be displayed properly, we will do this using 'padding-bottom: 300px;'

We have used the class .gallery-tab for each slider because you will be able to insert as many sliders as you want. The only special property that .gallery-tab has is the CSS3 transform property. We will have to use the transform property to rotate .gallery-tab and we have to write it three times in different formats for all the major browsers.  

The next element its probably a bit confusing but i will hopefully clear it out for you. .gallery-tab-title is the parent for .gallery-tab-title-holder-controls and .gallery-tab-title-holder will have to be set to 'position: relative;' because we want to move .gallery-tab-title-holder -45px to the left.

Inside .gallery-tab-title-holder you will find .gallery-tab-title-holder-controls and .gallery-tab-title-holder-text, .gallery-tab-title-holder-controls also has .gallery-tab-title-holder-controls-button inside.

The next element after .gallery-tab-title is .gallery-tab-images. Inside this one we have another div .gallery-tab-images-slider that will be used to hold images. Images are hold in tabs. Inside the tab you will also find a hover element .gallery-tab-images-slider-tab-hover that will only be shown when a user hovers above the picture. The .gallery-tab-images element needs to have a relative position because we will use this as a reference to move .gallery-tab-images-slider.

The element .gallery-tab-images-slider-tab also needs to have a relative position because .gallery-tab-images-slider-tab-hover will be positioned above the content of this element. We will display and hide the hover icon not by using javascript but by changeling the opacity of this element.


Please note that we will have to use '$(window).load(function () {});' instead of the usual '$(function() {});' because we want to start after all the images are loaded, this way when we have to calculate the actual height and width of the pictures they will be displayed correctly.
We will start by creating a variable that we will use for display the transition time in milliseconds.

The next thing we have to do is create the click function for when a user clicks on the title. When they click the arrow controls will appear from the left. If the element .gallery-tab-images is visible we will move .gallery-tab-title-holder -45 px to the left if the element is hidden we will show the controls by moving .gallery-tab-title-holder 50 px from left side. We also have to hide or show .gallery-tab-images each time they click on the title.

In order for the images to be displayed correctly we have to set the width of .gallery-tab-images-slider equal to the total widths of all the pictures plus the space between the pictures. There is a problem because all the images have a default height of 220 px set up in CSS and the pictures don't have the final width set up in HTML, so its not possible to get he width of an image straight away. We will have to create a loop that will check each .gallery-tab-images-slider class and then create another loop inside the master loop. This final loop will take each individual image and get its default height and width to create a ratio between the two. We will have the final width by multiplying the ratio with 220 which is the height set up in CSS. Keep in mind we have to add 20 px for each picture because that is the distance between the images. We will also add two HTML5 attributes 'data-height' and 'data-width' to each image that will later help us when we use the image popup.

The next thing is to create functions for when a user presses one of the two buttons to move the slider. When a user presses the right arrow we will have to check if the left position value for the slider is not higher than the negative value of the slider's width minus the width the #gallery element. If the result is TRUE we will proceed to moving the slider.

When a user presses the left button before moving the slider to the left we will have to check if the left position value for the slider is smaller than minus 200 and if the result is TRUE we will move the slider.

The last part of this tutorial is to teach you how the popup works. To make the popup happen we will have to create a click function. Inside the click function we will set the source of the image located inside the popup to the source of the image we have clicked on. We will also have to set its width and its height to the height and width of the image we have clicked on. If we don't change the width of #popup-box each time we click on a different image the popup will not be centered so we will have to change the width of the box as well. The last thing we have to do is make the popup element fade in.

Last but not least we will make the popup element to fade out when we click on the close icon.

About the author
Popa Robert: Is the founder of Simbyone.com, freelance web developer and designer, he likes to create beautiful websites and water cooled PC's
Would you like to share your thoughts?
Fields marked with * are mandatory. We don't share your email with anyone. Email provided in the above fields are used for identification only. If you chose to subscribe to our newsletter you can cancel at any time.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Hot Posts