Accordion implementation using jQuery

Posted on November 19, 2014 by

"So what is an accordion?" you may be thinking to yourself. It's a very simple and sometimes user-friendly alternative when you need to fit quite a bit of content into a small space, not unlike a tabs component.

An accordion is most commonly used in F.A.Q.'s where you have questions and answers. Which, if just displayed straight to the page could potentially become incredibly long, stopping users from actually wanting to read or finding the answer they’re looking for.

That is also another good argument for accordion components, it allows users to see the outline of the content at a glance. Imagine if you were browsing a F.A.Q. and had to scroll all the way down through 20 different section just to find your answer. It would be a nightmare. If you could just scroll through the titles with content temporarily hidden, then it would make for a much better experience.

Implementation

Click on the topic and witness the magic around.

Why Do People Lie?
One reason people lie is to achieve personal power. Achieving personal power is helpful for someone who pretends to be more confident than he really is. For example, one of my friends threw a party at his house last month. He asked me to come to his party and bring a date. However, I didn't have a girlfriend. One of my other friends, who had a date to go to the party with, asked me about my date. I didn't want to be embarrassed, so I claimed that I had a lot of work to do. I said I could easily find a date even better than his if I wanted to. I also told him that his date was ugly. I achieved power to help me feel confident; however, I embarrassed my friend and his date. Although this lie helped me at the time, since then it has made me look down on myself.
Describe a Stressful Time in Your Life
Money causes teenagers to feel stress. It makes them feel bad about themselves and envy other people. My friend, for instance, lives with her family and has to share a room with her sister, who is very cute and intelligent. This girl wishes she could have her own room and have a lot of stuff, but she can't have these things because her family doesn't have much money. Her family's income is pretty low because her father is old and doesn't go to work. Her sister is the only one who works. Because her family can't buy her the things she wants, she feels a lot of stress and gets angry sometimes. Once, she wanted a beautiful dress to wear to a sweetheart dance. She asked her sister for some money to buy the dress. She was disappointed because her sister didn't have money to give her. She sat in silence for a little while and then started yelling out loud. She said her friends got anything they wanted but she didn't. Then she felt sorry for herself and asked why she was born into a poor family. Not having money has caused this girl to think negatively about herself and her family. It has caused a lot of stress in her life.
Describe College Life
Most students like the freedom they have in college. Usually college students live on their own, in the dormitory or in an apartment. This means they are free to come and go as they like. Their parents can't tell them when to get up, when to go to school, and when to come home. It also means that they are free to wear what they want. There are no parents to comment about their hair styles or their dirty jeans. Finally, they are free to listen to their favorite music without interference from parents.
The Best Place to Visit
Visakhapatnam is the most wonderful place to visit because of its variety of weather and its beautiful nature. Visitors to Visakhapatnam can find any weather they like. They can find cool temperatures in the summer; also they can find warm weather in the winter. They can find places that are difficult for humans to live in the summer because they are so hot. Or they can find places closed in the winter because of the snow. On the other hand, visitors can find the nature they like. They can find high mountains and low valleys. Visitors can find a huge forest, a dead desert, and a beautiful coast. So Visakhapatnam is the most wonderful place to visit because of its weather and nature.

Code

<html>
	<head>
		<style>
			.accordion{
				width:70%;
				margin: 0 auto;
			}
			.accordion .title{
				background-color: #E0CEF2 !important;
				padding:1.5%;
				color: white;
				cursor: pointer;
				border-bottom: 1px solid #fff !important;    
			}
			.accordion .content{
				display: none;
				background-color: #faf5ff !important;
				padding:1.5%;
			}
		</style>
		<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
		<script>
			$(document).ready(function(){
				$('.accordion > .section').click(function(){
					$('.accordion > .section > .content').slideUp("slow");
					
					var thisContent = $(this).children('.content');
					if(thisContent.css('display') == 'none')    
						thisContent.slideDown("fast");
					thisContent = null;
				});
			});
		</script>
	</head>
	<body>
		<div class="accordion">
			<div class="section">
				<div class="title">Why Do People Lie?</div>
				<div class="content">One reason people lie is to achieve personal power. Achieving personal power is helpful for someone who pretends to be more confident than he really is. For example, one of my friends threw a party at his house last month. He asked me to come to his party and bring a date. However, I didn't have a girlfriend. One of my other friends, who had a date to go to the party with, asked me about my date. I didn't want to be embarrassed, so I claimed that I had a lot of work to do. I said I could easily find a date even better than his if I wanted to. I also told him that his date was ugly. I achieved power to help me feel confident; however, I embarrassed my friend and his date. Although this lie helped me at the time, since then it has made me look down on myself.</div>
			</div>
			<div class="section">
				<div class="title">Describe a Stressful Time in Your Life</div>
				<div class="content">Money causes teenagers to feel stress. It makes them feel bad about themselves and envy other people. My friend, for instance, lives with her family and has to share a room with her sister, who is very cute and intelligent. This girl wishes she could have her own room and have a lot of stuff, but she can't have these things because her family doesn't have much money. Her family's income is pretty low because her father is old and doesn't go to work. Her sister is the only one who works. Because her family can't buy her the things she wants, she feels a lot of stress and gets angry sometimes. Once, she wanted a beautiful dress to wear to a sweetheart dance. She asked her sister for some money to buy the dress. She was disappointed because her sister didn't have money to give her. She sat in silence for a little while and then started yelling out loud. She said her friends got anything they wanted but she didn't. Then she felt sorry for herself and asked why she was born into a poor family. Not having money has caused this girl to think negatively about herself and her family. It has caused a lot of stress in her life.</div>
			</div>
			<div class="section">
				<div class="title">Describe College Life</div>
				<div class="content">Most students like the freedom they have in college. Usually college students live on their own, in the dormitory or in an apartment. This means they are free to come and go as they like. Their parents can't tell them when to get up, when to go to school, and when to come home. It also means that they are free to wear what they want. There are no parents to comment about their hair styles or their dirty jeans. Finally, they are free to listen to their favorite music without interference from parents.</div>
			</div>
			<div class="section">
				<div class="title">The Best Place to Visit</div>
				<div class="content">Visakhapatnam is the most wonderful place to visit because of its variety of weather and its beautiful nature. Visitors to Visakhapatnam can find any weather they like. They can find cool temperatures in the summer; also they can find warm weather in the winter. They can find places that are difficult for humans to live in the summer because they are so hot. Or they can find places closed in the winter because of the snow. On the other hand, visitors can find the nature they like. They can find high mountains and low valleys. Visitors can find a huge forest, a dead desert, and a beautiful coast. So Visakhapatnam is the most wonderful place to visit because of its weather and nature.</div>
			</div>
		</div>
	</body>
</html>
This website is made possible by displaying online advertisements to our visitors.
Please consider supporting by disabling your ad blocker.
loading comments...