SlideShare una empresa de Scribd logo
1 de 6
Sliding touch photo gallery for iPhone – HTML5, CSS3
A post from my Blog: http://jbkflex.wordpress.com/2012/03/29/sliding-touch-photo-gallery-for-iphone-
html5-css3/

This tutorial speaks about a simple sliding touch photo gallery for iPhone. The app is meant to run on the mobile safari web
browser. We start off by looking at a demo first and then talk about the code in details. Note that the same logic and
implementation will work even on Android devices, you just need to correct the dimensions/positions and place the elements
according to screen resolution.
Demo Link: http://jbk404.site50.net/css3/slidinggallery/ (open in your iPhone/iPod or Android device’s web browser)




                                                   Gallery with the thumbnail images

The images used in the demo are not of great quality or appeal as I am not a Photoshop expert. Once you have cool assets
you are good to go.
The demo is all about having two panels, one with the thumbnail images and the other panel with the corresponding bigger
image of the thumbnail selected in the first panel. The two panels have been placed side by side – horizontally and they slide in
and out of the viewport to give a sliding effect. The sliding of panels uses the same concept (using CSS3 transitions and
transformations) that I discussed in one of my earlier post. Here in this tutorial I will not go into the details of how to create
sliding touch panels. You can refer my previous post.
The two panels are placed horizontally inside the wrapper using float:left

As you have seen in the demo when you tap on a thumbnail image the entire panel slides out and a second panel with the
corresponding larger image slides in. That is what we will achieve at the end of this post. Let’s begin by creating the grid that
will hold the thumbnail images in panel1.
Create the thumbnail image grid in Panel1
First, let us create our thumbnail gallery. We will use HTML and CSS to design and place the thumbnail images. The HTML
code block needed is shown below,

   <section id="panel1">

     <ul>

        <li class="normal"><img src="images/thumb1.jpg"/></li>

        <li class="normal"><img src="images/thumb2.jpg"/></li>

        <li class="normal"><img src="images/thumb3.jpg"/></li>

        <li class="normal"><img src="images/thumb4.jpg"/></li>

        <li class="normal"><img src="images/thumb5.jpg"/></li>

        <li class="normal"><img src="images/thumb6.jpg"/></li>

        <li class="normal"><img src="images/thumb7.jpg"/></li>

        <li class="normal"><img src="images/thumb8.jpg"/></li>

        <li class="normal"><img src="images/thumb9.jpg"/></li>

     </ul>

   </section>


As you can see I have placed each thumbnail image inside an <li> element. And all the <li> elements are in turn placed inside
an <ul> element. Now, to arrange them in a grid I do this in the CSS,
#panel1 ul

   {

       list-style:none;

       width:280px;

       position:absolute;

       left:40px;

       top:90px;

   }




   #panel1 ul li.normal

   {

       float:left; /* this is important to place the thumbnails in a grid */

       margin-right:8px;

       margin-bottom:4px;

       background-color:#fff;

       padding-top:3px;

       padding-left:2px;

       padding-right:2px;

   }




   #panel1 ul li img

   {

       height:70px;

   }


Also if you notice, each thumbnail image is initially given a classname of normal as none of them is selected. Once selected a
thumbnail is assigned a classname of selected (this is done in the script block). This is done to differentiate between the last
selected thumbnail image and the rest of them in the grid (I do it by giving a red border to the selected thumbnail, you can see it
in the picture above). The CSS needed is below,

   #panel1 ul li.selected

   {
float:left;

   margin-right:8px;

   margin-bottom:4px;

   background-color:#ff0000;

   padding-top:3px;

   padding-left:2px;

   padding-right:2px;

   }


Creating Panel2
Panel2 is relatively simpler to build. It has the back button at the top. When user taps on the back button, panel2 slides out to
the right and panel1 slides in again from the left. The larger image is displayed by the Image element with id =
bigImage. Below is the screenshot of Panel2, followed by the HTML code,




                                                        Panel2 screenshot


   <section id="panel2" style="margin:0 !important; padding:0 !important;">

       <img id="backBtn" src="images/back.png" />

       <div id="bigImageHolder"><img id="bigImage" src="" /></div>

   </section>


Adding touch events to thumbnail images
When you tap on a thumbnail image I get the id of the thumbnail and using that id I determine the bigger image i.e its path. I
have named the images as big1, big2…. etc so I just concatenate the id to the string value “big”. I have zipped the images used
in the demo. You can download it at the end of the tutorial. But before that we need to add touch events to the thumbnails. Let’s
add it in our script block.

   gridGallery.thumbnailArr = gridGallery.panel1.getElementsByTagName("img");

   for (var i = 0; i < gridGallery.thumbnailArr.length; i++) {

       gridGallery.thumbnailArr[i].addEventListener("touchend", gridGallery.taptHandler,
   false);

       gridGallery.thumbnailArr[i].setAttribute("id", i + 1);

   }


As you can see above in the script block, I got the reference to all the thumbnail images and store them in
thethumbnailArr array. Later I loop through the length of the array and assign a unique id to each thumbnail image and also
register a touchend event listener. Now, that I have registered a touch event to the thumbnail images I need to handle when
the event is generated i.e when user taps on a thumbnail image. For that I have created thetapHandler function.

   tapHandler: function(event) {

       if (event.target.id != "backBtn") {

           gridGallery.distanceX = -320;

           gridGallery.currentSelectedImage = event.target.parentNode;

           gridGallery.prevSelectedImage.className = "normal";

           gridGallery.currentSelectedImage.className = "selected";

           gridGallery.prevSelectedImage = gridGallery.currentSelectedImage;

           document.getElementById("bigImage").src = "images/big" +
   gridGallery.currentSelectedImage.getElementsByTagName("img")[0].getAttribute("id") +
   ".jpg";

       }

       else {

           gridGallery.distanceX = 0;

       }

       gridGallery.panelContainer.style.webkitTransform = "translate3d(" +
   gridGallery.distanceX + "px, 0, 0)";

       event.preventDefault();

   }


If you see inside the tapHandler function I have an if-else block. I will talk about the else part later. The if block starts by
checking if the touch event is generated by the thumbnail images. I am doing this because I have used the
sametapHandler event listener function for the back button as well. Note that the back button is also registered to listen to
touch events. I will talk on this shortly. Now, if we look inside the if block, I set distanceX property a value of -320, this is for
translating the panel container a distance of 320px to the left. I have hard coded the value as 320 since I am specifically
building this app for iPhone and iPod Touch and they have a width of 320px in the portrait mode. You can use the browser
window width for a more generic result - window.innerWidth. Then I get the reference of the current selected thumbnail image
and assign it a class name of selected, this will immediately set the border of the selected image to red. Also I set the class of
the previous selected thumbnail to normal to remove its red border. Then finally I get the path of the corresponding larger
image that will be shown in panel2. I get the id of the current selected thumbnail and then manipulate the name of the larger
image. As I have already told, that I have named my images as big1, big2…. etc. So once I get the id I concatenate it to the
path string to get the larger image. This is simple I guess.
Adding touch event to the Back button
When user taps on the back button panel2 slides out and panel1 slides in from the left again. For this to happen we need to
add touch event to the back button as well. This is how to do it,

   gridGallery.backBtn = document.getElementById("backBtn");

   gridGallery.backBtn.addEventListener("touchend", gridGallery.tapHandler, false);


If you notice I have used the same event function (tapHandler) for handling the touch event generated by the back button. This
is the reason why I used the if-else check inside tapHandler. When user taps on the back button the else part is executed.
Inside it, I only set the distanceX to zero as now the panel container will be translated back to its original position. Nothing more
is needed.

   else {

          gridGallery.distanceX = 0;

      }


After the if-else block I have written a very important line,

   gridGallery.panelContainer.style.webkitTransform = "translate3d(" +
   gridGallery.distanceX + "px, 0, 0)";


This is where the actual sliding of the panels happen. I have used CSS3 Transformation – translate3d function to move the
panel container in the x-direction. The distance to be moved is determined by distanceX property which I have talked about
earlier. I will not go into the concepts of CSS3 transformation and transitions now, you can read my previous post about sliding
touch panels.
One thing you might be confused about my java script code is the syntax I have used to write. I have used the JSON pattern of
writing my code wherein I have encapsulated everything inside an object. You can view the full source code in the desktop
browser. Just open the demo link in any desktop browser and view source.
The link to the demo once again: http://jbk404.site50.net/css3/slidinggallery/ (open in your iPhone/iPod or Android device’s
web browser)
Download the zipped images folder here.

Más contenido relacionado

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 

Destacado

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Sliding touch photo gallery for iPhone – HTML5, CSS3

  • 1. Sliding touch photo gallery for iPhone – HTML5, CSS3 A post from my Blog: http://jbkflex.wordpress.com/2012/03/29/sliding-touch-photo-gallery-for-iphone- html5-css3/ This tutorial speaks about a simple sliding touch photo gallery for iPhone. The app is meant to run on the mobile safari web browser. We start off by looking at a demo first and then talk about the code in details. Note that the same logic and implementation will work even on Android devices, you just need to correct the dimensions/positions and place the elements according to screen resolution. Demo Link: http://jbk404.site50.net/css3/slidinggallery/ (open in your iPhone/iPod or Android device’s web browser) Gallery with the thumbnail images The images used in the demo are not of great quality or appeal as I am not a Photoshop expert. Once you have cool assets you are good to go. The demo is all about having two panels, one with the thumbnail images and the other panel with the corresponding bigger image of the thumbnail selected in the first panel. The two panels have been placed side by side – horizontally and they slide in and out of the viewport to give a sliding effect. The sliding of panels uses the same concept (using CSS3 transitions and transformations) that I discussed in one of my earlier post. Here in this tutorial I will not go into the details of how to create sliding touch panels. You can refer my previous post.
  • 2. The two panels are placed horizontally inside the wrapper using float:left As you have seen in the demo when you tap on a thumbnail image the entire panel slides out and a second panel with the corresponding larger image slides in. That is what we will achieve at the end of this post. Let’s begin by creating the grid that will hold the thumbnail images in panel1. Create the thumbnail image grid in Panel1 First, let us create our thumbnail gallery. We will use HTML and CSS to design and place the thumbnail images. The HTML code block needed is shown below, <section id="panel1"> <ul> <li class="normal"><img src="images/thumb1.jpg"/></li> <li class="normal"><img src="images/thumb2.jpg"/></li> <li class="normal"><img src="images/thumb3.jpg"/></li> <li class="normal"><img src="images/thumb4.jpg"/></li> <li class="normal"><img src="images/thumb5.jpg"/></li> <li class="normal"><img src="images/thumb6.jpg"/></li> <li class="normal"><img src="images/thumb7.jpg"/></li> <li class="normal"><img src="images/thumb8.jpg"/></li> <li class="normal"><img src="images/thumb9.jpg"/></li> </ul> </section> As you can see I have placed each thumbnail image inside an <li> element. And all the <li> elements are in turn placed inside an <ul> element. Now, to arrange them in a grid I do this in the CSS,
  • 3. #panel1 ul { list-style:none; width:280px; position:absolute; left:40px; top:90px; } #panel1 ul li.normal { float:left; /* this is important to place the thumbnails in a grid */ margin-right:8px; margin-bottom:4px; background-color:#fff; padding-top:3px; padding-left:2px; padding-right:2px; } #panel1 ul li img { height:70px; } Also if you notice, each thumbnail image is initially given a classname of normal as none of them is selected. Once selected a thumbnail is assigned a classname of selected (this is done in the script block). This is done to differentiate between the last selected thumbnail image and the rest of them in the grid (I do it by giving a red border to the selected thumbnail, you can see it in the picture above). The CSS needed is below, #panel1 ul li.selected {
  • 4. float:left; margin-right:8px; margin-bottom:4px; background-color:#ff0000; padding-top:3px; padding-left:2px; padding-right:2px; } Creating Panel2 Panel2 is relatively simpler to build. It has the back button at the top. When user taps on the back button, panel2 slides out to the right and panel1 slides in again from the left. The larger image is displayed by the Image element with id = bigImage. Below is the screenshot of Panel2, followed by the HTML code, Panel2 screenshot <section id="panel2" style="margin:0 !important; padding:0 !important;"> <img id="backBtn" src="images/back.png" /> <div id="bigImageHolder"><img id="bigImage" src="" /></div> </section> Adding touch events to thumbnail images When you tap on a thumbnail image I get the id of the thumbnail and using that id I determine the bigger image i.e its path. I have named the images as big1, big2…. etc so I just concatenate the id to the string value “big”. I have zipped the images used
  • 5. in the demo. You can download it at the end of the tutorial. But before that we need to add touch events to the thumbnails. Let’s add it in our script block. gridGallery.thumbnailArr = gridGallery.panel1.getElementsByTagName("img"); for (var i = 0; i < gridGallery.thumbnailArr.length; i++) { gridGallery.thumbnailArr[i].addEventListener("touchend", gridGallery.taptHandler, false); gridGallery.thumbnailArr[i].setAttribute("id", i + 1); } As you can see above in the script block, I got the reference to all the thumbnail images and store them in thethumbnailArr array. Later I loop through the length of the array and assign a unique id to each thumbnail image and also register a touchend event listener. Now, that I have registered a touch event to the thumbnail images I need to handle when the event is generated i.e when user taps on a thumbnail image. For that I have created thetapHandler function. tapHandler: function(event) { if (event.target.id != "backBtn") { gridGallery.distanceX = -320; gridGallery.currentSelectedImage = event.target.parentNode; gridGallery.prevSelectedImage.className = "normal"; gridGallery.currentSelectedImage.className = "selected"; gridGallery.prevSelectedImage = gridGallery.currentSelectedImage; document.getElementById("bigImage").src = "images/big" + gridGallery.currentSelectedImage.getElementsByTagName("img")[0].getAttribute("id") + ".jpg"; } else { gridGallery.distanceX = 0; } gridGallery.panelContainer.style.webkitTransform = "translate3d(" + gridGallery.distanceX + "px, 0, 0)"; event.preventDefault(); } If you see inside the tapHandler function I have an if-else block. I will talk about the else part later. The if block starts by checking if the touch event is generated by the thumbnail images. I am doing this because I have used the sametapHandler event listener function for the back button as well. Note that the back button is also registered to listen to touch events. I will talk on this shortly. Now, if we look inside the if block, I set distanceX property a value of -320, this is for translating the panel container a distance of 320px to the left. I have hard coded the value as 320 since I am specifically
  • 6. building this app for iPhone and iPod Touch and they have a width of 320px in the portrait mode. You can use the browser window width for a more generic result - window.innerWidth. Then I get the reference of the current selected thumbnail image and assign it a class name of selected, this will immediately set the border of the selected image to red. Also I set the class of the previous selected thumbnail to normal to remove its red border. Then finally I get the path of the corresponding larger image that will be shown in panel2. I get the id of the current selected thumbnail and then manipulate the name of the larger image. As I have already told, that I have named my images as big1, big2…. etc. So once I get the id I concatenate it to the path string to get the larger image. This is simple I guess. Adding touch event to the Back button When user taps on the back button panel2 slides out and panel1 slides in from the left again. For this to happen we need to add touch event to the back button as well. This is how to do it, gridGallery.backBtn = document.getElementById("backBtn"); gridGallery.backBtn.addEventListener("touchend", gridGallery.tapHandler, false); If you notice I have used the same event function (tapHandler) for handling the touch event generated by the back button. This is the reason why I used the if-else check inside tapHandler. When user taps on the back button the else part is executed. Inside it, I only set the distanceX to zero as now the panel container will be translated back to its original position. Nothing more is needed. else { gridGallery.distanceX = 0; } After the if-else block I have written a very important line, gridGallery.panelContainer.style.webkitTransform = "translate3d(" + gridGallery.distanceX + "px, 0, 0)"; This is where the actual sliding of the panels happen. I have used CSS3 Transformation – translate3d function to move the panel container in the x-direction. The distance to be moved is determined by distanceX property which I have talked about earlier. I will not go into the concepts of CSS3 transformation and transitions now, you can read my previous post about sliding touch panels. One thing you might be confused about my java script code is the syntax I have used to write. I have used the JSON pattern of writing my code wherein I have encapsulated everything inside an object. You can view the full source code in the desktop browser. Just open the demo link in any desktop browser and view source. The link to the demo once again: http://jbk404.site50.net/css3/slidinggallery/ (open in your iPhone/iPod or Android device’s web browser) Download the zipped images folder here.