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 :

https://i.sstatic.net/pK4Ex.png

And for smaller windows :

https://i.sstatic.net/EjnZU.png 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

The background image in CSS fails to display when a relative path is utilized

Currently, I am working on a JavaFX project which has the following file structure (CEP serves as the root directory): CEP +img menu.jpg +src +css_files MainMenu.css The main objective is to set the background image from the img dir ...

The body can only stretch up to a certain percentage of its width, not a full 100%

Recently, I've been encountering an issue with my webpage where the body does not stretch 100% the width of the screen. I've tried numerous CSS rules to fix it, but nothing seems to be working. When you view the page in a browser, it appears to c ...

Transform an array containing objects into a single object

Currently exploring the Mapael Jquery plugin, which requires an object to draw map elements. In my PHP code, I am returning a JSON-encoded array of objects: [ { "Aveiro": { "latitude": 40.6443, "longitude": -8.6455, ...

Different color for background of division

I am working with a structure that looks like this: <div class="wrapper"...> <a href="#"...>blah</a> <div class="post"...>stuff</div> </div> This structure repeats multiple times on a dynamic page. I am looking f ...

Is there a method to identify if the dark theme is enabled in iBooks while working within an EPUB file?

Is there a specific code or feature that iBooks uses to enable the customization of certain sections within the book, such as changing to lighter colors when night mode is on? ...

Jquery and Ajax failing to function properly with multiple dropdown selections

I've been working on creating a functionality to have multiple dropdown menus using Bootstrap, JQuery, and AJAX. The goal is to select multiple countries from these dropdowns and store the selected values in CSV format in my database. Initially, I had ...

JavaScript promises do not guarantee the execution of the loop

There was a block of Javascript code that I needed to modify. Initially, the code looked like this: if(!this.top3){ const promises = this.images.map((el, index) => { return this.getData(el.title, index); }); return Promise.all(promise ...

When clicked, you will be redirected to the details page

Currently, I am developing a React JS application that showcases a list of companies fetched from a Node.js/Express server in a table format. When clicking on each row, it triggers a modal to display some details about the company. The modal consists of t ...

I am encountering an issue with the return ( statement and I'm unable to comprehend the reason behind it

import { connect } from 'react-redux' import { Link } from 'react-router-dom' class MyFavoriteStories extends React.Component { markAsFavorite = (e) => { this.setState({ bgColor: "blue" }) } render () { con ...

What is the best method for dividing a user interface into several arrays of keys, each grouped by type?

Given a simple structure: structure IPerson { firstName: string; lastName: string; age: number; city: string; favoriteNumber: number; isMarried: boolean; hasDriverLicense: boolean; } How do I create arrays containing keys grouped by data typ ...

When running the `vue-cli-service test:unit` command, an error involving an "Unexpected token" message related to the usage of the spread operator

Within my code, I am utilizing the destructuring operator. However, during the module build phase, I encountered an "Unexpected token" error. Any suggestions on how to resolve this issue without completely rewriting my code to avoid using the destructuring ...

The select2 plugin seems to be having trouble recognizing the Li click functionality

I have a select menu that is using the select2 plugin. I am trying to add a class to the <select> element when a menu option is clicked, but it doesn't seem to be working as expected even after testing with an alert. https://jsfiddle.net/ysm4qh ...

jQuery/AJAX caching problem

I have a question regarding retrieving data using $.ajax multiple times. I am facing an issue where the data is not getting refreshed with each call, instead I am receiving the same old data every time I make a call to $.ajax. Surprisingly, this code was w ...

What could be the reason behind receiving a TypeError upon refreshing the page, where it claims that a state is not found even though that may not be the actual situation?

Within my application, I have a component called Wall. Users are redirected to the Wall component after successfully logging in from the Login component. In the Wall component, I am retrieving and displaying the user's name that they used during regis ...

Displaying a progress bar while fetching data in Vue: A step-by-step guide

I am working on developing a progress bar using vue js and bootstrap for my desktop application. Within the template, I have the code that will generate the necessary markup: <div class="container-fluid p-0 vh-100" v-if="isLoading&quo ...

"Troubleshooting problems with jQuery click function during real-time search execution

In my code, I have an empty result div that dynamically fills up with item elements as search queries are entered (using jQuery ajax). If I use the following code: $(".result").click(function(){ alrert("click on whole result class captured") }); I r ...

How to include a javascript file in a different file and compile it using Node.js

For running my practice JS files, I rely on Node. As an example, I use node bts.js. In order to implement a queue, I decided to install Collections by using the command npm install collections --save. You can also see this reflected in the hierarchy shown ...

Exploring the dynamic duo of Github and vue.js

As I am new to programming and currently learning JavaScript and Vue.js, I have been trying to deploy my first Vue.js app without success. Despite spending hours on it, I still cannot figure it out and need to seek assistance. Every time I try to deploy, ...

Button creation not functioning properly with JQGrid's custom formatter

While using the JQGrid customer formatter to create a button, I am encountering an issue where the button is not displaying. function viewLineBtn(cellvalue, options, rowObject) { alert(cellvalue +","+options+","+rowObject); var rowid = options.row ...

best way to display json data in a table-style list

I am utilizing a function that I call from AJAX to retrieve data in JSON format. My goal is to present all the data in a Table listview sequentially. While my function successfully retrieves the data, I am struggling with how to display it in the table vie ...