-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathaccordions.php
184 lines (176 loc) · 6.28 KB
/
accordions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<section id="accordions">
<h1>Accordions</h1>
<h3>Plan</h3>
<ol>
<li>Mark up a few sections with a title and content</li>
<li>Set the height to all but the first to 0, and overflow to hidden on all of them</li>
<li>Bind click events on the titles to change the heights</li>
</ol>
<h3>Demo</h3>
<style>
#accordion section, #accordion .pointer, #accordion h1, #accordion p {
-webkit-transition: all 1.0s ease-in-out;
-moz-transition: all 1.0s ease-in-out;
-o-transition: all 1.0s ease-in-out;
-ms-transition: all 1.0s ease-in-out;
transition: all 1.0s ease-in-out;
}
#accordion {
margin-bottom:30px;
}
#accordion h1 {
font-size:20px;
background-color:rgba(255,0,0,0.3);
margin:0;
padding: 10px 10px 10px 30px;
}
#accordion h1 a {
color:black;
}
#accordion section {
overflow:hidden;
height:220px;
border:1px #333 solid;
}
#accordion p {
padding:0 10px;
color:black;
}
#accordion section.ac_hidden p:not(.pointer) {
color:#fff;
}
#accordion section.ac_hidden {
height:44px;
}
#accordion .pointer {
padding:0;
margin:10px 0 0 6px;
line-height:20px;
width:13px;
position:absolute;
}
#accordion section:not(.ac_hidden) h1 {
background-color:rgba(255,0,0,0.7);
}
#accordion section:not(.ac_hidden) .pointer {
display:block;
-webkit-transform:rotate(90deg);
-moz-transform:rotate(90deg);
-o-transform:rotate(90deg);
-ms-transform:rotate(90deg);
transform:rotate(90deg);
padding:0;
}
</style>
<script>
$(document).ready(function() {
$("#accordion section h1").click(function(e) {
$(this).parents().siblings("section").addClass("ac_hidden");
$(this).parents("section").removeClass("ac_hidden");
e.preventDefault();
});
});
</script>
<div id="accordion">
<section id="item1">
<p class="pointer">▶</p><h1><a href="#">A long paragraph</a></h1>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
</section>
<section id="item2" class="ac_hidden">
<p class="pointer">▶</p><h1><a href="#">A medium paragraph</a></h1>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
</section>
<section id="item3" class="ac_hidden">
<p class="pointer">▶</p><h1><a href="#">Two short paragraphs</a></h1>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante.</p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante.</p>
</section>
</div>
<p>Not sure if the use/abuse of Unicode is really a good idea, but you can see that it's pretty easy to get a simple accordion working.</p>
<p>I'm using classes again here to define different states, then using jQuery to turn them on and off. As always, I could use the :target pseudo selector, but I'd want to use preventDefault() onClick anyway to prevent the page skipping up and down, so I might as well just do it all in jQuery.</p>
<h3>The Code</h3>
<p>HTML:</p>
<pre class="prettyprint lang-html">
<div id="accordion">
<section id="item1">
<p class="pointer">&#9654;</p><h1><a href="#">A long paragraph</a></h1>
<p>Pellentesque habitant... </p>
</section>
<section id="item2" class="ac_hidden">
<p class="pointer">&#9654;</p><h1><a href="#">A medium paragraph</a></h1>
<p>Pellentesque habitant... </p>
</section>
<section id="item3" class="ac_hidden">
<p class="pointer">&#9654;</p><h1><a href="#">Two short paragraphs</a></h1>
<p>Pellentesque habitant... </p>
<p>Pellentesque habitant... </p>
</section>
</div>
</pre>
<p>CSS:</p>
<pre class="prettyprint lang-css">
#accordion section, #accordion .pointer, #accordion h1, #accordion p {
-webkit-transition: all 0.5s ease-in-out;
-moz-transition: all 0.5s ease-in-out;
-o-transition: all 0.5s ease-in-out;
-ms-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
}
#accordion {
margin-bottom:30px;
}
#accordion h1 {
font-size:20px;
background-color:rgba(255,0,0,0.3);
margin:0;
padding: 10px 10px 10px 30px;
}
#accordion h1 a {
color:black;
}
#accordion section {
overflow:hidden;
height:220px;
border:1px #333 solid;
}
#accordion p {
padding:0 10px;
color:black;
}
#accordion section.ac_hidden p:not(.pointer) {
color:#fff;
}
#accordion section.ac_hidden {
height:44px;
}
#accordion .pointer {
padding:0;
margin:10px 0 0 6px;
line-height:20px;
width:13px;
position:absolute;
}
#accordion section:not(.ac_hidden) h1 {
background-color:rgba(255,0,0,0.7);
}
#accordion section:not(.ac_hidden) .pointer {
display:block;
-webkit-transform:rotate(90deg);
-moz-transform:rotate(90deg);
-o-transform:rotate(90deg);
-ms-transform:rotate(90deg);
transform:rotate(90deg);
padding:0;
}
</pre>
<p>Plus a bit of javascript to turn the classes on and off.</p>
<pre class="prettyprint lang-js">
$(document).ready(function() {
$("#accordion section h1").click(function(e) {
$(this).parents().siblings("section").addClass("ac_hidden");
$(this).parents("section").removeClass("ac_hidden");
e.preventDefault();
});
});
</pre>
</section>