Tap to smoothly scroll through each block using the animate() function

ul {
          padding: 0;
          margin: 0;
          height: 180px;
          overflow: auto;
        }
        li {
          height: 50px;
          background: pink;
          list-style: none;
          margin-bottom: 10px;
          height: 50px;
        }
        body {
          padding: 0;
          margin: 0;
        }
<ul>
          <li>1</li>
          <li>2</li>
          <li>3</li>
          <li>4</li>
          <li>5</li>
          <li>6</li>
          <li>7</li>
          <li>8</li>
          <li>9</li>
          <li>10</li>
        </ul>
        <button>up</button>
        <button>down</button>

Is there a way to make the down button scroll block by block when clicked? I'm familiar with .animate() but I am unsure how to implement it for this specific functionality.

Answer №1

Here is a simple function I created to achieve this functionality without relying on any plugins. The code snippet using jQuery looks like this:

$('ul').animate({scrollTop:scrollValue});

You can see the working example on this fiddle.

Answer №2

According to the suggestion made in the previous response, please refer to the following code snippet:

$('#up').click(function(){
$('ul').animate({ "scrollTop": "-=100"},'fast');
}); 
$('#down').click(function(){
$('ul').animate({ "scrollTop": "+=100"},'fast');
}); 

In addition to using fast, you can also use:

$('ul').animate({ "scrollTop": "+=100"},0);

The value of 0 in this case indicates no delay.

If you require a delay, please utilize the following code:

var scroll=0;
$('#up').click(function(){
    if(scroll<=0){
        scroll=0;
    }
    else{
        scroll = scroll - 60;
        $('ul').animate({ "scrollTop": scroll});
    }
}); 
$('#down').click(function(){
   if(scroll>=$(document).height() )
   {
       scroll=$(document).height();
   }
    else{
          scroll = scroll + 60;
          $('ul').animate({ "scrollTop": scroll});
        }
}); 

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

When using node.js with MySQL, the insert statement with a WHERE clause does not execute properly

Snippet: function setValue(currentVal) { idOutput = currentVal; encodedVal = Base.encode(idOutput); var baseInsert = {ShortUrlCode: encodedVal}; console.log(idOutput); console.log(encodedVal); con.q ...

How to eliminate the added URL segment from the URL parameter when using $.ajax with jQuery

I am having a problem with using cakephp and implementing the jQuery.ajax method to send data to my server. The issue I am facing is that jQuery's $.ajax automatically adds the protocol, hostname, and current controller to the URL, making it impossibl ...

Backdrop styling for Material-UI dialogs/modals

Is there a way to customize the semi-transparent overlay of a material-ui dialog or modal? I am currently using material-ui with React and Typescript. https://i.stack.imgur.com/ODQvN.png Instead of a dark transparent overlay, I would like it to be transp ...

React Hooks findByID method is throwing an error stating that it cannot read the property 'params' because it is undefined

As I dive into learning React hooks, I'm realizing that it's crucial to stay up-to-date with the latest trends in web development. Despite hours of research and tinkering around, I can't seem to wrap my head around some key concepts. The fac ...

Having trouble with the sleep function in nodejs?

Hey there! I'm currently working on a Node.js function that sends requests using array.map. However, I'm facing a challenge with making the function wait for 4 minutes when an error occurs before sending the next request. I've attempted to u ...

Clicking on an embedded YouTube video will automatically redirect you to the video's

Hey there, I've added an embedded video to my website and I'm wondering if it's possible to redirect users to a new site when they click the play button? Thanks in advance! ...

Encountering issues while attempting to transmit several files to backend in React/NestJS resulting in a BAD REQUEST error

My goal is to allow users to upload both their CV and image at the same time as a feature. However, every time I attempt to send both files simultaneously to the backend, I encounter a Bad Request error 400. I have made various attempts to troubleshoot th ...

How can we prevent floating li elements from overlapping the parent div element?

What could be causing the yellow stripe (#header) to disappear when I apply "float left;" to "#header ul li"? Despite setting a fixed size for "#header ul li", I anticipated that the list items would line up next to each other horizontally, not exceeding t ...

Incorporating a hamburger symbol into an established menu for easy navigation

I have come across a tutorial on creating a navigation menu with a logo positioned to the left. However, I now wish to incorporate a hamburger icon for mobile devices and I am unsure about the process. Despite my efforts to find a suitable tutorial onlin ...

What is causing my background div to jerk around when I implement jQuery animate to adjust its height?

UPDATE: I have replicated my issue in jsFiddle: http://jsfiddle.net/leongaban/3v8vW/3/ I am dealing with 2 tabs - one should reduce the height of the form-background when clicked, while the other should increase it. I want a smooth animation without any a ...

tracking scroll position within div on main page

I have a div tag enclosed within a content tag due to the implementation of a masterpage containing the forms and body tags. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"> <div id="xxx" style="overflow:s ...

Using Perl to pass query information with ajax/json and including scalar reference for DBIx::Class

I am currently facing an issue while trying to pass a query from jquery/ajax/json to a perl script that utilizes DBIx::Class and returns the results. While I have no trouble getting a basic query to function, I encounter difficulties when needing to includ ...

The filter() method in Jquery does not function properly when used with a class

Seeking guidance as a beginner web developer. Encountering an issue with the filter() function. When using it with an ID selector like ('#test'), everything works smoothly. However, when attempting to apply it to a class selector like ('.loc ...

Ways to display JSON data in Angular 2

My goal is to display a list of JSON data, but I keep encountering an error message ERROR TypeError: Cannot read property 'title' of undefined. Interestingly, the console log shows that the JSON data is being printed. mydata.service.ts import { ...

Retrieving JSON information using JavaScript

I am encountering an issue with the "Ion.RangeSlider" library. I am attempting to dynamically load values via JSON, but I am unable to get the slider to accept the data from the "from" field. It is important that the value is not hardcoded since the user h ...

Is there a way to verify the identity of two fields using an external script such as "signup.js"?

My current project involves working with Electron, and it consists of three essential files. The first file is index.html: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="styles ...

Express Concurrency: Managing Multiple Tasks Simultaneously

Looking to create an API using Express that is capable of processing requests either through multithreading or multiprocessing. For example, the following API has a 5-second sleep before responding. If I make 3 quick calls to it, the first response will ta ...

Having trouble with ng-click in my AngularJS Restful application

I was attempting to create CRUD operations in AngularJS. Unfortunately, I am facing an issue where my ng-click is not functioning properly. Below is the code for my list.html: <div class="container"> <input type="text" ng-controlle ...

Learn how to dynamically pass a value from a prop to a router-link in Vue.js

I created a custom button component and decided to switch from using <a> tags to <router-link>. However, I encountered an error because the router-link was rendering before the prop received its value. To address this, I added an if statement b ...

I have exhausted all possible solutions in my attempts to eliminate the whitespace below my footer. Is there something I have overlooked or not tried yet?

After updating the CSS stylesheet, I noticed a white space below my footer. A screenshot has been included for reference. Despite including a CSS reset, the issue still persists. /* html5doctor.com Reset Stylesheet v1.6.1 Last Updated: 2010-09-17 Author ...