Navigation through dots on a single page

Having an issue with my dot navigation and anchor links placement. I want the anchors to be vertically centered in the middle of the section, regardless of window size. Here's what I'm aiming for :

For larger windows :

And for smaller windows :

Additionally, how can I implement a highlighted link on the dot navigation? Similar to this example: where the dot turns green when on that specific section.

Check out my JSFIDDLE here

Here's the HTML code :

<div class="nav">
        <a href="#section01">01</a>
        <a href="#section02">02</a>
        <a href="#section03">03</a>
        <a href="#section04">04</a>
</div>


<div class="content">
        <div class="section" id="section01">Section 01</div>
        <div class="section" id="section02">Section 02</div>
        <div class="section" id="section03">Section 03</div>
        <div class="section" id="section04">Section 04</div>
</div>

And the CSS code :

body {
  margin:0;
}


.nav {
  position:fixed;
  right:0;
}


.nav a {
  display:block;
  margin:10px;
  height:30px;
  width:30px;
  border-radius:100%;
  background:grey;
}

.nav a:hover {

    background: orange;
}

.nav a.active{
    background: red;
}

.section {
  height:200px;
  background:pink;
  margin:100px;

  }

Answer №1

Looking for a solution? Check out this jsFiddle link: https://jsfiddle.net/5oz0uzuz/1/

Here's a snippet of the code provided:

 $('a').click(function(){
     var $elem = $($(this).attr("href"));
   var offset = $elem.offset().top - ($(window).height() / 2) + ($elem.height() / 2);
   $('html, body').animate({
     scrollTop: offset
   }, 400);
   $(this).addClass("active").siblings().removeClass("active");
   return false;
 });

An explanation of the added active class and scrolling functionality is also included in the code above.

Hoping that this information proves to be useful!

Answer №2

To ensure the anchor has the active class, include the following code snippet inside a click function right before any animation is initiated.

$('.navigation a').removeClass('active');
$(this).addClass('active');

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

Utilizing the <style scoped> feature within my Angular template

When adding CSS styles in the specified htm file's templateUrl while loading a directive, I wonder if this is a bad idea. Will it be repeated every time the template is instantiated on the rendered page, or does Angular handle this differently? By usi ...

I have successfully set up micro-cors on my system, and as I tried to compile, I received the following outcome

While working on the Next.js Stripe project, I ran into an unexpected problem that I need help with. ./src/pages/api/webhooks.ts:3:18 Type error: Could not find a declaration file for module 'micro-cors'. 'E:/Project/longlifecoin/node_module ...

Creating a reusable field for reactive forms in Angular: A step-by-step guide

I need assistance with creating a versatile field component for reactive forms, but I am facing challenges in retrieving the value from the custom-input element. <form [formGroup]="form" (ngSubmit)="submit()"> <custom-input i ...

The issue I'm facing is that the ng-click functionality in Angular JS is not functioning properly when the

Upon loading the page, my JavaScript function creates a tree structure from JSON data and generates a list of anchor tags which look like this: <a ng-click="shareParent(4619)">Some data</a> Despite associating the ng-click directive with the ...

The combination of Ajax.BeginForm, Partial View, and Model State is causing some issues

In my ASP .NET MVC project, I have the Create action method below: public ActionResult CreateVMNetwork(int vmid) { VMAssignIps vmips = new VMAssignIps() { TechnologyIP = new TechnologyIP() { TechnologyID = ...

When using angularjs, the $window.location.href may cause the page to load without any

I have a dilemma where I have linked all my CSS and JS files in the index.html file, but subpages are located in a templates directory. When using $window.location.href, only a plain HTML page is returned without any CSS styles. The page renders fine when ...

Utilizing JavaScript Fetch with User Input to Integrate OpenWeather API Retains Previous Data on HTML Page

I have been working with JavaScript Fetch to retrieve data from the OpenWeather API. In my project, I have created a form where users can input the name of a city to view its weather information. However, I am facing an issue where the data from the previo ...

The authentication0 router fails to initiate navigation

I'm currently using Auth0 in combination with Angular 2. The issue I am encountering is that my login code isn't redirecting to the home page after authentication. Based on my understanding, Auth0 does not handle the redirection process itself. ...

In order to utilize Next.js with pkg, you must enable one of the specified parser plugins: 'flow' or 'typescript'

Utilizing next.js with the pkg in my project, following the steps outlined in this tutorial, I encountered an error when running the pkg command: > Error! This experimental syntax requires enabling one of the following parser plugin(s): 'flow, t ...

Tips for showcasing the contents of a file on a website

Greetings! I am a newcomer to the world of web development and I am seeking a method to showcase file content on a webpage. Presently, I have succeeded in loading text file content and displaying it in a large text box. However, I am now interested in di ...

Displaying a component after retrieving a value from AsyncStorage in a React Native application

I have developed a React Component that saves user settings in the AsyncStorage and retrieves them upon loading. The functionality of storing and retrieving data is working fine, but I am facing an issue where the component renders before the values are ...

Unexpected token error occurs when using map-spread operator in vue-test-utils combined with jest

I recently set up testing for my Vue project by following the instructions provided in this helpful guide here Upon completion of the guide, I proceeded to create a test for one of my components. However, when I ran jest, I encountered the following error ...

RxJS emits an array of strings with a one second interval between each emission

Currently, my code is set up to transform an Observable<string[]> into an Observable<string>, emitting the values one second apart from each other. It's like a message ticker on a website. Here's how it works at the moment: const ...

Creating a unique function to map an array in VueJS specifically designed for table manipulation

I am currently working on displaying and sorting data in a bootstrap table within VueJS. My goal is to change the date format within an array retrieved from an API endpoint. The original date format is in "January 21, 2010" and I need it to be in "MM/DD/Y ...

When working with NextJs, you may encounter a ValidationError indicating that the configuration object is invalid. This error occurs when Webpack has been initialized with a configuration object that doesn't

After upgrading from Next v12 to v12.2.3, I encountered a problem when running "yarn dev" with a new middleware.js file in the root directory: ValidationError: Invalid configuration object. Webpack initialization error due to mismatched API schema. - Deta ...

Making sure the axios API call is completed before rendering the child component with props

Check out the snippet of code I've provided: function StoryCarousel(props) { const [ivrDests, setIVRDests] = useState([]); useEffect(() => { async function getIVRDests() { var data = { "customer-id": ...

What is the best way to include default text in my HTML input text field?

Is it possible to include uneditable default text within an HTML input field? https://i.stack.imgur.com/eklro.png Below is the current snippet of my HTML code: <input type="text" class="form-control input-sm" name="guardian_officeno" placeholder="Off ...

Establish an interval that loops through the elements of an array

I'm currently working on implementing an interval for the length of an array stored in state. The particular array eventDate consists of 4 elements, and I want the function to cycle through values 0, 1, 2, 3, then loop back to 0. This is my code ...

The initial click event for the input element in Jquery is not functioning correctly

I found a jQuery date selector online and the script looked something like this... <script type="text/javascript> $(document).ready(function () { $("#date3").click(function() { $("#date3").scroller({ preset: 'datetime' }); wheels = []; whe ...

Using Quill with express and Node.js can greatly enhance the user experience of

I am encountering issues with displaying the Quill editor on my website. My project consists of two ejs files - a layout file and an add_new file which includes the layout. The layout.ejs file is as follows: <div class="snippet" data-lang="js" data-h ...