javascript - create and use a CSS form in <Style>, not in <Body> with HTML -
i have learnt html , have small program.
var acc = document.getelementsbyclassname("accordion"); var i; (i = 0; < acc.length; i++) { acc[i].onclick = function(){ this.classlist.toggle("active"); this.nextelementsibling.classlist.toggle("show"); } }
.triangleright { width: 0; height: 0; border: solid 10px; border-color: transparent transparent transparent green ; } .triangledown { width: 0; height: 0; border: solid 10px; border-color: white transparent transparent transparent ; } button.accordion { background-color: rgb(213,227,233); color: #444; cursor: pointer; padding: 18px; width: 100%; border: none; text-align: left; outline: none; font-size: 15px; transition: 0.4s; } button.accordion.active { background-color: rgb(0,56,96); color: white; } button.accordion:after { /* content:"/2795"; */ content: "<div class="triangleright"></div>"; font-size: 13px; float:left; margin-left: 5px; } button.accordion.active:after { /* content:"/2796"; */ content: "<div class="triangledown"></div>"; } div.panel { padding: 0 18px; background-color: white; max-height: 0; overflow: hidden; transition: 0.6s ease-in-out; opacity: 0; } div.panel.show { opacity: 1; max-height: 500px; }
<button class="accordion"> title</button> <div class="panel"> <p>content</p> </div>
i need create small triange before "title". when insert triangle form have created button.accordion:after
, button.accordion.active:after
, not work.
you can't create html elements in content property of css, more info
alternatively can have approach, can put button in div, , in div can have absolute position element shows arrow, , can handle div same have handle nextelementsibling...
here fiddle solves issue
html:
<div class="parent"> <div class="arrow triangleright"></div> <button class="accordion"> title</button> <div class="panel"> <p>content</p> </div> </div>
javascript:
var acc = document.getelementsbyclassname("accordion"); var accdiv = document.getelementsbyclassname("arrow"); var i; (i = 0; < acc.length; i++) { acc[i].onclick = function() { onaccclick(this); } } (i = 0; < accdiv.length; i++) { accdiv[i].onclick = function() { onaccclick(this.nextelementsibling); } } function onaccclick(elem) { elem.classlist.toggle("active"); elem.nextelementsibling.classlist.toggle("show"); elem.previouselementsibling.classlist.toggle("triangledown"); }
css:
.triangleright { width: 0; height: 0; border: solid 10px; border-color: transparent transparent transparent green; } .triangledown { top: 22px !important; right: 13px !important; width: 0; height: 0; border: solid 10px; border-color: white transparent transparent transparent; } div.arrow { cursor: pointer; } button.accordion { background-color: rgb(213, 227, 233); color: #444; cursor: pointer; padding: 18px; width: 100%; border: none; text-align: left; outline: none; font-size: 15px; transition: 0.4s; } button.accordion.active { background-color: rgb(0, 56, 96); color: white; } .parent { position: relative } div.panel { padding: 0 18px; background-color: white; max-height: 0; overflow: hidden; transition: 0.6s ease-in-out; opacity: 0; } .arrow { position: absolute; top: 16px; right: 10px; } div.panel.show { opacity: 1; max-height: 500px; }
Comments
Post a Comment