button bouncing effect when scrolling vertically

Is there a way to create an element that bounces based on the user's scroll direction?

For instance, if the user scrolls down, the button should bounce from bottom to top, and if scrolling up, the button should bounce from top to bottom.

I have been considering using jQuery for this effect. Below is my current progress with the HTML and CSS styling:

var oldscrolltop = 0;
$(window).scroll(function(event){
   var scrolltop = $(this).scrollTop();
   if (scrolltop > oldscrolltop){
       $( ".bounce-scroll" ).removeClass( "bounce-down" ).addClass( "bounce" );
   } else {
      $( ".bounce-scroll" ).removeClass( "bounce" ).addClass( "bounce-down" );
   }
   oldscrolltop= scrolltop;
});
.wrapper {
  padding: 50px;
}

#register-btn-2 a {
    color: #fff;
    border: 1px solid #CC68F8;
    padding: 5px 7px;
    border-radius: 5px;
    background: #CC68F8;
    display: inline-block;
}

.animated {
  -webkit-animation-duration: .4s;
  animation-duration: .4s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  -webkit-animation-timing-function: linear;
  animation-timing-function: linear;
  animation-iteration-count: 5;
  -webkit-animation-iteration-count: 5;
}
@-webkit-keyframes bounce {
  0%, 100% {
    -webkit-transform: translateY(0);
  }
  50% {
    -webkit-transform: translateY(-5px);
  }
}
@keyframes bounce {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-5px);
  }
}
.bounce {
  -webkit-animation-name: bounce;
  animation-name: bounce;
}

@-webkit-keyframes bounce-down {
  0%, 100% {
    -webkit-transform: translateY(0);
  }
  50% {
    -webkit-transform: translateY(-5px);
  }
}
@keyframes bounce-down {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-5px);
  }
}
.bounce-down {
  -webkit-animation-name: bounce-down;
  animation-name: bounce-down;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  
  <p style="margin-bottom: 25px;">Lorem ipsum dolor sit amet, consectetur adipiscing elit.......
..........voluptate. Fusce at facilisis nisi ...........Ut ut quam quis ex bibendum rutrum in eu orci.</p>

  <div class="more-info-div">
    <p id="register-btn-2" class="bounce-scroll animated bounce" style="display: block; font-size: 16px; letter-spacing: 2px;"><a style="padding: 5px 40px;" target="_blank" rel="noopener">Coming Soon</a></p>
  </div>
  
   <p style="margin-bottom: 25px;">Lorem ipsum dolor sit amet, consectetur adipiscing elit......
.........voluptate. Fusce at facilisis nisi.......Ut ut quam quis ex bibendum rutrum in eu orci.</p>

</div>

Answer №1

To achieve this, compare the current scroll position with the previous scroll position using jQuery.

var prevScrollTop = 0;
$(window).scroll(function(event){
   var currentScrollTop = $(this).scrollTop();
   if (currentScrollTop > prevScrollTop){
       // code for downscroll behavior
   } else {
      // code for upscroll behavior
   }
   prevScrollTop = currentScrollTop;
});

Answer №2

var previousScrollTop = 0;
$(window).scroll(function(event){
    var scrollTop = $(this).scrollTop();
    if (scrollTop > previousScrollTop){
        var counter=0;
        while (counter<3) {
            $( ".bounce-scroll" ).removeClass( "bounce-down" ).addClass( "bounce" );
            counter++;
        }
    } else {
        var counter=0;
        while (counter<3) {
            $( ".bounce-scroll" ).removeClass( "bounce" ).addClass( "bounce-down" );
            counter++;
        }
    }
    previousScrollTop= scrollTop;
});
.wrapper {
  padding: 50px;
}

#register-btn-2 a {
    color: #fff;
    border: 1px solid #CC68F8;
    padding: 5px 7px;
    border-radius: 5px;
    background: #CC68F8;
    display: inline-block;
}

.animated {
  -webkit-animation-duration: .4s;
  animation-duration: .4s;
  -webkit-animation-fill-mode: both;
  animation-fill-mode: both;
  -webkit-animation-timing-function: linear;
  animation-timing-function: linear;
  animation-iteration-count: 5;
  -webkit-animation-iteration-count: 5;
}
@-webkit-keyframes bounce {
  0%, 100% {
    -webkit-transform: translateY(0);
  }
  50% {
    -webkit-transform: translateY(-5px);
  }
}
@keyframes bounce {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-5px);
  }
}
.bounce {
  -webkit-animation-name: bounce;
  animation-name: bounce;
}

@-webkit-keyframes bounce-down {
  0%, 100% {
    -webkit-transform: translateY(0);
  }
  50% {
    -webkit-transform: translateY(-5px);
  }
}
@keyframes bounce-down {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-5px);
  }
}
.bounce-down {
  -webkit-animation-name: bounce-down;
  animation-name: bounce-down;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  
  <p style="margin-bottom: 25px;">Unique Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus vitae risus velit. Aenean consectetur lorem at nibh congue, eget tincidunt metus eleifend. Vivamus accumsan at orci vitae congue. Nunc non congue lorem, non hendrerit elit. Donec ullamcorper lacus quis placerat congue. Pellentesque diam quam, pretium sodales felis vitae, lobortis lobortis lectus. In ipsum nulla, efficitur vitae volutpat iaculis, viverra et urna. Quisque mollis, erat vel iaculis malesuada, diam ex pulvinar eros, nec dictum metus ex vel sapien....

</p>

  <div class="more-info-div">
    <p id="register-btn-2" class="bounce-scroll animated bounce" style="display: block; font-size: 16px; letter-spacing: 2px;"><a style="padding: 5px 40px;" target="_blank" rel="noopener">Coming Soon</a></p>
  </div>
  
   <p style="margin-bottom: 25px;">Another segment of unique text goes here...</p>

</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

AngularJS ng-submit can be configured to trigger two separate actions

I am currently diving into AngularJS and working on a simple project to create an expense tracker as a beginner task. However, I have encountered some issues with my code. While I have successfully implemented most functions, I am struggling with the upda ...

Generating a unique event triggered by element class change

Is it possible to create custom JavaScript events that are triggered when elements receive a specific class? I am trying to monitor all elements within a table and perform certain actions once a particular class is added to them. Can this be done, and if ...

Combine multiple .less files into a single .less file without the need for recompilation

I currently have 3 different .less files (1file.less, 2file.less, 3file.less) that I want to combine into a single file called base.less. To import these files into base.less, I am using the @import "1file.less" method. However, upon inspecting the pa ...

I am looking to implement the ajax data variable for use in a submit button. How should

I'm facing an issue where, upon clicking the submit button, I am unable to receive the value yearend. Instead, I am getting an undefined value. How can I ensure that I receive the correct yearend value when I click the submit button? Here is my code: ...

What is the best method to organize HTML tables in Knockout by utilizing a Breeze navigation property?

Currently, I am utilizing Breeze.js to manage my data model within Knockout.js. This involves a simple view model with an adapter library for handling sorting tables on entity properties. The tables are sorted using tablesorter, along with a knockout bindi ...

Tips for vertically aligning text in the center with bootstrap

When creating a portfolio with bootstrap, I encountered an issue where I couldn't get the text to be vertically centered. Even after trying align-items-center and justify-content-center, I still couldn't achieve the desired result. Here is the sn ...

.comment {Visibility:hidden;} is only functioning on one specific page

What could be the reason behind this function only functioning on one page and not the others? The URL in question is . Specifically, the functionality is only active on the contact page while all other pages still display a comment section. ...

Solving layout issues / Techniques for determining element heights

This new question is in relation to my previous one: How to position relative elements after absolute elements I have updated the JsFiddle from that question to better represent my current HTML, which I unfortunately do not have a URL for at the moment. Y ...

jQuery not functioning properly for adjusting quantities with plus and minus buttons

Hey there, I've been trying to create a number input increment using jQuery but haven't had any luck so far. Could you please take a look at my code and provide some guidance? CHECK OUT THE DEMO HERE Here's the HTML: <div class="sp-qua ...

"Troubleshooting the issue of consistently receiving null values when uploading files with

I'm facing an issue with uploading a file using jQuery AJAX. Although I can see all the details of the file object such as its name and size, when I check the console with formdata.get("files"), the context.request.files size always shows zero. It see ...

Perform a jQuery computation using a CSS style

Having trouble with a calculation using a CSS property that is not returning any value. Seems like the issue might be due to the CSS property returning '200px' instead of just '200'. Any suggestions for a workaround? Thanks in advance. ...

Difficulty integrating Bootstrap with JavaScript file in Visual Studio Code

I am attempting to incorporate a dynamic progress bar by utilizing Bootstrap's progressbar and creating a custom JavaScript file. The goal is to continuously adjust the width of the progress bar at regular intervals to give the appearance of a moving ...

A guide on how to activate an event when a radio button is selected using jQuery

I experimented with implementing this. When I try clicking on the radio button, the code functions correctly, but the radio button does not appear to be checked and remains unchanged. Any ideas on how to resolve this issue? <p> <input id="p ...

Zooming out on mobile devices on a webpage using Bootstrap 4

After creating a web app and styling it with bootstrap 4, I incorporated a media query to adjust font size and padding on mobile devices. The code snippet used for this purpose is as follows: @media (max-width: 1200px) and (orientation: portrait) { .con ...

What could be preventing this code from automatically scrolling to the designated class when a key is pressed?

When a key is pressed on the document, the default action is prevented and the element with the ID "show-all-win" is animated to scroll to the position of the element with the class "key_" plus the key code of the pressed key. This animat ...

What is the proper syntax for using .focus() with the nextElementSibling method in typing?

As I strive to programmatically shift focus in my form using nextElementSibling, I encounter a challenge with typing variables/constants due to working with Typescript... I have managed to achieve success without typing by implementing the following: myF ...

Enhance form validation by utilizing jquery.validate to display appropriate icons indicating correct and incorrect input, with the option to

Trying to replicate the functionality of the jQuery validate plugin demo seen here, I am facing an issue on my own website. Upon clicking submit after the page loads, the plugin displays validation messages with a cross symbol. However, upon entering text, ...

Interactive navigation and delayed loading

After generating a set of 80 images in PHP using a for loop, I realized that the loading time is too slow. To address this issue, I attempted to incorporate lazy loading into my gallery by utilizing jQuery's simplePager and lazyLoad plugins. Despite m ...

Converting HTML to PDF: Transform your web content into

I have a couple of tasks that need to be completed: Firstly, I need to create a functionality where clicking a button will convert an HTML5 page into a .PDF file. Secondly, I am looking to generate another page with a table (Grid) containing data. The gr ...

Avoiding the use of dynamic dropdown with jquery in a spring MVC and hibernate setup

When clicking on an "add" button, I have implemented jQuery code to navigate to the next page. Below is the jQuery code snippet: $("#id-add-mer").click(function(){ location.href="../ecom/addMer"; }); And here is my controller method: @RequestMa ...