jquery scroll animation code example

Example 1: smooth scroll

// Scroll to specific values
// scrollTo is the same
window.scroll({
  top: 2500, 
  left: 0, 
  behavior: 'smooth'
});

// Scroll certain amounts from current position 
window.scrollBy({ 
  top: 100, // could be negative value
  left: 0, 
  behavior: 'smooth' 
});

// Scroll to a certain element
document.querySelector('.hello').scrollIntoView({ 
  behavior: 'smooth' 
});

Example 2: jquery scroll when object appear on screen make animation

$(window).on("scroll", function(){
  if($(window).scrollTop() + $(window).height() - 100 >= $(".target").offset().top){
    alert("On viewport");
  }
})

Example 3: jquery scroll when object appear on screen make animation

body{
  padding-top: 150vh;
}

.target{
  background: red;
  width: 100%;
  height: 50px;
  margin-bottom: 500px;
}

Tags:

Misc Example