Smoothly scroll down to the bottom of the page with a jQuery button using the anchor

Recently, I've been working on setting up a navbar with buttons that smoothly scroll down to specific sections when clicked. However, as a beginner in jquery, I'm having some trouble figuring out how to make it work seamlessly.

Can anyone guide me on what jquery code I should use to achieve this functionality?

html

  <div class="navbar navbar-transparent navbar-fixed-top" role="navigation">
<div class="container">
  <div class="navbar-header">
    <!-- The button functions as a navigation drop down for phones -->
    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
    <span class="sr-only">Toggle naviggation</span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    </button>
    <a class="navbar-brand" href="index.html">JEROME REYES</a>
  </div>
  <div class="navbar-collapse collapse">
    <ul class="nav navbar-nav navbar-right">
      <li class="active"><a href="index.html">Home</a></li>
      <li><a href="about.html">About</a></li>
      <li><a href="#work">Work</a></li>
    </ul>
  </div>
</div>

js

$(document).ready(function(){
  $('a[href^="#"]').click(function(){
    $('html , body').animate({
      scrollTop:0
    },'slow');
})

Answer №1


  $(document).ready(function(){
    $('.navbar-nav li a').click(function(){
      $("html, body").animate({
        scrollTop: $($(this).attr('href')).offset().top - 0
      }, 500);
    });
  });

Answer №2

$(document).ready(function() {
    $('.nav-headers').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
                }, 1000);
                return false;
            }
        }
    });
});

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

Passing Data Between Blazor App Pages Using .NET Core

Recently, I began learning how to create websites using the Blazor template. I'm having trouble figuring out how to pass data from one page to another. It's quite different compared to .NET CORE MVC web applications and I haven't been able t ...

bootstrap style list-group without overlapping

In the image below, you can see that I have a list of American states within a list-group class. My issue is that I am attempting to eliminate the whitespace and essentially move Arkansas below Alaska and so forth. Here is my current HTML and CSS: CodePen ...

Creating a soft focus effect in a specific region (behind the text)

While working on my homepage created with HTML/CSS/Javascript, I have text positioned at the top left corner of the screen. The challenge arises from the fact that I am using different backgrounds sourced from Reddit, and a script randomly selects one duri ...

developing a dropdown menu feature

I'm struggling with a small issue related to my drop-down menu function. My goal is to hide the visibility of a menu tab after it has been clicked for the second time. Below is the code snippet I've been working on: HTML:- <nav class="clea ...

Determining with jQuery if the right element has been successfully "dropped"

Imagine having 10 different draggable boxes and 2 droppable areas. You need to correctly match 5 boxes with droppable area 1 and the other 5 with droppable area 2. If a box intended for droppable area 1 is mistakenly dropped into area 2, it should not wor ...

When on a touch screen, event.relatedTarget will be null during a focusout event

When working with a textarea, I am trying to use the focusout event to capture the value of the clicked button that triggered the focusout, so I can later click it after some processing. This solution is effective on most devices, but I am encountering iss ...

extracting the data from the list item within an AJAX response

Attempting to create a search function that retrieves the value inside an li element from an AJAX response. Here is my HTML page: <form role="form" action="<?php echo base_url();?>comparison/selCourse" method="post" id="selAll"> <h4> ...

Is it possible to use jQuery to initiate a change event without losing focus

Here is a script that I am currently using: $(".b").hide(); $(".uid").bind("change", function() { if($(this).val().length>0) { $(".b").show(); } else { $(".b").hide(); } }); The current functionality requires me to click ou ...

How can I change the ActionLink in a MVC3 Grid column to an <a> tag?

Is there a way to swap out the MVC3 Grid column ActionLink with an <a> tag instead? grid.Column(format: (item) => Html.ActionLink("Delete", "Delete", new { id = item.ID }, new { @class = "delete-button" } ), style: "column-action") ...

Using tabs within a Dialog prevents the Dialog from adjusting its height automatically

Solved: Find the solution in the comments section below. I recently built a Tabs feature within a Dialog box, but ran into an issue where the height of the Dialog did not match up with the height of the tab. The problem arose when a form was placed insid ...

Tips for properly removing Bootstrap 4 tooltips when deleting their corresponding DOM element using html()

In my Bootstrap 4 project, I've implemented a live search box that displays results with tooltips for longer descriptions. I've written jQuery scripts to hide the search results and their parent div when certain events occur, like clearing the se ...

Netbeans fails to detect Jquery

Looking for some assistance here. I'm in the process of setting up my computer for a new project and for some reason, Netbeans isn't recognizing any of the .js files. Any tips on how to enable it to start recognizing these files? ...

Executing jQuery script with an ASP button: Step-by-step guide

Can anyone help me with executing a jQuery script using an ASP button? I need to run a VB/C# code first to check database connectivity, and if it fails, display a jQuery dialog box. The sequence of steps should be: Button Clicked > Run VB/C# command ...

CSS: Inject Text at the End of Input Text's Value

Hey there, I'm facing a minor issue. The user wants to add some funny text to an input field if a specific condition is met. For example: Normal Condition: the input field shows "#{model.user}" Abnormal condition: the input field should display "# ...

Where should I transform data into an HTML table: PHP or JavaScript?

I'm seeking guidance on structuring my website. I have data in my database that I want to dynamically display in different sections on my website. The main question is: Where should I handle the conversion of raw data to HTML code? Here are the opti ...

Tips for acquiring the initial fully visible item within a scrollable element with horizontal alignment through jQuery

I'm trying to use jQuery to find the first visible element inside a scrollable DIV, but I can't quite get it to work correctly. Can someone please help me identify what's causing the issue? $('div').on('scroll', functio ...

`After the initial instance, the menu title will be altered.`

I have a responsive menu using Bootstrap. When the menu is collapsed, I want it to display the name of the page, and when it is open, I want it to show "Menu." Currently, I have named it "Watch the Trailer" (so let's assume that we are on that page). ...

AngularJS Material Design sticky header MD table container

I am looking to create a table container to display records with a unique requirement - when the table reaches the top of the screen, its header should stick in place. Can you provide guidance on how to implement this feature? For reference, please check ...

Having Trouble with Jquery UI Autocomplete and JSON Integration

Hi everyone, I'm currently investigating why the autocomplete() method is not performing a GET request when I append a variable to the end of the source URL. Here's an example: <script> $(document).ready(function(){ var se ...

Automatically updating d3.js data in real-time using jQuery's setInterval function with support for JSON, XML, text, and other data

Is there anyone who can provide guidance on how to use jQuery's get method (for json, txt, xml, or any other format) in conjunction with setInterval from d3.js? I'm looking to update my d3 bar chart every N seconds. Alternatively, is there an exi ...