Is there a way to make an animation when we see a div for a certain time?

Viewed 45

I'm trying to do something like this ... User clicks the button called "debug" and then the window scrolls to a div (#div2) with a "thank you" text inside, then (after 2 seconds with #div2 on the screen) I want the window to scroll to ANOTHER div (#div3) with the rest of the content. The problem is that I want to do everything with a smooth scroll, I tried it by myself for 2 days but with no results yet :(

Here's the code:

HTML:

<div id="div1">
        <h2>hi! my name is <b>Alan Aguilar</b></h2>
        <h3>and i'm a web develop...</h3>
        <p class="pIn">ummm...</p>
        <p class="pAp">sorry can u press this button for me, please?</p>
        <a href="#div2">debug</a>
    </div>

    <div id="div2">
        <h2>Th4nk5.</h2>
    </div>

    <div id="div3">
        <h2>Thanks.</h2>
    </div>

CSS:

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    scroll-behavior: smooth;
}

#div1 h2 {
    animation: opIn 2s backwards;
}

#div1 h3 {
    animation: opIn 1.2s 1.4s backwards;
}

#div1 {
    height: 650px;
    display: flex;
    justify-content: center;
    flex-direction: column;
    text-align: left;
    margin-left: 300px;
}

#div1 h2, h3 {
    font-weight: normal;
    font-size: 30px;
    margin-bottom: 10px;
}

#div1 h3 {
    font-size: 23px;
}

.pIn {
    font-size: 17px;
    margin-top: 30px;
    animation: opIn .5s 2.5s backwards;
}

.pAp {
    font-size: 17px;
    margin-top: 5px;
    animation: opIn .5s 3.8s backwards;
}

#div2 {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 700px;
    background: linear-gradient(to top, #000000 0%, #ffffff 100%);
}

#div1 a {
    width: 50px;
    padding: 5px;
    margin-top: 10px;
    position: relative;
    left: 245px;
    text-decoration: none;
    background-color: rgba(0, 0, 0, .1);
    border: 1px solid rgba(0, 0, 0, .5);
    border-radius: 2px;
    animation: opIn .3s 4.5s backwards;
    color: #000000;
}

#div1 a:hover {
    background-color: rgba(0, 0, 0, .15);
    box-shadow: inset 0px 0px 1px #000000;
}

#div2 h2 {
    font-size: 190px;
}

#div3 {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 700px;
    background-color: #000000;
    color: #ffffff;
}

@keyframes opIn {
    from {opacity: 0;}
    to {opacity: 1;}
}

Javascript:

window.onbeforeunload = function () {
  window.scrollTo(0, 0);
}

// --------------------------------------------------------------------------
// THIS CODE IS NOT MINE, I FOUND IT ON THIS PAGE LOOKING FOR THE SAME TOPIC.

$(document).ready(function(){
  // Add smooth scrolling to all links
  $("a").on('click', function(event) {

    // Make sure this.hash has a value before overriding default behavior
    if (this.hash !== "") {
      // Prevent default anchor click behavior
      event.preventDefault();

      // Store hash
      var hash = this.hash;

      // Using jQuery's animate() method to add smooth page scroll
      // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
      $('html, body').animate({
        scrollTop: $(hash).offset().top
      }, 800, function(){

        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = hash;
      });
    } // End if
  });
});

// --------------------------------------------------------------------------

$("a").click(function() {
  setTimeout(function() {window.location = "#div3"}, 1500, function() {
  })
})
1 Answers

Select the element with the id div3, get the offset top, and animate using jQuery's animate function:

window.onbeforeunload = function() {
  window.scrollTo(0, 0);
}

// --------------------------------------------------------------------------
// THIS CODE IS NOT MINE, I FOUND IT ON THIS PAGE LOOKING FOR THE SAME TOPIC.

$(document).ready(function() {
  // Add smooth scrolling to all links
  $("a").on('click', function(event) {

    // Make sure this.hash has a value before overriding default behavior
    if (this.hash !== "") {
      // Prevent default anchor click behavior
      event.preventDefault();

      // Store hash
      var hash = this.hash;

      // Using jQuery's animate() method to add smooth page scroll
      // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
      $('html, body').animate({
        scrollTop: $(hash).offset().top
      }, 800, function() {

        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = hash;
      });
    } // End if
  });
});

// --------------------------------------------------------------------------

$("a").click(function() {
  setTimeout(function() {
    $('html, body').animate({
      scrollTop: $("#div3").offset().top
    }, 800);
  }, 1500)
})
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  scroll-behavior: smooth;
}

#div1 h2 {
  animation: opIn 2s backwards;
}

#div1 h3 {
  animation: opIn 1.2s 1.4s backwards;
}

#div1 {
  height: 650px;
  display: flex;
  justify-content: center;
  flex-direction: column;
  text-align: left;
  margin-left: 300px;
}

#div1 h2,
h3 {
  font-weight: normal;
  font-size: 30px;
  margin-bottom: 10px;
}

#div1 h3 {
  font-size: 23px;
}

.pIn {
  font-size: 17px;
  margin-top: 30px;
  animation: opIn .5s 2.5s backwards;
}

.pAp {
  font-size: 17px;
  margin-top: 5px;
  animation: opIn .5s 3.8s backwards;
}

#div2 {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 700px;
  background: linear-gradient(to top, #000000 0%, #ffffff 100%);
}

#div1 a {
  width: 50px;
  padding: 5px;
  margin-top: 10px;
  position: relative;
  left: 245px;
  text-decoration: none;
  background-color: rgba(0, 0, 0, .1);
  border: 1px solid rgba(0, 0, 0, .5);
  border-radius: 2px;
  animation: opIn .3s 4.5s backwards;
  color: #000000;
}

#div1 a:hover {
  background-color: rgba(0, 0, 0, .15);
  box-shadow: inset 0px 0px 1px #000000;
}

#div2 h2 {
  font-size: 190px;
}

#div3 {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 700px;
  background-color: #000000;
  color: #ffffff;
}

@keyframes opIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="div1">
  <h2>hi! my name is <b>Alan Aguilar</b></h2>
  <h3>and i'm a web develop...</h3>
  <p class="pIn">ummm...</p>
  <p class="pAp">sorry can u press this button for me, please?</p>
  <a href="#div2">debug</a>
</div>

<div id="div2">
  <h2>Th4nk5.</h2>
</div>

<div id="div3">
  <h2>Thanks.</h2>
</div>

Related