Effortless scrolling implementation

How can I make the scroll smooth and animated with this code structure?

Check out my JSFiddle here: https://jsfiddle.net/v7qhzo8q/

The code structure is as follows:

<nav id="nav">
 <ul id="top">
    <li><a href="#">Home</a></li>
    <li><a href="#vinedo">Viñedo</a></li> 
    <li><a href="#elaboracion">Elaboracion</a></li>
    <li><a href="#contacto">Contacto</a></li>
 </ul>
</nav>

<div id='vinedo' class="div1">
  Vinedo DIV
  <a href="#top">Back to top!</a>
</div>
<div id="elaboracion" class="div2">
  Elaboracion DIV
  <a href="#top">Back to top!</a>
</div>
<div id="contacto" class="div3">
  Contacto DIV
  <a href="#top">Back to top!</a>
</div>

CSS:

.div1 {background:red;height:1000px;}
.div2 {background:green;height:1000px;}
.div3 {background:blue;height:1000px;}

Answer №1

If you give this a try, it might just be the solution you're looking for, provided I've grasped your needs correctly. If you wish to adjust the scroll duration, you can simply modify the number just before the "return false" code. Currently, it's set at 600 milliseconds.

    $(function() {
      $('a[href*="#"]:not([href="#"])').click(function() {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
          var target = $(this.hash);
          target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
          if (target.length) {
            $('html, body').animate({
              scrollTop: target.offset().top
            }, 600);  //adjust this number here to change the scroll duration
            return false;
          }
        }
      });
    });

Answer №2

If you're looking for a simple and convenient way to implement smooth scrolling on your website, check out this link and follow the setup instructions provided in the Readme. I hope this helps you achieve the desired result. Smoothscroll for Websites

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

Rotating an object in Three.js: Animate back and forth along two different azimuth angles

My three.js project features a 3D object that is meant to be viewed from the front only, as it appears as a single plane and is transparent from the back... Using orbitControls, I have restricted movement of both azimuth and polar angle... To enhance the ...

Error: The value of 'PlacesService' property is not defined and cannot be read

I am working towards gathering a selection of locations that meet specific criteria by making a "routeRequest" and storing them. To achieve this, I aim to utilize Google Places to search for locations around the central point of my map. let map = new goog ...

Is there a problem with the string comparison in my JavaScript code?

I am dealing with various XML files specific to different operating systems. Here is an excerpt from the SunOS XML: <osname>SunOS </osname> This data is extracted using jQuery: var osname = $(this).find('osname').text(); However ...

I attempted to set up a Discord bot using JavaScript on Replit, but unfortunately, it seems to only respond when I mention the bot specifically, rather than to any regular

I've successfully created a python discord bot, but I'm encountering issues with the javascript one. Despite trying various solutions from StackOverflow, I'm unable to understand how to get it working. The plethora of online solutions for d ...

Press a button to navigate through a series of web pages

Currently, I am working on a website focused on profiles. The challenge I am facing is implementing fixed side buttons (SKIP and ADD) that will cycle through the list of profiles. I understand that I may need to create an array to store all the profiles, ...

What are some best practices for utilizing `element.offsetParent` in HTML SVG elements?

I'm currently updating some JavaScript code that relies on the .offsetParent property. The recent changes in the application involve the use of SVG elements, which are causing issues with the JavaScript as mySvgElement.offsetParent always returns unde ...

Why are my images only 1 pixel tall in Internet Explorer 8?

<img src="blah.png" width="150" height="auto"> When viewed in Firefox, Chrome, or Safari, the image will appear as intended. However, Internet Explorer may cause the image to display at a height of only 1 pixel while maintaining the width of 150 pix ...

Disabling eslint does not prevent errors from occurring for the unicorn/filename-case rule

I have a file called payment-shipping.tsx and eslint is throwing an error Filename is not in camel case. Rename it to 'paymentShipping.tsx' unicorn/filename-case However, the file needs to be in kebab case since it's a next.js page that s ...

What are some common applications of JSON?

Can you explain the purpose of JSON in Javascript and PHP and when it is necessary to use a JSON method? I checked out the link below but couldn't find any information on how JSON is implemented in actual projects. http://www.json.org/js.html ...

Is it possible to use Chrome developer tools to find out the current file's name?

I am having difficulty locating a specific code within my online shop on Prestashop. The meta-data of the current page contains the content="Shop on PrestaShop" string, but when I search for it in Webstorm to identify which page file it is located in, I ca ...

What methods can I use to get my kahoot botter to produce unpredictable answers?

After attempting to create a kahoot botter using the updated kahoot.js library, I came across this code snippet that supposedly answers random questions: const Kahoot = require("kahoot.js-updated"); var kahoots = [] for (var i = 0; i < bot_cou ...

Creating your own browser extension similar to Firebug: A comprehensive guide

As a beginner dotnet developer, I am looking to create an Internet Explorer addon similar to Firebug in Firefox. Specifically, I need the HTML and CSS features of Firebug without all the extras. I'm not sure where to start or which platform to choose. ...

What could be causing a case where this.props.posts is undefined in React and Redux

Recently, I attempted to follow a React and Redux tutorial on a blog. However, when working with the PostList component, I encountered an error related to the reducer binding. My main goal was simply to render the renderPost function by calling the reducer ...

Assistance needed in obtaining the ID of a specific table row using jQuery

Currently, I am working on an AJAX request using Jquery and PHP. The task involves looping through an array to generate a table. During each iteration, a new ID is assigned to the table row. When users click on the "read me" link, additional content is f ...

Searching in TypeScript tables with Angular's search bar

I've successfully completed a basic CRUD application, but now I need to incorporate a Search Bar that can filter my table and display rows with matching letters. I'm unsure how to approach this in my component. I've seen examples using pipe ...

How can I retrieve the row index in an Angular Mat-Table that has expandable content?

Having a mat-table with expandable content on a page, I am seeking a solution to record the row number of the table when clicked. While I successfully achieved this with a regular table in the HTML file using: <tr mat-row *matRowDef="let row; columns: ...

Create an interactive webpage that automatically generates new HTML elements after retrieving JSON data from a Web API during page load

I am currently in the process of developing a hybrid Android App using Phonegap/Apache Cordova. The main function of my app is to retrieve data from my web API, which is being served through JSON. I have implemented the following code snippet for this task ...

What are some ways to make session variables available for sub applications?

I am currently working on setting up an API to interact with a MongoDB database, which I have integrated as a subapplication. In my server controller, I have created a session variable. However, I am facing an issue where the session variables are not be ...

Determining the Last Modified Date for a Sitemap using Javascript

I am trying to replicate the date format shown within <lastmod></lastmod> tags in a sitemap.xml file. How can I do this? The desired output is as follows: <lastmod>2016-01-21EEST18:01:18+03:00</lastmod> It appears that 'EEST ...

Why is the Angular directive '=&' value binding to the scope showing as undefined?

An Angular directive has been defined within my application: (function() { 'use strict'; angular .module('almonds') .directive('security', ['$animate', 'AuthFactory', directive]); fun ...