jquery - slideToggle with slideUp -
i have posted cv on website, , i've got different sections of hidden when page loads. used slidetoggle allow user click on each section open or close it. part working great. wanted user have option open or close sections @ once, instead of having scroll down page opening or closing each section 1 one. created "show all" option @ top of page. once tried work, things got wonky. here html (just sample), css, , jquery:
<div class="toggle"> <h2><span class="toggleicon">[+]</span> education</h2> <p>ph.d. in english, duke university, 2009-present<p> <p class="indent">certificate in college teaching</p> <p class="indent">certificate in feminist studies</p> <p>m.a. in english, brooklyn college, cuny, 2008</p> <p>b.a. in english , anthropology, university of texas, austin, 2005</p> </div> <div class="toggle"> <h2><span class="toggleicon">[+]</span> publications</h2> <h3>journal articles</h3> <p class="indent">"'after' theory," in <span>theory@buffalo: conditions of possibility</span> 13 (2009). print.</p> <h3>book chapters</h3> <p class="indent">"'i not want judgment of man': unstable animal-human boundary in linguistics , kafka's 'a report academy,'' in <span>of mice , men: animals in human culture</span>. newcastle upon tyne: cambridge scholars press, 2009. 81-91. print.</p> <h3>other</h3> <p class="indent">rev. of <span>chang , eng reconnected: original siamese twins in american culture</span>, cynthia wu, <span>imperfect unions: staging miscegenation in u.s. drama , fiction</span>, diana rebekkah paulin, , <span>racial indigestion: eating bodies in 19th century</span>, kyla wazana tompkins, in <span>american literature</span> 86.2 (2014): 394-396. print.</p> </div> #options { text-align: center; margin-left: -25px; } #toggleallicon { font-style: normal; font-weight: normal; cursor: pointer; font-size: 13px; } .toggle p, .toggle h3 { display: none; } $(function() { $(".toggleicon").click(function() { //when user clicks icon $(this).parent().parent().children("h3, p").slidetoggle(); //content slides down or }); }); $(function() { $("#toggleallicon").click(function() { if ( $(".toggle h3, .toggle p").is(":visible") ) { $(".toggle h3, .toggle p").slideup(function() { $(".toggle h3, toggle p").slidetoggle(); }); } else { $(".toggle h3, .toggle p").slidetoggle(); }; }); }); a lot of people working these menus want 1 section visible @ time. clarify, isn't issue. want users able have multiple sections open @ once if want. want them have option control sections @ same time. original code, when clicked "#toggleallicon", control entire document, if user had section open, section close , other sections open. want work when click icon, sections either open or close together. tried calling stop after slideup shut code down.
you can see happens above code: http://www.clare-eileen-callahan.com/ page starts dance. it's confused. imagine there not correct way more efficient way achieve effect want. appreciated!
in opinion easier if differentiate between when toggle should "show all" or "hide all". suggest using toggleclass determine toggle should (slideup or slidedown all)
$(function() { $("#toggleallicon").click(function() { $("#toggleallicon").toggleclass('show-all'); if($("#toggleallicon").hasclass('show-all')) { $(".toggle h3, .toggle p").slidedown(); } else { $(".toggle h3, .toggle p").slideup(); } }); }); see http://codepen.io/anon/pen/dcmak
hope helps bit. 1 scenario might want consider happens if user has individually expanded sections when click "show all"? , vice versa "hide all".
Comments
Post a Comment