How we can create a select option input with folding dropdowns? Solution: See this Folding Select Dropdown Using JavaScript and CSS, Dropdown Options.
Previously I have shared some Option Select programs, but it has a folding dropdown feature. Basically, the <select>Â element is used to create a drop-down list. And the <option>Â tags inside the <select> element define the available options in the dropdown. By default, this tag is non-stylish but we can style or customize it.
Today you will learn to create unique and stylish dropdown options for select. Basically, there ate 2 select inputs with their options list. The main factor of this select dropdown is the folding option list, when you will click on the input then the options will reveal with folding effect. And there is a tick mark also, when you will select an option then the tick mark will visible on the right of the option and the list will hide again with fold effect.
So, Today I am sharing Folding Select Dropdown Using JavaScript and CSS. There I have used HTML tags to create the layout, CSS for styling and JavaScript for functioning. This is a unique and complete select option program and ready to use. You can use this program on your website to attract your users.
If you are thinking now how this folding dropdown program actually is, then see the preview given below.
Preview Of Dropdown Options
See this video preview to getting an idea of how this program looks like.
Now you can see this program visually, also you can see it live by pressing the button given above. If you like this, then get the source code of its.
You May Also Like:
Folding Select Dropdown Using JavaScript and CSS Source Code
Before sharing source code, let’s talk about it. First I have created a form tag and placed all elements inside it. Inside the form tag, I have placed 2 select tags with labels and options tag. In option tags, I have placed values and texts also. Also in the HTML file, I have linked external files like CSS and JavaScript.
Now using CSS I have placed all the elements in the right places, as you can see in the preview. With CSS I gave basic values like size, position, margin, padding, etc to all the elements. There I have used CSS variables to store and pass custom data. For creating the folding animation, I have used CSS @keyframe command.
JavaScript handling here all the functions in the program. There I have used JS if{} else{} statements to create some functions. Also, there I have used switch, case, break commands. There the JS part is a little complicated and hard to explain. You will understand easily after getting the codes, I can’t explain in writing.
For creating this program you have to create 3 files. First file for HTML, second for CSS, and third file for JavaScript. Follow the steps to creating this program without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
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 |
<!DOCTYPE html> <!--Code By Webdevtrick ( https://webdevtrick.com )--> <html lang="en" > <head> <meta charset="UTF-8"> <title>Folding Select Dropdowns | Webdevtrick.com</title> <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"> <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Gaegu:400,700&display=swap'> <link rel="stylesheet" href="style.css"> </head> <body> <form> <label for="frontend">Fronted</label> <select id="frontend" name="Frontend"> <option value="none" selected>Select…</option> <option value="HTML">HTML</option> <option value="CSS">CSS</option> <option value="JavaScript">JavaScript</option> </select> <label for="backend">Backend</label> <select id="backend" name="backend"> <option value="none" selected>Select…</option> <option value="PHP">PHP</option> <option value="MySQL">MySQL</option> <option value="Node JS">Node JS</option> <option value="Python">Python</option> </select> </form> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
/* Code By Webdevtrick ( https://webdevtrick.com ) */ * { border: 0; box-sizing: border-box; margin: 0; padding: 0; } :root { --foldDur: 0.7s; font-size: calc(24px + (30 - 24)*(100vw - 320px)/(1280 - 320)); } body, button { color: #171717; font: 1em Gaegu, cursive; line-height: 1.5; } body { background-color: #fff; } form, .select__button, .select__options { width: 100%; } form { margin: auto; padding: 3em 1.5em 6em 1.5em; max-width: 13.25em; } label { display: block; font-weight: bold; } .select, .select__button { position: relative; } .select { margin-bottom: 1.5em; perspective: 20em; } .select select { display: none; } .select__button, .select__option { background: #f1f1f1; cursor: pointer; display: flex; justify-content: space-between; align-items: center; padding: 0 0.375em; } .select__button { animation-duration: var(--foldDur); animation-timing-function: ease-in-out; animation-direction: reverse; border-radius: 0; box-shadow: -0.05em 0 0 #fff inset, 0.05em 0 0 #ccc inset, 0 -0.05em 0 #d9d9d9 inset; transform-origin: 50% 100%; -webkit-appearance: none; appearance: none; z-index: 1; } .select__button:focus, .select__option:focus { outline: transparent; } .select__button:focus { color: #ff3f3f; } .select__button:after, .select__option:first-child:after { border-left: 0.25em solid transparent; border-right: 0.25em solid transparent; content: ""; display: block; width: 0; height: 0; } .select__button:after { border-top: 0.25em solid; } .select__button--pristine { animation: none; } .select__options { position: absolute; top: 0; left: 0; visibility: hidden; transition: visibility 0s var(--foldDur) steps(1); } .select__option { box-shadow: -0.05em -0.1em 0.2em 0 #0001 inset, 0.05em 0 0 #ccc inset, -0.05em 0.1em 0.2em 0 #fff inset, -0.05em 0 0 #fff inset; color: inherit; opacity: 0; text-decoration: none; transition: opacity 0s var(--foldDur) steps(1); } .select__option:first-child { box-shadow: -0.05em -0.1em 0.2em 0 #0001 inset, 0.05em 0 0 #ccc inset, -0.05em 0 0 #fff inset; } .select__option:first-child:after { border-bottom: 0.25em solid; } .select__option:hover, .select__option:focus { background: #ff7c7c; box-shadow: -0.05em -0.1em 0.2em 0 #0001 inset, 0.05em 0 0 #a9bbe5 inset, -0.05em 0 0 #c1d4ff inset; } .select__option[aria-selected="true"]:not(:first-child):after { content: "\2713"; } /* Animation */ .select__button[aria-expanded="true"] { animation-direction: normal; animation-fill-mode: forwards; box-shadow: -0.05em -0.1em 0.2em 0 #fff inset, -0.05em 0 0 #fff inset; cursor: default; pointer-events: none; } .select__button--fold4 { animation-name: fold4; } .select__button--fold5 { animation-name: fold5; } .select__button[aria-expanded="true"] + .select__options, .select__button[aria-expanded="true"] + .select__options .select__option { transition-delay: 0s; } .select__button--fold4[aria-expanded="true"] + .select__options, .select__button--fold5[aria-expanded="true"] + .select__options { visibility: visible; } .select__button--fold4[aria-expanded="true"] + .select__options .select__option, .select__button--fold5[aria-expanded="true"] + .select__options .select__option { opacity: 1; } .select__button--fold4 + .select__options .select__option:nth-child(4), .select__button--fold4[aria-expanded="true"] + .select__options .select__option:nth-child(2) { transition-delay: calc(var(--foldDur) * 0.25); } .select__button--fold4 + .select__options .select__option:nth-child(3), .select__button--fold4[aria-expanded="true"] + .select__options .select__option:nth-child(3) { transition-delay: calc(var(--foldDur) * 0.5); } .select__button--fold4 + .select__options .select__option:nth-child(2), .select__button--fold4[aria-expanded="true"] + .select__options .select__option:nth-child(4) { transition-delay: calc(var(--foldDur) * 0.75); } .select__button--fold5 + .select__options .select__option:nth-child(5), .select__button--fold5[aria-expanded="true"] + .select__options .select__option:nth-child(2) { transition-delay: calc(var(--foldDur) * 0.2); } .select__button--fold5 + .select__options .select__option:nth-child(4), .select__button--fold5[aria-expanded="true"] + .select__options .select__option:nth-child(3) { transition-delay: calc(var(--foldDur) * 0.4); } .select__button--fold5 + .select__options .select__option:nth-child(3), .select__button--fold5[aria-expanded="true"] + .select__options .select__option:nth-child(4) { transition-delay: calc(var(--foldDur) * 0.6); } .select__button--fold5 + .select__options .select__option:nth-child(2), .select__button--fold5[aria-expanded="true"] + .select__options .select__option:nth-child(5) { transition-delay: calc(var(--foldDur) * 0.8); } @keyframes appear { from { opacity: 0 } to { opacity: 1 } } @keyframes fold4 { from { color: #171717; transform: translateY(0) rotateX(0deg) } 12.5% { color: #171717; transform: translateY(0) rotateX(-90deg) } 12.51% { color: transparent; transform: translateY(0) rotateX(-90deg) } 25% { color: transparent; transform: translateY(0) rotateX(-180deg) } 25.01% { color: transparent; transform: translateY(100%) rotateX(0deg) } 50% { color: transparent; transform: translateY(100%) rotateX(-180deg) } 50.01% { color: transparent; transform: translateY(200%) rotateX(0deg) } 75% { color: transparent; transform: translateY(200%) rotateX(-180deg) } 75.01% { color: transparent; transform: translateY(300%) rotateX(0deg) } to { color: transparent; transform: translateY(300%) rotateX(-100deg) } } @keyframes fold5 { from { color: #171717; transform: translateY(0) rotateX(0deg) } 10% { color: #171717; transform: translateY(0) rotateX(-90deg) } 10.01% { color: transparent; transform: translateY(0) rotateX(-90deg) } 20% { color: transparent; transform: translateY(0) rotateX(-180deg) } 20.01% { color: transparent; transform: translateY(100%) rotateX(0deg) } 40% { color: transparent; transform: translateY(100%) rotateX(-180deg) } 40.01% { color: transparent; transform: translateY(200%) rotateX(0deg) } 60% { color: transparent; transform: translateY(200%) rotateX(-180deg) } 60.01% { color: transparent; transform: translateY(300%) rotateX(0deg) } 80% { color: transparent; transform: translateY(300%) rotateX(-180deg) } 80.01% { color: transparent; transform: translateY(400%) rotateX(0deg) } to { color: transparent; transform: translateY(400%) rotateX(-100deg) } } |
function.js
Now create a JavaScript file named ‘function.js‘ and put the codes.
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
// Code By Webdevtrick ( https://webdevtrick.com ) document.addEventListener("DOMContentLoaded",() => { const os = new SelectDropdown({ id: "frontend" }), fruit = new SelectDropdown({ id: "backend" }); }); class SelectDropdown { constructor(args) { this.isOpening = false; this.select = document.querySelector(`select[id=${args.id}]`); this.selectBtn = document.createElement("button"); this.options = document.createElement("div"); this.buildDropdown(); } buildDropdown() { if (this.select !== null) { // create div to contain <select> let wrapper = document.createElement("div"); wrapper.setAttribute("class","select"); this.select.parentElement.insertBefore(wrapper,this.select); wrapper.appendChild(this.select); // create button let id = this.select.id, selectBtnAttrs = { "class": "select__button select__button--pristine", "type": "button", "id": `${id}-options`, "aria-haspopup": "listbox", "aria-expanded": "false" }; for (let a in selectBtnAttrs) this.selectBtn.setAttribute(a,selectBtnAttrs[a]); let selectBtnText = document.createTextNode(this.select.options[0].innerHTML); this.selectBtn.appendChild(selectBtnText); wrapper.appendChild(this.selectBtn); // create options div let optionsAttrs = { "class": "select__options", "aria-labelledby": selectBtnAttrs.id }; for (let a in optionsAttrs) this.options.setAttribute(a,optionsAttrs[a]); // then each option for (let o of this.select.options) { let option = document.createElement("a"), optionText = document.createTextNode(o.innerHTML), optionAttrs = { "href": "#", "class": "select__option", "data-value": o.value, "role": "option", "aria-selected": "false" }; for (let a in optionAttrs) option.setAttribute(a,optionAttrs[a]); option.appendChild(optionText); this.options.appendChild(option); } wrapper.appendChild(this.options); // sync with pre-selected option let preselected = this.options.querySelector(`[data-value=${this.select.value}]`); preselected.setAttribute("aria-selected",true); this.selectBtn.innerHTML = preselected.innerHTML; // restack so options can appear over other dropdowns let selects = document.querySelectorAll(".select"), selectCount = 0; while (selectCount < selects.length) { selects[selectCount].style.zIndex = selects.length - selectCount; ++selectCount; } // assign event listeners document.querySelector(`label[for=${id}]`).addEventListener("click",() => { if (!this.isExpanded()) this.selectBtn.focus(); else this.closeSelect(); }); this.selectBtn.addEventListener("click",() => { this.openSelect(); }); this.options.addEventListener("click",e => { this.closeSelect(e); }); document.addEventListener("click",() => { let el = document.activeElement; if (el.parentElement.getAttribute("aria-labelledby") !== selectBtnAttrs.id) this.closeSelect(); }); window.addEventListener("keydown",e => { switch (e.keyCode) { case 27: // Esc this.closeSelect(); break; case 32: // Spacebar this.closeSelect(e); break; case 38: // Up this.goToOption("previous"); break; case 40: // Down this.goToOption("next"); break; default: break; } }); } } isExpanded() { return this.selectBtn.getAttribute("aria-expanded") === "true"; } openSelect(e) { if (!this.isExpanded()) { // prevent immediate closing to not ruin animation let foldDur = window.getComputedStyle(this.options); foldDur = foldDur.getPropertyValue("transition-delay").split(""); if (foldDur.indexOf("m") > -1) { foldDur.splice(foldDur.length - 2,2); foldDur = parseInt(foldDur.join("")); } else if (foldDur.indexOf("s") > -1) { foldDur.pop(); foldDur = parseFloat(foldDur.join("")) * 1e3; } this.isOpening = true; setTimeout(() => {this.isOpening = false;},foldDur); // manage states this.selectBtn.setAttribute("aria-expanded",true); let btnClasses = this.selectBtn.classList, pristineClass = "select__button--pristine", animClass = `select__button--fold${this.select.options.length}`; if (btnClasses.contains(pristineClass)) { btnClasses.remove(pristineClass); } else { btnClasses.remove(animClass); void this.selectBtn.offsetWidth; } btnClasses.add(animClass); // set focus to selected option let selected = this.options.querySelector("[aria-selected=true]"); if (selected !== null) selected.focus(); else this.options.childNodes[0].focus(); } } closeSelect(e) { if (this.isExpanded() && !this.isOpening) { if (e && e.target !== this.options.childNodes[0]) { // update values of both original and custom dropdowns this.select.value = e.target.getAttribute("data-value"); this.selectBtn.innerHTML = e.target.innerHTML; // indicate selected item for (let n of this.options.childNodes) n.setAttribute("aria-selected",false); e.target.setAttribute("aria-selected",true); e.preventDefault(); } this.selectBtn.setAttribute("aria-expanded",false); this.selectBtn.focus(); // fire animation let btnClasses = this.selectBtn.classList, animClass = `select__button--fold${this.select.options.length}`; btnClasses.remove(animClass); void this.selectBtn.offsetWidth; btnClasses.add(animClass); } } goToOption(goTo) { if (this.isExpanded()) { let optionLinks = this.options.querySelectorAll("a"), activeEl = document.activeElement, linkFound = false; // check for focused option for (let l of optionLinks) { if (activeEl === l) { linkFound = true; break; } } // allow movement with arrows until top or bottom option is reached if (linkFound) { if (goTo === "previous" && activeEl !== optionLinks[0]) activeEl.previousSibling.focus(); else if (goTo === "next" && activeEl !== optionLinks[optionLinks.length - 1]) activeEl.nextSibling.focus(); } } } } |
That’s It. Now you have successfully created Folding Select Dropdown Using JavaScript and CSS, Dropdown Options List. If you have any doubt or questions comment down below.
Thanks For Visiting, Keep Visiting.