SlideShare a Scribd company logo
1 of 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.

More Related Content

Recently uploaded

Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 

Recently uploaded (20)

Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 

Featured

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 HubspotMarius Sescu
 
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 ChatGPTExpeed Software
 
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 EngineeringsPixeldarts
 
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 HealthThinkNow
 
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.pdfmarketingartwork
 
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 2024Neil Kimberley
 
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)contently
 
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 2024Albert Qian
 
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 InsightsKurio // The Social Media Age(ncy)
 
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 2024Search Engine Journal
 
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 summarySpeakerHub
 
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 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 Tessa Mero
 
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 IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
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 managementMindGenius
 
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...RachelPearson36
 

Featured (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.