Do you want to create multi step from on your website, to gather user information like registration form etc then here i have share bootstrap, css and javascript code for creating multi step form with awesome animation you can easily switch to from clicking on steps between you want. this is easy to fill form for your website visitors.
HTML
--PEN HEADER--> |
Pick animation type
Click on steps or ‘Prev’ and ‘Next’ buttons
CSS
* { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: 'Poppins', sans-serif; font-size: 16px; color: #2c2c2c; } body a { color: inherit; text-decoration: none; } .header__btn { -webkit-transition-property: all; transition-property: all; -webkit-transition-duration: 0.2s; transition-duration: 0.2s; -webkit-transition-timing-function: linear; transition-timing-function: linear; -webkit-transition-delay: 0s; transition-delay: 0s; padding: 10px 20px; display: inline-block; margin-right: 10px; background-color: #fff; border: 1px solid #2c2c2c; border-radius: 3px; cursor: pointer; outline: none; } .header__btn:last-child { margin-right: 0; } .header__btn:hover, .header__btn.js-active { color: #fff; background-color: #2c2c2c; } .header { max-width: 600px; margin: 50px auto; text-align: center; } .header__title { margin-bottom: 30px; font-size: 2.1rem; } .content { width: 95%; margin: 0 auto 50px; } .content__title { margin-bottom: 40px; font-size: 20px; text-align: center; } .content__title--m-sm { margin-bottom: 10px; } .multisteps-form__progress { display: grid; grid-template-columns: repeat(auto-fit, minmax(0, 1fr)); } .multisteps-form__progress-btn { -webkit-transition-property: all; transition-property: all; -webkit-transition-duration: 0.15s; transition-duration: 0.15s; -webkit-transition-timing-function: linear; transition-timing-function: linear; -webkit-transition-delay: 0s; transition-delay: 0s; position: relative; padding-top: 20px; color: rgba(108, 117, 125, 0.7); text-indent: -9999px; border: none; background-color: transparent; outline: none !important; cursor: pointer; } @media (min-width: 500px) { .multisteps-form__progress-btn { text-indent: 0; } } .multisteps-form__progress-btn:before { position: absolute; top: 0; left: 50%; display: block; width: 13px; height: 13px; content: ''; -webkit-transform: translateX(-50%); transform: translateX(-50%); -webkit-transition: all 0.15s linear 0s, -webkit-transform 0.15s cubic-bezier(0.05, 1.09, 0.16, 1.4) 0s; transition: all 0.15s linear 0s, -webkit-transform 0.15s cubic-bezier(0.05, 1.09, 0.16, 1.4) 0s; transition: all 0.15s linear 0s, transform 0.15s cubic-bezier(0.05, 1.09, 0.16, 1.4) 0s; transition: all 0.15s linear 0s, transform 0.15s cubic-bezier(0.05, 1.09, 0.16, 1.4) 0s, -webkit-transform 0.15s cubic-bezier(0.05, 1.09, 0.16, 1.4) 0s; border: 2px solid currentColor; border-radius: 50%; background-color: #fff; box-sizing: border-box; z-index: 3; } .multisteps-form__progress-btn:after { position: absolute; top: 5px; left: calc(-50% - 13px / 2); -webkit-transition-property: all; transition-property: all; -webkit-transition-duration: 0.15s; transition-duration: 0.15s; -webkit-transition-timing-function: linear; transition-timing-function: linear; -webkit-transition-delay: 0s; transition-delay: 0s; display: block; width: 100%; height: 2px; content: ''; background-color: currentColor; z-index: 1; } .multisteps-form__progress-btn:first-child:after { display: none; } .multisteps-form__progress-btn.js-active { color: #007bff; } .multisteps-form__progress-btn.js-active:before { -webkit-transform: translateX(-50%) scale(1.2); transform: translateX(-50%) scale(1.2); background-color: currentColor; } .multisteps-form__form { position: relative; } .multisteps-form__panel { position: absolute; top: 0; left: 0; width: 100%; height: 0; opacity: 0; visibility: hidden; } .multisteps-form__panel.js-active { height: auto; opacity: 1; visibility: visible; } .multisteps-form__panel[data-animation="scaleOut"] { -webkit-transform: scale(1.1); transform: scale(1.1); } .multisteps-form__panel[data-animation="scaleOut"].js-active { -webkit-transition-property: all; transition-property: all; -webkit-transition-duration: 0.2s; transition-duration: 0.2s; -webkit-transition-timing-function: linear; transition-timing-function: linear; -webkit-transition-delay: 0s; transition-delay: 0s; -webkit-transform: scale(1); transform: scale(1); } .multisteps-form__panel[data-animation="slideHorz"] { left: 50px; } .multisteps-form__panel[data-animation="slideHorz"].js-active { -webkit-transition-property: all; transition-property: all; -webkit-transition-duration: 0.25s; transition-duration: 0.25s; -webkit-transition-timing-function: cubic-bezier(0.2, 1.13, 0.38, 1.43); transition-timing-function: cubic-bezier(0.2, 1.13, 0.38, 1.43); -webkit-transition-delay: 0s; transition-delay: 0s; left: 0; } .multisteps-form__panel[data-animation="slideVert"] { top: 30px; } .multisteps-form__panel[data-animation="slideVert"].js-active { -webkit-transition-property: all; transition-property: all; -webkit-transition-duration: 0.2s; transition-duration: 0.2s; -webkit-transition-timing-function: linear; transition-timing-function: linear; -webkit-transition-delay: 0s; transition-delay: 0s; top: 0; } .multisteps-form__panel[data-animation="fadeIn"].js-active { -webkit-transition-property: all; transition-property: all; -webkit-transition-duration: 0.3s; transition-duration: 0.3s; -webkit-transition-timing-function: linear; transition-timing-function: linear; -webkit-transition-delay: 0s; transition-delay: 0s; } .multisteps-form__panel[data-animation="scaleIn"] { -webkit-transform: scale(0.9); transform: scale(0.9); } .multisteps-form__panel[data-animation="scaleIn"].js-active { -webkit-transition-property: all; transition-property: all; -webkit-transition-duration: 0.2s; transition-duration: 0.2s; -webkit-transition-timing-function: linear; transition-timing-function: linear; -webkit-transition-delay: 0s; transition-delay: 0s; -webkit-transform: scale(1); transform: scale(1); } |
JS
//DOM elements const DOMstrings = { stepsBtnClass: 'multisteps-form__progress-btn', stepsBtns: document.querySelectorAll(`.multisteps-form__progress-btn`), stepsBar: document.querySelector('.multisteps-form__progress'), stepsForm: document.querySelector('.multisteps-form__form'), stepsFormTextareas: document.querySelectorAll('.multisteps-form__textarea'), stepFormPanelClass: 'multisteps-form__panel', stepFormPanels: document.querySelectorAll('.multisteps-form__panel'), stepPrevBtnClass: 'js-btn-prev', stepNextBtnClass: 'js-btn-next' }; //remove class from a set of items const removeClasses = (elemSet, className) => { elemSet.forEach(elem => { elem.classList.remove(className); }); }; //return exect parent node of the element const findParent = (elem, parentClass) => { let currentNode = elem; while (!currentNode.classList.contains(parentClass)) {if (window.CP.shouldStopExecution(0)) break; currentNode = currentNode.parentNode; }window.CP.exitedLoop(0); return currentNode; }; //get active button step number const getActiveStep = elem => { return Array.from(DOMstrings.stepsBtns).indexOf(elem); }; //set all steps before clicked (and clicked too) to active const setActiveStep = activeStepNum => { //remove active state from all the state removeClasses(DOMstrings.stepsBtns, 'js-active'); //set picked items to active DOMstrings.stepsBtns.forEach((elem, index) => { if (index <= activeStepNum) { elem.classList.add('js-active'); } }); }; //get active panel const getActivePanel = () => { let activePanel; DOMstrings.stepFormPanels.forEach(elem => { if (elem.classList.contains('js-active')) { activePanel = elem; } }); return activePanel; }; //open active panel (and close unactive panels) const setActivePanel = activePanelNum => { //remove active class from all the panels removeClasses(DOMstrings.stepFormPanels, 'js-active'); //show active panel DOMstrings.stepFormPanels.forEach((elem, index) => { if (index === activePanelNum) { elem.classList.add('js-active'); setFormHeight(elem); } }); }; //set form height equal to current panel height const formHeight = activePanel => { const activePanelHeight = activePanel.offsetHeight; DOMstrings.stepsForm.style.height = `${activePanelHeight}px`; }; const setFormHeight = () => { const activePanel = getActivePanel(); formHeight(activePanel); }; //STEPS BAR CLICK FUNCTION DOMstrings.stepsBar.addEventListener('click', e => { //check if click target is a step button const eventTarget = e.target; if (!eventTarget.classList.contains(`${DOMstrings.stepsBtnClass}`)) { return; } //get active button step number const activeStep = getActiveStep(eventTarget); //set all steps before clicked (and clicked too) to active setActiveStep(activeStep); //open active panel setActivePanel(activeStep); }); //PREV/NEXT BTNS CLICK DOMstrings.stepsForm.addEventListener('click', e => { const eventTarget = e.target; //check if we clicked on `PREV` or NEXT` buttons if (!(eventTarget.classList.contains(`${DOMstrings.stepPrevBtnClass}`) || eventTarget.classList.contains(`${DOMstrings.stepNextBtnClass}`))) { return; } //find active panel const activePanel = findParent(eventTarget, `${DOMstrings.stepFormPanelClass}`); let activePanelNum = Array.from(DOMstrings.stepFormPanels).indexOf(activePanel); //set active step and active panel onclick if (eventTarget.classList.contains(`${DOMstrings.stepPrevBtnClass}`)) { activePanelNum--; } else { activePanelNum++; } setActiveStep(activePanelNum); setActivePanel(activePanelNum); }); //SETTING PROPER FORM HEIGHT ONLOAD window.addEventListener('load', setFormHeight, false); //SETTING PROPER FORM HEIGHT ONRESIZE window.addEventListener('resize', setFormHeight, false); //changing animation via animation select !!!YOU DON'T NEED THIS CODE (if you want to change animation type, just change form panels data-attr) const setAnimationType = newType => { DOMstrings.stepFormPanels.forEach(elem => { elem.dataset.animation = newType; }); }; //selector onchange - changing animation const animationSelect = document.querySelector('.pick-animation__select'); animationSelect.addEventListener('change', () => { const newAnimationType = animationSelect.value; setAnimationType(newAnimationType); }); |
See live demo and download source code.
|
Don’t forget to Subscribe My Public Notebook for more useful free scripts, tutorials and articles.
This awesome script developed by nat-davydova. Visit their official repository for more information and follow for future updates.