I am having trouble creating an accordion menu for a left-hand navigation panel. I tried to follow the instructions on W3Schools, but I cannot get my demo page to work. One of the problems is that I have two kinds of blocks: one type of block which does not expand, because I have not yet done the subordinate pages; and another type of block which expands (the accordion buttons). I addressed this problem by creating two different kinds of accordions and two different kinds of nonexpanding blocks.
My goal:
1. uniform vertical spacing between the blocks;
2. all blocks the same width;
3. URLs for the first level pages (i.e., Home, Trails, Habitats) indented slightly and uniformly;
4. URLs for the second level pages (i.e., Trees, Shrubs, Wildflowers) uniformly indented a few spaces more (to show that they are a subset of the first level pages).
5. the plus and minus icons lined up uniformly for both first and second order expandable blocks.
Here is the demo page: http://wildadirondacks.org/accordion-example-2.html
Sadly, as you can all-too-clearly see, the accordions are not working as desired:
● On my desktop, the accordions open in Chrome, Firefox, Opera, and Safari. On my iPad, the accordions open in Chrome, Firefox, Opera Mini, and Safari. The accordions DO NOT open in Internet Explorer on my desktop. (As far as I know, Internet Explorer is not available as an app for the iPad.) I must have messed up the code somehow when I adapted it for my purposes. How do I fix the code so that the accordions open in Internet Explorer?
● The spacing isn't correct. The spacing seems to work as desired on my desktop in Chrome, Firefox, Opera, Safari, and Internet Explorer. However, on my iPad the spacing is messed up in all browsers: Chrome, Firefox, Opera Mini, and Safari. The accordion buttons (as opposed to the blocks that do not expand) are indented more than the nonexpanding blocks.
● Something weird is going on in the Shrubs panel. Scientific names of plants are, by tradition, italicized. The italics work fine in the Tree panel, but I get this weird symbol in the shrub panel: Â . This symbol appears in Firefox, Opera, and Safari on both the desktop and the iPad. The weird symbol also appears in Chrome on the iPad, although not in Chrome on my desktop.
● Another weird symbol appears when I try to use an apostrophe: ’s. This symbol appears in place of the apostrophe with all browsers on both the desktop and the iPad.
● The plus and minus icons are not uniformly aligned. For the second level accordion buttons (Trees and Shrubs), the icons appear a few spaces to the right, compared with where they appear on the first level accordion button (Trails).
Please help! I have spent about six hours trying to figure out how to fix these problems, and I have made no progress in solving them. Also, after I get the demo navigation panel working, I then have to translate this to the web site, where the left hand navigation panel is a server side include. I expect that will be another nightmare.
Thank you, Ellen
Here is the css:
Code:
<style>
body {
background: #f0f4fa;
}
button.accordion {
background-color: #e2e9f3;
cursor: pointer;
padding-bottom: .5em;
padding-top: .5em;
width: 25%;
text-align: left;
border: none;
font-size: 1em;
transition: 0.4s;
font-family: "Times New Roman", Times, serif;
}
button.accordionSecond {
background-color: #e2e9f3;
cursor: pointer;
padding-bottom: .5em;
padding-top: .5em;
width: 25%;
text-align: left;
border: none;
font-size: 1em;
transition: 0.4s;
text-indent: .5em;
font-family: "Times New Roman", Times, serif;
}
button.accordion.active, button.accordion:hover {
background-color: #e2e9f3;
}
button.accordionSecond.active, button.accordion:hover {
background-color: #e2e9f3;
}
button.accordion:after {
width: 25%;
background-color: #e2e9f3;
content: '\002B';
color: ##000000;
font-weight: bold;
float: right;
margin-left: 0px;
border: none;
}
button.accordionSecond:after {
width: 25%;
background-color: #e2e9f3;
content: '\002B';
color: ##000000;
font-weight: bold;
float: right;
margin-left: 0px;
border: none;
}
button.accordion.active:after {
content: "\2212";
border: none;
}
button.accordionSecond.active:after {
content: "\2212";
border: none;
}
div.panel {
margin-top: .5em;
margin-bottom: .5m;
width: 25%;
background-color: #ffffff;
color: #000000;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
font-family: "Times New Roman", Times, serif;
}
div.panel ul li {
color: #000000;
font-family: "Times New Roman", Times, serif;
padding-right: 4px;
line-height: 1.5em;
}
div.panel ul li a {
color: #000000;
font-family: "Times New Roman", Times, serif;
padding-right: 4px;
line-height: 1.5em;
}
.searchSection {
width:25%;
padding-top: 5%;
min-height: 50px;
padding-left: 0px;
}
div.noexpandFirst {
background-color: #e2e9f3;
padding-bottom: .5em;
padding-top: .5em;
padding-left: 0px;
margin-top: 0px;
margin-bottom: .5em;
margin-right: 0px;
margin-left: 0px;
width: 25%;
text-align: left;
border: none;
font-size: 1em;
text-indent: 6px;
font-family: "Times New Roman", Times, serif;
}
div.noexpandSecond {
background-color: #e2e9f3;
padding-left: 0px;
padding-bottom: .5em;
padding-top: .5em;
margin-top: 0px;
margin-bottom: .5em;
margin-right: 0px;
margin-left: 0px;
width: 25%;
text-align: left;
border: none;
font-size: 1em;
text-indent: 1em;
font-family: "Times New Roman", Times, serif;
}
div.noexpandFirst ul li a {
color: #000000;
font-size: 1em;
font-family: "Times New Roman", Times, serif;
}
div.noexpandFirst h2 a {
color: #000000;
font-size: 1em;
}
div.noexpandFirst a {
color: #000000;
font-size: 1em;
}
div.noexpandSecond ul li a {
color: #000000;
font-size: 1em;
margin-left: 1em;
}
div.noexpandSecond h2 a {
color: #000000;
font-size: 1em;
}
div.noexpandSecond a {
color: #000000;
font-size: 1em;
}
</style>
Here is the html:
HTML Code:
<div class="noexpandFirst">
<a href="index.html" title="Forever Wild"><strong>HOME: FOREVER WILD</strong></a></div>
<div class="noexpandSecond">
<a href="adirondack-geology.html" title="Adirondack Geology">Adirondack Geology
</a></div>
<div class="noexpandSecond">
<a href="adirondack-wilderness.html" title="Adirondack Wilderness">Wilderness</a></div>
<div class="noexpandSecond">
<a href="forever-wild-the-adirondack-park.html" title="The Adirondack Park">The Adirondack Park</a>
</div>
<button class="accordion"><strong>TRAILS</strong></button>
<div class="panel">
<ul>
<li><a href="adirondack-trails.html" title="Adirondack Trails">Adirondack Trails</a>
</li>
<li><a href="barnum-brook-interpretive-trail.html">Barnum Brook Trail (VIC)</a> </li>
<li><a href="boreal-life-interpretive-trail.html">Boreal Life Trail (VIC)</a> </li>
<li><a href="heron-marsh-interpretive-trail.html">Heron Marsh Trail (VIC)
</a></li>
<li><a href="jenkins-mountain-trail.html">Jenkins Mountain Trail (VIC)
</a></li>
<li><a href="loggers-loop-trail.html">Logger’s Loop Trail (VIC)
</a></li>
<li><a href="woods-and-waters-trail.html">Woods & Waters Trail (VIC)</a></li>
<li><a href="black-pond-trail.html">Black Pond Trail (VIC)</a></li>
<li><a href="long-pond-trail.html">Long Pond Trail (VIC)</a></li>
<li><a href="bobcat-trail.html">Bobcat Trail (VIC)</a></li>
<li><a href="silvi-trail.html">Silvi Trail (VIC)</a></li>
<li><a href="easy-street-trail.html">Easy Street Trail (VIC)</a></li>
<li><a href="fox-run-trail.html">Fox Run Trail (VIC)/a></li>
<li><a href="skidder-trail.html">Skidder Trail (VIC)</a></li>
<li><a href="esker-trail.html">Esker Trail (VIC)</a></li>
<li>Bloomingdale Bog Trail (Bloomingdale)</li>
<li>Henry’s Woods Trail (Lake Placid)</li>
<li>John Brown Farm Trails (Lake Placid)</li>
<li>Heaven Hill Trails (Lake Placid)</li>
<li>Penninsula Trails (Lake Placid)</li>
</ul>
</div>
<div class="noexpandFirst"> <a href="adirondack-habitats.html" title="Adirondack Habitats"><strong>HABITATS</strong></a></div>
<div class="noexpandSecond"> <a href="adirondack-wetlands.html" title="Adirondack Wetlands">Adirondack Wetlands</a></div>
<div class="noexpandSecond"> <a href="adirondack-mixed-wood-forest.html" title="Adirondack Mixed Wood Forest">Mixed Wood Forest</a></div>
<div class="noexpandSecond"> <a href="adirondack-northern-hardwood-forest.html" title="Adirondack Northern Hardwood Forest">Northern Hardwood Forest</a></div>
<div class="noexpandSecond"> <a href="adirondack-forest-succession.html" title="Adirondack Forest Succession">Forest Succession</a></div>
<div class="noexpandFirst"> <a href="adirondack-plants.html" title="Adirondack Plant Life"><strong>PLANTS</strong></a></div>
<button class="accordionSecond">Trees</button>
<div class="panel">
<ul>
<li><a href="adirondack-trees.html">Adirondack Trees</a></li>
<li><a href="trees-of-the-adirondacks-american-beech-fagus-grandifolia.html">American Beech (<em>Fagus grandifolia</em>)</a></li>
<li><a href="trees-of-the-adirondacks-balsam-fir-abies-balsamea.html">Balsam Fir (<em>Abies balsamea</em>)</a></li>
<li><a href="trees-of-the-adirondacks-bigtooth-aspen-populus-grandidentata.html">Bigtooth Aspen (<em>Populus</em> <em>grandidentata</em>)</a></li>
<li><a href="trees-of-the-adirondacks-black-cherry-prunus-serotina.html">Black Cherry (<em>Prunus serotina</em>)</a></li>
<li><a href="trees-of-the-adirondacks-black-spruce-picea-mariana.html">Black Spruce (<em>Picea mariana</em>)</a></li>
<li><a href="trees-of-the-adirondacks-eastern-hemlock-tsuga-canadensis.html">Eastern Hemlock (<em>Tsuga canadensis</em>)</a></li>
<li><a href="trees-of-the-adirondacks-eastern-hemlock-tsuga-canadensis.html">Eastern White Pine (<em>Pinus strobus</em>)</a></li>
<li><a href="trees-of-the-adirondacks-paper-birch-betula-papyrifera.html">Paper Birch (<em>Betula papyrifera </em>var.<em>papyrifera</em>)</a></li>
<li><a href="trees-of-the-adirondacks-red-maple-acer-rubrum.html">Red Maple (<em>Acer rubrum</em>)</a></li>
<li><a href="trees-of-the-adirondacks-red-spruce-picea-rubens.html">Red Spruce (<em>Picea rubens</em>)</a></li>
<li><a href="trees-of-the-adirondacks-striped-maple-acer-pensylvanicum.html">Striped Maple (<em>Acer pensylvanicum</em>)</a></li>
<li><a href="trees-of-the-adirondacks-sugar-maple-acer-saccharum-Marshall.html">Sugar Maple (<em>Acer saccharum</em>)</a></li>
<li><a href="trees-of-the-adirondacks-tamarack-larix-laricina.html">Tamarack (<em>Larix laricina</em>)</a></li>
<li><a href="trees-of-the-adirondacks-yellow-birch-betula-alleghaniensis.html">Yellow Birch (<em>Betula alleghaniensis</em>)</a></li>
<li><a href="adirondack-tree-list.html">Adirondack Tree List</a></li>
</ul>
</div>
<button class="accordionSecond">Shrubs</button>
<div class="panel">
<ul>
<li><a href="adirondack-shrubs.html">Adirondack Shrubs</a></li>
<li><a href="adirondack-shrubs-northern-wild-raisin-viburnum-nudum-l-var-cassinoides.html">Northern Wild Raisin (<em>Viburnum nudum</em> var <em>cassinoides</em>)</a></li>
<li><a href="adirondack-shrubs-red-elderberry-sambucus-racemosa-l-var-racemosa.html">Red Elderberry (<em>Sambucus racemosa</em>)</a></li>
<li><a href="adirondack-shrubs-white-meadowsweet-spiraea-alba-var-latifolia.html">White Meadowsweet (<em>Spiraea alba</em> var <em>latifolia</em>)</a></li>
</ul>
</div>
<div class="noexpandSecond"><a href="adirondack-wildflowers.html" title="Adirondack Wildflowers">Wildflowers</a></div>
<div class="noexpandSecond"><a href="adirondack-mosses.html" title="Adirondack Moss">Moss</a></div>
<div class="noexpandSecond"><a href="adirondack-ferns.html" title="Adirondack Ferns">Ferns</a></div>
<div class="noexpandFirst">
<a href="adirondack-wildlife.html" title="Adirondack Wildlife"><strong>WILDLIFE</strong></a></div>
<div class="noexpandFirst">
<a href="adirondack-fungi.html" title="Adirondack Fungi"><strong>FUNGI</strong></a></div>
Here is the Javascript:
Code:
<script>
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight){
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
}
}
</script>
<script>
var acc = document.getElementsByClassName("accordionSecond");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight){
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
}
}
</script>
Bookmarks