Automated jQuery Anchor Navigation with Custom Pixel Offset

Visit our live page:

After incorporating some jQuery code (with the help of StackOverflow), I have managed to automate the linking of my chapter titles. For instance, clicking on "Chapter Three" will take you directly to the third chapter on the page.

If you look at the live page, you'll notice that my sticky navigation bars obstruct the beginning of each chapter. I am looking for a way to offset each jQuery anchor by 200px vertically so that the starting point of each chapter is correctly displayed.

Here is the HTML structure for the Chapters Nav:

<nav id="chapters">
    <ul>
        <li><a href="#">Chapter One</a></li>
        <li><a href="#">Chapter Two</a></li>
        <li><a href="#">Chapter Three</a></li>
    </ul>
</nav>

This is how the Article section is structured in HTML:

<article class="clearfix">  
    <h1>Chapter One</h1>
    ...
</article>

Below is the jQuery code snippet being used:

// Chapters - locate the navigation and headers
var nav = $('#chapters li a'),
    articles = $('#main article > h1');

// Assigning unique ids/hrefs to each chapter
nav.each(function (i) {
    nav.eq(i).attr('href', '#article-' + i);
    articles[i].id = 'article-' + i;
});

Answer №1

To achieve this, you can rearrange the elements as follows:

<h1>Chapter One<a id="article-0" style="position: relative; top: -200px;">&nbsp;</a></h1>

In this code snippet, the anchor being linked is not within the h1 tag but inside a 'stub' element that is positioned lower by a few pixels.

An improved approach would be to use CSS classes like so:

.v-offset { 
    position: relative;
    top: -200px;
}

...

<h1>Chapter One<a id="article-0" class="v-offset">&nbsp;</a></h1>

You can utilize jQuery's append() method for dynamically adding the anchors:

nav.each(function (i, ele) {
    $(ele).attr('href', '#article-' + i); // $(ele) == $nav.eq(i)
    $(articles[i]).append('<a id="article-' + i + '" class="v-offset">& </a>');
});

Answer №2

To achieve indentation, apply either margin-left:200px or padding-left:200px to the specified element.

Answer №3

If you wish to shift the text in a list, there are different ways to achieve that effect:

Using HTML

<ul>
    <li>test</li>
    <li>test2</li>
    <li>test3</li>
</ul>

With CSS

  ul{
        margin-left:200px;
    }

Alternatively,

Using HTML

<ul>
    <li>test</li>
    <li>test2</li>
    <li>test3</li>
</ul>

In CSS

li{
    text-indent:200px;
    list-style-type: none;
}

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

Is it possible to load jQuery dialog from the Controller/View?

I have implemented a jQuery dialog feature to upload files in my application. When the file is selected and submitted, a controller is invoked to process the file and send it to the model for insertion. The model then communicates back to the controller wi ...

The bottom border of the check boxes is not displaying in Internet Explorer

https://i.stack.imgur.com/EAkMC.pnghttps://i.stack.imgur.com/ysU1R.png While the checkboxes function correctly in Chrome, they are experiencing issues in Internet Explorer. Below is the HTML code being used: <div class="patient-div-w" style="width: 9 ...

The HTML element seems to be missing its height parameter

My element doesn't have a set height, but it's populated with images causing the Anchor around the images to be unclickable unless I assign a fixed height to .portfolio. Any ideas on how to resolve this? HTML Snippet : <h1 class="text-c ...

Splitting a string into individual parts using PHP

$team = "Admini-aruturek,Kura126"; $parts = explode('-', $team); foreach ($parts as $part){ echo substr($part, 0, strpos($part, ',')); echo substr($part, strpos($part, ',')+1); } So, you w ...

Create a custom ajaxComplete function for a specific ajax request

Is there a way to override the ajaxComplete() function defined in a common .js file that is included on all pages of my application? Alternatively, how can I prevent it from being called specifically for certain $.ajax() requests? ...

JavaScript: Replace image title on mouse over

I am having trouble adding '_hover.jpg' to my images on mouse over. Can anyone provide assistance with this issue? Thank you in advance. <script type="text/javascript> $(document).ready(function () { $(".img").mouseover(function (e) { ...

Prevent the body from being redirected to the static menu

One issue I'm facing is that when I resize my page, the body content ends up in the fixed menu area. I have been using a combination of pixels and percentages, but how can I prevent this from happening? Check out this example on jsfiddle <div cl ...

What is the method for determining the numerical worth of the px containers?

https://i.stack.imgur.com/0K2TD.png Total width of the bar is set to 500px, with the red box at a width of 150px, the yellow box at 200px, and the green box at 50px. CSS Styles: .box { float:left; width:150px; box-shadow:3px 3p ...

Using jQuery, extract the input value and verify its accuracy. If correct, display the number of accurately predicted results

I am attempting to extract the value from an input field and validate if the answer is accurate. Rather than simply indicating correctness, I aim to analyze and display the number of correct responses alongside incorrect ones. Encountering difficulties wit ...

The evaluation of jQuery did not function properly on the less.css file

I have a jQuery function that I am using as shown below @winheight:`$(window).height()` within my less file. However, when I compile it to CSS, I encounter a compiler error I am currently using a software called Crunch Compiler Errors You are usin ...

Storing data with jQuery

Looking to store objects in jQuery for events. Each event will have a date, title, and some text that needs to be stored in an array. Wondering if using a multi-dimensional array would be the best way to go so I can easily iterate through them with a count ...

I'm encountering an issue with the submission button in the wizard, I suspect it could be a problem

The submit button is not working on this wizard. I believe the issue lies within the JavaScript of the form wizard. When I click the submit button, nothing happens. Upon inspecting the submit button code, I found it to be like this... <a href="#finish" ...

Using an Ajax call to show a date picker when a button is clicked

I've created buttons for each ID in the "habitaciones" table. When a button is clicked, it should display a specific datepicker with dates disabled for that ID. To achieve this, I utilized an Ajax function where the form should be displayed based on ...

Creating a unique Bootstrap 4 custom checkbox design without the use of the "for" attribute on the label

Are there alternative methods for maintaining the Bootstrap 4 custom checkbox design without utilizing the input's ID and label's 'for' attribute? The styling for when the box is checked disappears if these are removed. For instance: ...

Is there a way to smoothly incorporate a new animation into a page, while ensuring that existing elements adjust to make room for it?

I am looking to smoothly fade and move a hidden element (display:none) within the page, shifting other elements to animate into their new positions as necessary. When I reveal the hidden element by changing it to "display:initial," it should seamlessly a ...

Alter the URL path with the help of jQuery

I developed a website with content in two different languages, each stored in separate folders named en and bn. My goal is to toggle between these folders when clicking on an href element using jQuery. <a class="eng_link " style="" href="#"> English ...

I'm finding it difficult to grasp the concept of CSS positioning

I've been diving into the world of HTML/CSS and decided to challenge myself by converting a PSD file to HTML. Here's what I've been working on: So far, I've encountered an issue where there is some space between two of my divs, and I c ...

Adjust the font size within the grid layout

I'm utilizing the grid system to display 7 small images with a number next to each one, just like in this example. In my initial attempt, I didn't use the grid system, which made the process quite complex and time-consuming, taking me around 60 t ...

Angular and Datepair seem to have trouble cooperating

I've been struggling to solve this issue for a few days now and I feel like it's time to seek help from you wonderful people. I am currently developing a calendar application using Angular along with the datepair/datepicker/timepicker libraries. ...

Every time I use Get for ajax calls, I keep getting hit with a frustrating

Initially, I was making a call to a [web method] using the POST method. However, since I need to receive data back, I attempted to switch to using the GET method instead. The previous implementation using POST was successful. Unfortunately, using GET resu ...