javascript - How to create multiple spoiler buttons -
when building website, decided wanted add show/hide (spoiler) section in order conserve space. here "working" code:
jquery:
$(document).ready(function(){ //waits page load $("a.spoilerbutton, a.spoilerbuttondark").click(function () { //attaches listeners $($(this).attr('href')).slidetoggle(1000, null); //open/closes spoiler }); }); css:
a.spoilerbutton, a.spoilerbuttondark { text-decoration:none; color:white; } a.spoilerbutton:hover, a.spoilerbuttondark:hover { color:grey; cursor: pointer; } a.spoiler { display:none; } html:
<div id="spoiler1" class="spoiler">content</div> <!--spoiler--> <div class="contentboxfooter"> <a href="#spoiler1" class = "spoilerbutton">show/hide</a> <!--button--> </div> what like:
- support multiple buttons
- a way link buttons appropriate spoiler @ place in html
problems facing:
- don't know proper way link button appropriate spoiler, or if i'm doing wrong
- current method uses href in anchor tag shifts page scroll location whenever clicked on
main question:
i thought using id tag in anchor tag tell script spoiler id was, although don't think id tags intended that. how should go doing this, or not proper way it?
i dont know if understand question correctly.
on page there 4 links open respective spoiler tags.
simple example, hope can you.
<!doctype html> <html> <head> <meta charset="utf-8"> <title></title> <style> .spoiler { display:none; width:100%; height:50px; background-color:red; margin-bottom:10px; } .contentboxfooter{position:absolute;bottom:10px;} </style> </head> <body> <div id="a1" class="spoiler">content</div> <div id="a2" class="spoiler">content</div> <div id="a3" class="spoiler">content</div> <div id="a4" class="spoiler">content</div> <div class="contentboxfooter"> <a href="a1" class = "spoilerbutton">show/hide</a> <a href="a2" class = "spoilerbutton">show/hide</a> <a href="a3" class = "spoilerbutton">show/hide</a> <a href="a4" class = "spoilerbutton">show/hide</a> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".spoilerbutton").click(function (e) { e.preventdefault() var foo=$(this).attr('href') $('#'+foo).slidetoggle(1000); }); }); </script> </body> </html>
Comments
Post a Comment