SlideShare ist ein Scribd-Unternehmen logo
1 von 5
Sliding touch panels for mobile web – HTML5, CSS3
Post from my blog: http://jbkflex.wordpress.com/2012/02/09/sliding-touch-panels-for-mobile-web-html5-
css3/

In this tutorial I will discuss about creating a sliding touch panel meant for mobile web apps. You must have seen, that
in iPhone or iPod settings when you tap on a menu item the whole panel slides out and a detailed panel slides in. I
am going to talk exactly on how to create something similar for iPhones and Android devices using CSS3 transitions
and a little bit of java script. Even those who have worked with mobile web apps using Sencha Touch must be aware
of sliding touch panels. Before moving on check out the demo on an iPhone or Android device,
Demo: http://jbk404.site50.net/css3/slidingtouchpanel/
You can check the demo in a desktop browser also – Google Chrome or Safari.
The concept
The concept is to have two equally sized panels horizontally laid out inside a wrapper container. The wrapper
occupies the dimensions of the mobile device. The size of each panel is same as the wrapper. Now, the wrapper has
overflow as hidden so you only see one panel at a time. Now that the basic set up is done, we just need to move the
two panels left and right. The image below visualizes the things that I have spoken above.




The sliding of the panels can be done using CSS3 transitions and transformations and the movement is very smooth.
I have another image below which shows the two panels side by side. Tap/click on the menu item in panel1 and it
takes you to panel2. Tap on the back button image in panel2 and it takes you back again to panel1. I have picked the
label for the menu items straight from my mobile app. You can change it according to your needs.
Two panels side by side

I will not go into the design aspect of the menu and the rest of the pages. You can always check out the CSS content
from the code. The menu itself can help you in some other project as it has got an iPhone look and feel with nice
rounded corners.
Getting started
Let’s see how to place the two panels horizontally inside a wrapper container. This needs some HTML and CSS,

<section id="wrapper">

  <dl id="panelContainer">

     <dd id="panel1">

        <!--contents of panel1 goes here -->

     </dd>

     <dd id="panel2">

        <!--contents of panel2 goes here -->

     </dd>

  </dl>

</section>
The wrapper is a HTML5 section tag. The panel container is a dl element and holds two dd elements –
panel 1 andpanel2. The two dd elements are placed horizontally using float:left CSS property. Note that only
float:left is not enough for them to be placed horizontally. There must be enough width inside the panelContainer dl
element to hold the two panels horizontally. I will talk about this later. Coming back to the HTML code above, this is
the basic HTML skeleton needed for the app. If you see the source code of the demo app you will see the contents
inside the panels (the header bar, navigation menu etc). Let’s see the CSS needed for the HTML block above,

#wrapper

{

    width:100%;

    height:auto;

    overflow:hidden;

}

#wrapper dl

{

    -webkit-transition:-webkit-transform ease;

}

#wrapper dl dd

{

    float:left;

}


The wrapper occupies the entire width of the device as it is 100%. For the dl (panelContainer) element I have set a
web-kit transition property. So whenever we move the dl element using CSS3 transformation functions (eg. -webkit-
transform = translateX(20px);) the movement is smooth and continuous over time. Try to remove the transition style
property from dl, and now if the panel container moves the movement will not be continuous. It will jump to its new
position. Remember transition is important for the sliding movement.
For panel1 and panel2 to be placed horizontally side by side enough width must be set to the panel container. I have
done this in java script code (when window loads, check out the source of the demo app) so that based on the device
width I can set the values accordingly. This is how I have done it,

panelContainer = document.getElementById("panelContainer");

panelContainer.style.width = (2 * window.innerWidth) + "px";
panel1 = document.getElementById("panel1");

panel1.style.width = window.innerWidth + "px";




panel2 = document.getElementById("panel2");

panel2.style.width = window.innerWidth + "px";


The panel container gets a width of twice the panels inside it. So it has got enough room now.
Let’s slide the Panels
If you have checked out the demo already, you might have clicked on one of the menu items to slide to the other
panel. For each of the menu items an event listener is registered. So when you tap/click on it a function is called –
navigateTo(event) and the event object is passed as a parameter. I have two of them as example below,

<dd onclick="navigateTo(event)">

  About

  <img src="images/arrow_grey.png"/>

</dd>

<dd onclick="navigateTo(event)">

  Work Areas

  <img src="images/arrow_grey.png"/>

</dd>


Similarly the back button also registers a listener to listen to click events.

backBtn = document.getElementById("backBtn");

backBtn.addEventListener("click", backBtnClicked, false);


Inside the navigateTo() event listener function, I get the label of the menu item clicked and also translate the panel
container to an x-distance of -window.innerWidth. The negative value is for moving the panel container to the left
so that panel1 slides out and panel2 slides in. The value of window.innerWidth is same as the individual panel
widths. I have used this value to auto adjust to every device screen – iPhone, iPad, Android, desktop browsers. Now,
the panel container moves to the left exactly a distance of the width of panel1, hence it looks like panel1 has slide
out. For iPhone it is -320px.

function navigateTo(event) {
panelContainer.style.webkitTransform = "translate3d(-" + window.innerWidth + "px, 0,
0)";

    panelContainer.style.webkitTransitionDuration = speed + "ms";




    clickedMenuText = event.currentTarget.firstChild.nodeValue.trim();

    headerText.innerText = clickedMenuText;

}


I have used translate3d(x,y,x) function over translateX() as the former one is hardware accelerated and helps in
optimization. You can also set the speed of transition by setting the webkitTransitionDuration value.
Similarly when you tap on the back button image in panel2 the panel container is again translated back to x=0 i.e the
original position. Hence it looks like panel1 has slide in.

function backBtnClicked() {

    panelContainer.style.webkitTransform = "translate3d(0, 0, 0)";

    panelContainer.style.webkitTransitionDuration = speed + "ms";

}


The entire concept of sliding effect is to move the panel container back and forth by a distance value equal to the
width of its panel which is equal to the device’s browser screen width.
For the full code check out the source of the demo app below,
Demo: http://jbk404.site50.net/css3/slidingtouchpanel/
The demo is meant for web-kit browsers only. So check in iPhone/iPod/iPad, Android browsers and for desktop check
out in Google Chrome or Safari.

Weitere ähnliche Inhalte

Kürzlich hochgeladen

MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
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
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 

Kürzlich hochgeladen (20)

MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
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
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 

Empfohlen

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
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 

Empfohlen (20)

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...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

Sliding touch panels for mobile web – HTML5, CSS3

  • 1. Sliding touch panels for mobile web – HTML5, CSS3 Post from my blog: http://jbkflex.wordpress.com/2012/02/09/sliding-touch-panels-for-mobile-web-html5- css3/ In this tutorial I will discuss about creating a sliding touch panel meant for mobile web apps. You must have seen, that in iPhone or iPod settings when you tap on a menu item the whole panel slides out and a detailed panel slides in. I am going to talk exactly on how to create something similar for iPhones and Android devices using CSS3 transitions and a little bit of java script. Even those who have worked with mobile web apps using Sencha Touch must be aware of sliding touch panels. Before moving on check out the demo on an iPhone or Android device, Demo: http://jbk404.site50.net/css3/slidingtouchpanel/ You can check the demo in a desktop browser also – Google Chrome or Safari. The concept The concept is to have two equally sized panels horizontally laid out inside a wrapper container. The wrapper occupies the dimensions of the mobile device. The size of each panel is same as the wrapper. Now, the wrapper has overflow as hidden so you only see one panel at a time. Now that the basic set up is done, we just need to move the two panels left and right. The image below visualizes the things that I have spoken above. The sliding of the panels can be done using CSS3 transitions and transformations and the movement is very smooth. I have another image below which shows the two panels side by side. Tap/click on the menu item in panel1 and it takes you to panel2. Tap on the back button image in panel2 and it takes you back again to panel1. I have picked the label for the menu items straight from my mobile app. You can change it according to your needs.
  • 2. Two panels side by side I will not go into the design aspect of the menu and the rest of the pages. You can always check out the CSS content from the code. The menu itself can help you in some other project as it has got an iPhone look and feel with nice rounded corners. Getting started Let’s see how to place the two panels horizontally inside a wrapper container. This needs some HTML and CSS, <section id="wrapper"> <dl id="panelContainer"> <dd id="panel1"> <!--contents of panel1 goes here --> </dd> <dd id="panel2"> <!--contents of panel2 goes here --> </dd> </dl> </section>
  • 3. The wrapper is a HTML5 section tag. The panel container is a dl element and holds two dd elements – panel 1 andpanel2. The two dd elements are placed horizontally using float:left CSS property. Note that only float:left is not enough for them to be placed horizontally. There must be enough width inside the panelContainer dl element to hold the two panels horizontally. I will talk about this later. Coming back to the HTML code above, this is the basic HTML skeleton needed for the app. If you see the source code of the demo app you will see the contents inside the panels (the header bar, navigation menu etc). Let’s see the CSS needed for the HTML block above, #wrapper { width:100%; height:auto; overflow:hidden; } #wrapper dl { -webkit-transition:-webkit-transform ease; } #wrapper dl dd { float:left; } The wrapper occupies the entire width of the device as it is 100%. For the dl (panelContainer) element I have set a web-kit transition property. So whenever we move the dl element using CSS3 transformation functions (eg. -webkit- transform = translateX(20px);) the movement is smooth and continuous over time. Try to remove the transition style property from dl, and now if the panel container moves the movement will not be continuous. It will jump to its new position. Remember transition is important for the sliding movement. For panel1 and panel2 to be placed horizontally side by side enough width must be set to the panel container. I have done this in java script code (when window loads, check out the source of the demo app) so that based on the device width I can set the values accordingly. This is how I have done it, panelContainer = document.getElementById("panelContainer"); panelContainer.style.width = (2 * window.innerWidth) + "px";
  • 4. panel1 = document.getElementById("panel1"); panel1.style.width = window.innerWidth + "px"; panel2 = document.getElementById("panel2"); panel2.style.width = window.innerWidth + "px"; The panel container gets a width of twice the panels inside it. So it has got enough room now. Let’s slide the Panels If you have checked out the demo already, you might have clicked on one of the menu items to slide to the other panel. For each of the menu items an event listener is registered. So when you tap/click on it a function is called – navigateTo(event) and the event object is passed as a parameter. I have two of them as example below, <dd onclick="navigateTo(event)"> About <img src="images/arrow_grey.png"/> </dd> <dd onclick="navigateTo(event)"> Work Areas <img src="images/arrow_grey.png"/> </dd> Similarly the back button also registers a listener to listen to click events. backBtn = document.getElementById("backBtn"); backBtn.addEventListener("click", backBtnClicked, false); Inside the navigateTo() event listener function, I get the label of the menu item clicked and also translate the panel container to an x-distance of -window.innerWidth. The negative value is for moving the panel container to the left so that panel1 slides out and panel2 slides in. The value of window.innerWidth is same as the individual panel widths. I have used this value to auto adjust to every device screen – iPhone, iPad, Android, desktop browsers. Now, the panel container moves to the left exactly a distance of the width of panel1, hence it looks like panel1 has slide out. For iPhone it is -320px. function navigateTo(event) {
  • 5. panelContainer.style.webkitTransform = "translate3d(-" + window.innerWidth + "px, 0, 0)"; panelContainer.style.webkitTransitionDuration = speed + "ms"; clickedMenuText = event.currentTarget.firstChild.nodeValue.trim(); headerText.innerText = clickedMenuText; } I have used translate3d(x,y,x) function over translateX() as the former one is hardware accelerated and helps in optimization. You can also set the speed of transition by setting the webkitTransitionDuration value. Similarly when you tap on the back button image in panel2 the panel container is again translated back to x=0 i.e the original position. Hence it looks like panel1 has slide in. function backBtnClicked() { panelContainer.style.webkitTransform = "translate3d(0, 0, 0)"; panelContainer.style.webkitTransitionDuration = speed + "ms"; } The entire concept of sliding effect is to move the panel container back and forth by a distance value equal to the width of its panel which is equal to the device’s browser screen width. For the full code check out the source of the demo app below, Demo: http://jbk404.site50.net/css3/slidingtouchpanel/ The demo is meant for web-kit browsers only. So check in iPhone/iPod/iPad, Android browsers and for desktop check out in Google Chrome or Safari.