SlideShare ist ein Scribd-Unternehmen logo
1 von 13
CSS3 Expandable-Collapsible Panel for iPhone
Many of you mighthave worked upon or have used a collapsible panel in one ofyour projects before reading this
post.And the bestchances were that the collapsible panel core logic was developed using traditional methods of
animating HTML objects using the setTimeout() or the setInterval() methods.Here Ihave listed down a quick
example of building a collapsible/expandable panel using the latest CSS3 technique of transitions. Of course,you
have to use a little bit of java scriptfor handling the style elements dynamically.This example is mainlyfocused
oniPhone (iOS) and Android touch devices and targets the mobile webkit browsers.You can also use the same
code for desktop web kit browsers such as Chrome and Safari,and a little generalization ofthe code would let you
use the application for all the other browsers supporting the newer CSS3 style rules (e.g. latestversion of Firefox).
Check out the images below,ofthe application in portraitand landscape mode ofmyiPod. And here is the link to the
demo app,check in Safari or Chrome.
http://jbk404.site50.net/html5/mobile/collapsiblepanel/
Expanded in portrait mode
Collapsed in portrait mode
Landscape mode
The HTML part
The HTML code block necessaryfor the app is below.We have a main div block that holds the header div and the
content div. The main idea is to hide the contentdiv when the main div’s heightis reduced for it to collapse and vice
versa. Below,I have two images thatwill explain the process better,
Expanded state of panel
Collapsed state of panel
<div id="mainHolder">
<div id="header">
<img src="images/collapse1.png" />
</div>
<div id="content">
<p>This is a collapsible panel</p>
<img src="images/bird.jpg" height="130"/>
</div>
</div>
The CSS/CSS3 part
Nothing fancy, very simple and is almostselfexplanatory.Lets look at it,
*{
margin:0; padding:0;
}
body
{
font-family:helvetica;
font-size:13px;
padding:20px;
}
#mainHolder
{
width:100%;
height:200px;
border:1px solid #9a9a9a;
background-color:#ffa500;
-webkit-transition-delay:0.1s;
-webkit-transition-duration:0.2s;
-webkit-transition-property:height;
}
#header
{
width:100%;
height:30px;
background-color:#ddd;
border-bottom:1px solid #9a9a9a;
overflow:hidden;
}
#header img
{
float:right;
}
#content
{
width:100%;
display:block;
margin:5px;
}
The main thing to notice is the CSS3 transition properties thatI have used for mainHolder. I have set the height as
the CSS3 transition propertyso that whenever there is a change in the heightof mainHolder the transition is smooth
and looks continuous over time.Note the other two properties delayfor a delay in start of the transition
and durationfor the overall duration of the transition.This will actas an alternative for the traditional
setTimeout/setInterval method for animating the collapse and exapansion ofthe main holder.Now, whenever we
change the heightof mainHolder,which we will do in our java scriptblock the animation will be smooth and
continuous.
The JavaScript part
I have kept it very simple and easy.I have added comments for clarity. Lets look at it,
<script type="text/javascript">
//all the variables
var header = null;
var content = null;
var mainHolder = null;
var expandCollapseBtn = null;
var heightValue = 0;
//get the instance of the necessary components
header = document.getElementById("header");
content = document.getElementById("content");
mainHolder = document.getElementById("mainHolder");
expandCollapseBtn = header.getElementsByTagName('img')[0];
//initially store the height of mainHolder for later use.
heightValue = mainHolder.offsetHeight;
//add events
header.addEventListener('click', handleClick, false);
mainHolder.addEventListener('webkitTransitionEnd',transitionEndHandler,false);
//event handler functions
function handleClick() {
if(expandCollapseBtn.src.search('collapse') !=-1)
{
mainHolder.style.height = "30px";
content.style.display = "none";
}
else
{
mainHolder.style.height = heightValue + "px";
}
}
function transitionEndHandler() {
if(expandCollapseBtn.src.search('collapse') !=-1)
{
expandCollapseBtn.src = "images/expand1.png";
}
else{
expandCollapseBtn.src = "images/collapse1.png";
content.style.display = "block";
}
}
</script>
The header gets a click event registered to it, so that when user taps/clicks on the header,the panel either collapses
or expands based on its current state.The action is handled in the handleClick function.
header.addEventListener('click', handleClick, false);
Initialy when the app loads the panel is expanded and the header icon is a minus. Inside the handleClick() function
we do a toggling maneouver.Firstwe check if the icon on the right of the header is a minus (collapse), that means
now you can collapse the panel by tapping on the header.If it’s a minus then inside the if block we setthe heightof
the main holder to 30px which is the heightof the header.The panel then collapses.Also we setthe displayof
thecontent div to none. This will not only hide the content area but also will free the space of the contentand let the
panel collapse.Similarlyin the else block which is meantfor the plus (expand) icon we setthe height of the main
holder again to its initial heightwhich we stored in the heightValue variable. Since there is a change in heightthe
effect is smooth and continuous as Ihave already talked about it. This expands the panel once again.So the toggling
expand-collapse behavior is implemented.
if(expandCollapseBtn.src.search('collapse') !=-1)
{
mainHolder.style.height = "30px";
content.style.display = "none";
}
else
{
mainHolder.style.height = heightValue + "px";
}
Now, the change of icon takes place in the next function which is called when each of the transition ends i.e expand
to collapse and collapse to expand. To listen to these kind of events we have registered
a webkitTransitionEndevent handler.It is a JavaScript event that is synonymous to the CSS3 transition property.
mainHolder.addEventListener('webkitTransitionEnd',transitionEndHandler,false);
After the end of each transition in heightthe transitionEndHandler function is called which controls the change of
the icon.
function transitionEndHandler() {
if(expandCollapseBtn.src.search('collapse') !=-1)
{
expandCollapseBtn.src = "images/expand1.png";
}
else{
expandCollapseBtn.src = "images/collapse1.png";
content.style.display = "block";
}
}
Also if you notice in the else block of the function I have set the displayof the contentagain to block. This ensures
that after the panel expands the content is displayed.Now,you may ask why the displayof the contentis changed in
separate functions.This is because,I have followed the process offirst hiding the content before the panel is
collapsed and then displaying the content again after the panel is expanded.This is all you need to make a very
smooth collapsible/expandable panel.Below is the full code,
<!DOCTYPE html>
<html>
<head>
<title>CSS3 Collapsible Panel</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width; initial-scale=1.0; minimum-
scale=1.0; maximum-scale=1.0;"/>
<style type="text/css">
*{
margin:0; padding:0;
}
body
{
font-family:helvetica;
font-size:13px;
padding:20px;
}
#mainHolder
{
width:100%;
height:200px;
border:1px solid #9a9a9a;
background-color:#ffa500;
-webkit-transition-delay:0.1s;
-webkit-transition-duration:0.2s;
-webkit-transition-property:height;
}
#header
{
width:100%;
height:30px;
background-color:#ddd;
border-bottom:1px solid #9a9a9a;
overflow:hidden;
}
#header img
{
float:right;
}
#content
{
width:100%;
display:block;
margin:5px;
}
</style>
</head>
<body>
<div id="mainHolder">
<div id="header">
<img src="images/collapse1.png" />
</div>
<div id="content">
<p>This is a collapsible panel</p>
</div>
</div>
</body>
<script type="text/javascript">
//all the variables
var header = null;
var content = null;
var mainHolder = null;
var expandCollapseBtn = null;
var heightValue = 0;
//get the instance of the necessary components
header = document.getElementById("header");
content = document.getElementById("content");
mainHolder = document.getElementById("mainHolder");
expandCollapseBtn = header.getElementsByTagName('img')[0];
//initially store the height of mainHolder for later use.
heightValue = mainHolder.offsetHeight;
//add events
header.addEventListener('click', handleClick, false);
mainHolder.addEventListener('webkitTransitionEnd',transitionEndHandler,false);
//event handler functions
function handleClick() {
if(expandCollapseBtn.src.search('collapse') !=-1)
{
mainHolder.style.height = "30px";
content.style.display = "none";
}
else
{
mainHolder.style.height = heightValue + "px";
}
}
function transitionEndHandler() {
if(expandCollapseBtn.src.search('collapse') !=-1)
{
expandCollapseBtn.src = "images/expand1.png";
}
else{
expandCollapseBtn.src = "images/collapse1.png";
content.style.display = "block";
}
}
</script>
</html>
Do ignore the indentation of the example code blocks in this tutorial, I had some issues with the editor.But anyways,
you can try the demo in your touch phone.Let me know if you have better ideas using CSS3.

Weitere ähnliche Inhalte

Kürzlich hochgeladen

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Kürzlich hochgeladen (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - 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
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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
 

Empfohlen

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)
 

Empfohlen (20)

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...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

CSS3 collapsible/expandable panel for iPhone/Android

  • 1. CSS3 Expandable-Collapsible Panel for iPhone Many of you mighthave worked upon or have used a collapsible panel in one ofyour projects before reading this post.And the bestchances were that the collapsible panel core logic was developed using traditional methods of animating HTML objects using the setTimeout() or the setInterval() methods.Here Ihave listed down a quick example of building a collapsible/expandable panel using the latest CSS3 technique of transitions. Of course,you have to use a little bit of java scriptfor handling the style elements dynamically.This example is mainlyfocused oniPhone (iOS) and Android touch devices and targets the mobile webkit browsers.You can also use the same code for desktop web kit browsers such as Chrome and Safari,and a little generalization ofthe code would let you use the application for all the other browsers supporting the newer CSS3 style rules (e.g. latestversion of Firefox). Check out the images below,ofthe application in portraitand landscape mode ofmyiPod. And here is the link to the demo app,check in Safari or Chrome. http://jbk404.site50.net/html5/mobile/collapsiblepanel/ Expanded in portrait mode
  • 2. Collapsed in portrait mode Landscape mode The HTML part The HTML code block necessaryfor the app is below.We have a main div block that holds the header div and the content div. The main idea is to hide the contentdiv when the main div’s heightis reduced for it to collapse and vice versa. Below,I have two images thatwill explain the process better,
  • 3. Expanded state of panel Collapsed state of panel <div id="mainHolder"> <div id="header"> <img src="images/collapse1.png" /> </div> <div id="content"> <p>This is a collapsible panel</p> <img src="images/bird.jpg" height="130"/> </div> </div> The CSS/CSS3 part Nothing fancy, very simple and is almostselfexplanatory.Lets look at it, *{
  • 4. margin:0; padding:0; } body { font-family:helvetica; font-size:13px; padding:20px; } #mainHolder { width:100%; height:200px; border:1px solid #9a9a9a; background-color:#ffa500; -webkit-transition-delay:0.1s; -webkit-transition-duration:0.2s; -webkit-transition-property:height; } #header { width:100%; height:30px; background-color:#ddd; border-bottom:1px solid #9a9a9a;
  • 5. overflow:hidden; } #header img { float:right; } #content { width:100%; display:block; margin:5px; } The main thing to notice is the CSS3 transition properties thatI have used for mainHolder. I have set the height as the CSS3 transition propertyso that whenever there is a change in the heightof mainHolder the transition is smooth and looks continuous over time.Note the other two properties delayfor a delay in start of the transition and durationfor the overall duration of the transition.This will actas an alternative for the traditional setTimeout/setInterval method for animating the collapse and exapansion ofthe main holder.Now, whenever we change the heightof mainHolder,which we will do in our java scriptblock the animation will be smooth and continuous. The JavaScript part I have kept it very simple and easy.I have added comments for clarity. Lets look at it, <script type="text/javascript"> //all the variables var header = null; var content = null; var mainHolder = null; var expandCollapseBtn = null; var heightValue = 0;
  • 6. //get the instance of the necessary components header = document.getElementById("header"); content = document.getElementById("content"); mainHolder = document.getElementById("mainHolder"); expandCollapseBtn = header.getElementsByTagName('img')[0]; //initially store the height of mainHolder for later use. heightValue = mainHolder.offsetHeight; //add events header.addEventListener('click', handleClick, false); mainHolder.addEventListener('webkitTransitionEnd',transitionEndHandler,false); //event handler functions function handleClick() { if(expandCollapseBtn.src.search('collapse') !=-1) { mainHolder.style.height = "30px"; content.style.display = "none"; } else { mainHolder.style.height = heightValue + "px";
  • 7. } } function transitionEndHandler() { if(expandCollapseBtn.src.search('collapse') !=-1) { expandCollapseBtn.src = "images/expand1.png"; } else{ expandCollapseBtn.src = "images/collapse1.png"; content.style.display = "block"; } } </script> The header gets a click event registered to it, so that when user taps/clicks on the header,the panel either collapses or expands based on its current state.The action is handled in the handleClick function. header.addEventListener('click', handleClick, false); Initialy when the app loads the panel is expanded and the header icon is a minus. Inside the handleClick() function we do a toggling maneouver.Firstwe check if the icon on the right of the header is a minus (collapse), that means now you can collapse the panel by tapping on the header.If it’s a minus then inside the if block we setthe heightof the main holder to 30px which is the heightof the header.The panel then collapses.Also we setthe displayof thecontent div to none. This will not only hide the content area but also will free the space of the contentand let the panel collapse.Similarlyin the else block which is meantfor the plus (expand) icon we setthe height of the main holder again to its initial heightwhich we stored in the heightValue variable. Since there is a change in heightthe effect is smooth and continuous as Ihave already talked about it. This expands the panel once again.So the toggling expand-collapse behavior is implemented. if(expandCollapseBtn.src.search('collapse') !=-1) {
  • 8. mainHolder.style.height = "30px"; content.style.display = "none"; } else { mainHolder.style.height = heightValue + "px"; } Now, the change of icon takes place in the next function which is called when each of the transition ends i.e expand to collapse and collapse to expand. To listen to these kind of events we have registered a webkitTransitionEndevent handler.It is a JavaScript event that is synonymous to the CSS3 transition property. mainHolder.addEventListener('webkitTransitionEnd',transitionEndHandler,false); After the end of each transition in heightthe transitionEndHandler function is called which controls the change of the icon. function transitionEndHandler() { if(expandCollapseBtn.src.search('collapse') !=-1) { expandCollapseBtn.src = "images/expand1.png"; } else{ expandCollapseBtn.src = "images/collapse1.png"; content.style.display = "block"; } } Also if you notice in the else block of the function I have set the displayof the contentagain to block. This ensures that after the panel expands the content is displayed.Now,you may ask why the displayof the contentis changed in separate functions.This is because,I have followed the process offirst hiding the content before the panel is
  • 9. collapsed and then displaying the content again after the panel is expanded.This is all you need to make a very smooth collapsible/expandable panel.Below is the full code, <!DOCTYPE html> <html> <head> <title>CSS3 Collapsible Panel</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width; initial-scale=1.0; minimum- scale=1.0; maximum-scale=1.0;"/> <style type="text/css"> *{ margin:0; padding:0; } body { font-family:helvetica; font-size:13px; padding:20px; } #mainHolder { width:100%; height:200px; border:1px solid #9a9a9a; background-color:#ffa500; -webkit-transition-delay:0.1s;
  • 11. <body> <div id="mainHolder"> <div id="header"> <img src="images/collapse1.png" /> </div> <div id="content"> <p>This is a collapsible panel</p> </div> </div> </body> <script type="text/javascript"> //all the variables var header = null; var content = null; var mainHolder = null; var expandCollapseBtn = null; var heightValue = 0; //get the instance of the necessary components header = document.getElementById("header"); content = document.getElementById("content"); mainHolder = document.getElementById("mainHolder"); expandCollapseBtn = header.getElementsByTagName('img')[0];
  • 12. //initially store the height of mainHolder for later use. heightValue = mainHolder.offsetHeight; //add events header.addEventListener('click', handleClick, false); mainHolder.addEventListener('webkitTransitionEnd',transitionEndHandler,false); //event handler functions function handleClick() { if(expandCollapseBtn.src.search('collapse') !=-1) { mainHolder.style.height = "30px"; content.style.display = "none"; } else { mainHolder.style.height = heightValue + "px"; } } function transitionEndHandler() { if(expandCollapseBtn.src.search('collapse') !=-1) { expandCollapseBtn.src = "images/expand1.png";
  • 13. } else{ expandCollapseBtn.src = "images/collapse1.png"; content.style.display = "block"; } } </script> </html> Do ignore the indentation of the example code blocks in this tutorial, I had some issues with the editor.But anyways, you can try the demo in your touch phone.Let me know if you have better ideas using CSS3.