How can I animate scrolling up or down using jQuery and CSS?

I have a a element that triggers an action when clicked:

<a href="#" class="hel" onclick="YPCP1_();">Scroll down</a>

Also,

function YPCP_(){
    var scroll = $(window).scrollTop();
    window.scrollTo(0, 600);
}

The function successfully scrolls down to position 600. However, it currently jumps directly to position 600, but I would like it to slide down instead. For example,

slide down and reach position 600 in 6 seconds
.

I hope this explanation is clear. Thank you in advance! ( '_' )

Answer №1

If you're seeking the jQuery method for smooth animations (http://api.jquery.com/animate/), look no further.

To specify the final scrollTop position and duration of the animation, try something like this:

$('.content').animate({
    scrollTop: "+=" + 600
  }, 6000);

A demonstration of this concept can be found in this fiddle: https://jsfiddle.net/L6uuve70/10/

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

The issue of slides overlapping in a Javascript slideshow during auto play mode

I encountered an issue while creating an automatic slideshow with 4 slides. Upon autoplay for the first time, slide 1 overlaps slide 4, as well as slide 2 and 3. This only happens once after the site loads. However, on subsequent playthroughs or when using ...

Error encounter in JSP is nested within another JSP file, both of which utilize jQuery

Before proceeding, it is important to note that I am in the process of selecting a month using a datepicker and a specific meter (by its serial number). Once this information is selected, a query will be sent to a MySQL database to retrieve data for plotti ...

The toggle button for columns is not triggering the callback action

When working with the following JSFiddle, I noticed that the action function does not seem to fire whenever a button to select a column in the column visibility tool is selected. Check out the code snippet below for reference: $(document).ready(function( ...

variations in menu functionality on firefox

Could you please take a look at the links below? It appears that there may be an issue with FireFox. The green top menu seems to be displaying incorrectly. In Chrome/IE, it appears in one line, but in FF, the last two links are on the second line despite s ...

Checkmate Validation

I've been trying to utilize the functionality provided by My goal is to use ajax to submit the form only if there are no errors present. No matter what I attempt, I can't seem to get it to work correctly. Looking for error validation $(docume ...

Drop-down selection triggering horizontal auto-scroll

Within my div element, I have a table with dropdowns. The div is set up to be horizontally scrollable. To create the dropdown functionality, I utilized Harvesthq Chosen. Let me provide some context. In Image 1, you'll notice that I've scrolled ...

What is the reason that when a variable is embedded within another variable, its value remains anchored to its initial declaration?

When looking at the code below, one would anticipate the output to read "text something different" but instead it displays "text something". var dynamic = "something"; var thistext = "text " + dynamic; dynamic = "something different"; console.log(this ...

PHP regular expression /only match 10 whole digits/;

Currently, I am working on updating a PHP script that contains the following code snippet: function CheckNumber(MyNumber) { var MN = /^\d{10}$/; if (MN.test(MyNumber)) { return true; } return false; } The current script enfor ...

Is it possible to send two parameters to a JavaScript function using HTML?

Seeking help to develop an .html page where two string inputs are passed as parameters to a .js function, which then returns the longer of the two strings based on their lengths. Initially, I successfully created a functional .js script in VS CODE. Here i ...

Ensure that the textbox remains valid when the reset button is clicked in Bootstrap

After implementing the code for bootstrap textbox and textarea, I encountered an issue when using the reset button. When entering an invalid shop name followed by a valid address and clicking on the Reset button, it resets the form. However, if I then only ...

Suggestions for improving the way time durations are calculated using jQuery

Although I have some experience with jQuery and javascript, I am still relatively new to the world of programming. I can write basic scripts, but I feel that there is room for improvement in optimizing some of my code. Specifically, I'm working on a p ...

Ways to identify when a person has scrolled a specific distance

Is it possible to modify the appearance of my top navigation bar once the page has been scrolled a specific number of pixels? I assume this requires some sort of listener to track the amount of scrolling. I came across element.scrollTop, but it doesn' ...

What is the process for eliminating the invocation of a function in Jquery?

I am currently facing an issue with my application. When I launch the Ficha() function, it initiates an ajax call and works perfectly fine. However, another ajax call is made later to load HTML tags that also need to invoke the Ficha() function. The prob ...

Implementing Bootstrap in Angular 2 using vanilla JavaScript/ES6: A step-by-step guide

Currently, I am in the process of developing an Angular 2 application without TypeScript and facing difficulties with bootstrapping it as I cannot find any relevant examples. My app, which does not include the bootstrap() function, starts off like this: ...

What is the best way to vertically reposition two divs within the DOM using jQuery?

I need some help with this task. Here is the code I'm working with: <div class="one">You might also invite...</div> <div class="pic1"></div> <div class="name1">Some name</div> <div class="pic2"></div> & ...

Fixing content at the bottom inside a container with Bootstrap 4

My app is contained within a <div class="container">. Inside this container, I have an editor and some buttons that I always want to be displayed at the bottom of the screen. Here's how it looks: <div class="container> // content here.. ...

Check out the HTML display instead of relying on console.log

Need help displaying the result of a JavaScript statement in an HTML page instead of console.log output. I am new to coding! Here is the JS code snippet: $.ajax({ url: 'xml/articles.json', dataType: 'json', type: ...

Showing outcomes from various API requests

I have encountered an issue while making two API calls to a backend server and trying to display both responses in JSON format to the user. Strangely, alert 2 is showing as 'undefined' when I check the console, while alert 1 works perfectly fine. ...

Encountering difficulty with the Cheerio find method during web scraping

I have been working with the code below: var request = require('request'); var cheerio = require('cheerio'); var url = "http://www.mbl.is/feeds/fp/"; request(url, function(err, resp, body) { if (err) throw err; $ = chee ...

Properly spaced website images

I'm trying to display my images side by side with a slight margin in between them. However, when I apply a `margin-right: 10px;` to each div, the last image doesn't line up properly with my title bar. Is there a way to create spacing between the ...