sticky form

In this example below you will see how to do a sticky form with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by 007design, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright 007design ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>sticky form</title>
  
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <section class="hero">
  <div class="hero-container">
    <div class="form-container">
      <button data-close-form>Close</button>
    </div>
  </div>
</section>
<section class="main">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam volutpat dui eget quam commodo ultrices nec suscipit odio. Morbi purus purus, imperdiet ac fringilla eget, euismod at purus. Curabitur facilisis libero ut velit congue rutrum. In sit amet ultrices mi. Morbi semper porttitor neque sit amet vehicula. Ut aliquam diam dolor, blandit faucibus metus pellentesque sed. Donec eget commodo magna. Sed nec porta nulla, sed aliquam ipsum. Etiam vel arcu vitae sem aliquet cursus a eu sem. Integer pulvinar fringilla mollis. Nam ultricies porta lacus, vitae pellentesque lacus pretium tempus. Pellentesque in sodales mi, ut tristique ex. Ut facilisis nunc purus, sit amet pharetra quam vestibulum sed.</p>

<p>Nullam iaculis risus sit amet quam gravida lobortis. Nulla ex massa, pretium nec placerat at, volutpat eget turpis. Cras pharetra lobortis tellus eget congue. Cras eu felis nisl. Nunc suscipit mauris eget ipsum fermentum, nec aliquet massa mollis. Suspendisse nec lobortis nibh, eget sagittis augue. Sed molestie malesuada diam et sollicitudin. Mauris fermentum blandit nunc eget ultrices. Pellentesque neque massa, dictum at est non, scelerisque euismod neque. Nunc et sapien ut elit luctus facilisis nec et orci. In at pretium ipsum, id laoreet ex.</p>

<p>Suspendisse quis mollis nibh. Suspendisse aliquet scelerisque risus nec suscipit. Donec tellus nunc, iaculis in arcu nec, vehicula rutrum ipsum. Duis et sodales nunc, egestas dictum lectus. Duis nulla erat, interdum ut dui sed, condimentum elementum quam. Vivamus consectetur molestie euismod. Aenean a metus eu eros volutpat maximus. Vestibulum in interdum sapien. Morbi non dui imperdiet, varius est tincidunt, hendrerit justo.</p>

<p>Morbi et metus dapibus, convallis turpis facilisis, lobortis justo. Nulla efficitur ligula vitae luctus congue. In hendrerit ligula quis lorem imperdiet, et cursus purus consequat. Phasellus porta tellus sapien, eu tristique enim rhoncus eu. Mauris eleifend lacus nec consectetur varius. Sed fermentum ipsum odio, eget luctus sapien scelerisque non. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Quisque sit amet orci vel nulla tempor vestibulum et sit amet nibh.</p>

<p>Quisque turpis justo, cursus eget lacus quis, pretium sagittis orci. Aliquam porta, nulla id commodo pharetra, mi libero maximus est, sit amet ornare mi erat ut odio. Curabitur accumsan interdum ipsum, vel vehicula augue consequat in. Donec finibus rutrum mauris, eu porttitor augue bibendum quis. Aliquam erat volutpat. Proin ut mollis eros, eu condimentum ipsum. Phasellus mollis nulla in lorem mattis vehicula. Quisque sagittis ut dolor eu cursus. Aenean pellentesque dui nec orci rutrum, at egestas mauris mollis. Curabitur nec libero sit amet libero semper mattis in et tellus. Mauris sagittis mi sit amet ipsum rhoncus, eu volutpat mi lobortis. Duis fermentum semper neque, hendrerit euismod diam tristique sit amet. Aenean fermentum ante vitae condimentum venenatis. Etiam elit mauris, congue quis dictum sed, malesuada sit amet velit. Etiam tincidunt porttitor dui et pharetra.</p>
  <button data-show-form>Show Form</button>
</section>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/007design/sticky-form-Lmqwyj */
.hero {
  height: 300px;
  background-color: grey;
}
.hero .hero-container {
  position: relative;
  max-width: 800px;
  margin: 0 auto;
}

.form-container {
  position: absolute;
  height: 250px;
  width: 320px;
  right: 10px;
  top: 20px;
  background-color: red;
  transition: -webkit-transform linear .2s;
  transition: transform linear .2s;
  transition: transform linear .2s, -webkit-transform linear .2s;
}
.form-container button {
  display: none;
}
.form-container.sticky {
  transition: none;
}
.form-container.sticky.moving {
  transition: -webkit-transform linear .2s;
  transition: transform linear .2s;
  transition: transform linear .2s, -webkit-transform linear .2s;
}
.form-container.sticky button {
  display: block;
}


/*Downloaded from https://www.codeseek.co/007design/sticky-form-Lmqwyj */
var formContainer = document.querySelector('.form-container');
var formButton = document.querySelector('[data-show-form]');

document.querySelector('[data-close-form]').addEventListener('click', function () {
  formContainer.classList.add('moving');
  formContainer.classList.remove('sticky');
  formContainer.style.cssText = '';
  setTimeout(function () {
    return formContainer.classList.remove('moving');
  }, 200);
});

function translateForm() {
  formContainer.style.cssText = 'transform:translateY(' + window.scrollY + 'px)';
}

formButton.addEventListener('click', function () {
  formContainer.classList.add('moving');
  formContainer.classList.add('sticky');
  translateForm();
  setTimeout(function () {
    return formContainer.classList.remove('moving');
  }, 200);
});

window.addEventListener('scroll', function () {
  if (formContainer.classList.contains('sticky') && window.innerWidth > 768) translateForm();

  if (window.scrollY === 0) {
    formContainer.classList.remove('sticky');
    formContainer.style.cssText = '';
  }
});

Comments