Modifying the verbiage in the menu based on location

Could anyone provide some assistance or guidance on how to solve a small issue I'm facing?

I would like the text in my navigation to change depending on my position on the site (position1, position2 etc.)

Here is a fiddle link for reference: http://jsfiddle.net/iBertel/ah2zj/6/

I need the fixed-nav text to dynamically adjust based on the current position.

EDIT: I have made changes to the fiddle, so it should make more sense now.

HTML

<div class="page-wrap">
    <div class="fixed-nav">
        <span>Position 1</span>
    </div>

    <div id="position1" class="link">
        page 1
    </div>
    <div id="position2" class="link">
        page 2
    </div>
    <div id="position3" class="link">
        page 3
    </div>
    <div id="position4" class="link">
        page 4
    </div>
</div>

CSS

html, body {
    width:100%;
    height:100%;
}
.page-wrap {
    width:100%;
    height:100%;
    position:relative;
}
.fixed-nav {
    position:fixed;
    top:50px;
    left:50px;
    width:100px;
    height:50px;
    background-color:#CCC;
}

.link {
    width:100%;
    height:100%;
    border-top:1px solid #F20;
}

JQUERY

No jQuery code yet - I am unsure where to begin with this.

I hope someone can assist me or point me in the right direction to find a solution to this problem.

Thank you for taking the time to read this. Cheers!

Answer №1

Is it possible to dynamically change the content of your fixed navigation bar based on how far the user has scrolled down the page?

If that's what you're looking for, you can achieve this with the following code:

$(document).ready(function(){
  $(document).scroll(function() {        
    if ($('body').scrollTop() > 600) {
      $('.fixed-nav span').text('Page 3+');
    } else if ($('body').scrollTop() > 200) {
      $('.fixed-nav span').text('Around page 2');
    } else {
      $('.fixed-nav span').text('Looking at page 1, ish.');
    }        
  });
});

Check out this demo on JSFiddle

Edit:

If you are unsure about the height of each 'page', you can use this alternative approach:

$(document).ready(function () {
  $(document).scroll(function () {
    $.each($('.link'), function (index, value) {
      var position = $(this).position();
      var top = position.top;
      var bottom = position.top + $(this).height();

      if ($('body').scrollTop() >= top && $('body').scrollTop() <= bottom) {
        $('.fixed-nav span').text($(this).attr('id'));
        return false;
      }
    });
  });
});

View this updated demo on JSFiddle

This script will update the navigation bar with the ID of the element currently in view as the user scrolls through the page.

Edit:

If you encounter compatibility issues with Firefox, you might need to include the following line of code:

var scrollTop = $(document).scrollTop();

See the revised version on JSFiddle

Answer №2

I believe I grasp your meaning, and I'm pretty sure I've addressed a similar inquiry before.

Perhaps this is what you're looking for...check out the attached Fiddle: Guidelines for implementing a jQuery navigation bar

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

Artwork expanding incorrectly on HTML canvas

I'm encountering an issue while attempting to draw on an HTML canvas. I've specified 50 circles and multiple lines within a canvas of size 1000x1000 px, but not all circles are appearing as expected. My assumption is that the elements are being ...

Implementing multiple filters with jQuery

Make a Selection `<select class="form-control" id="technology"> <option name="sort" value="2g" id="2g"gt;2G</option> <option name="sort" value="3g" id="3g"&g ...

JQuery Slider's Hidden Feature: Functioning Perfectly Despite Being Invisible

Having an issue with a jQuery slider on my local HTML page. The sliders are not showing up as intended. I want it to display like this example: http://jsfiddle.net/RwfFH/143/ HTML <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery ...

Passing an array of integer arrays to a controller method in ASP MVC using Ajax

I am encountering an issue with my AJAX call when trying to post data entered by the user on the webpage to a controller method. Unfortunately, the data never reaches the controller and always results in an error. I suspect that the problem lies in the typ ...

Trouble with Bootstrap popover functionality

Twitter bootstrap-2.3.2 popover is being utilized in my BackboneJs project. When I click, a function is triggered to open the popover: <li> <a class="candidPopover" href="#" id="loginUser" rel="popover"><%= candidateName %></a> & ...

Error parsing data in the $.ajaxSetup() function of JQuery

Currently, I am coding a program using jQuery. It was functioning perfectly in Firefox 3.5 until I upgraded to Firefox 4.0. Since then, the dreaded 'parsererror' keeps popping up and causing me quite a headache. I've pinpointed the exact pa ...

Nextjs exposes a bug in react-bootstrap's navbar component

After integrating react-bootstrap with nextjs, I encountered a strange issue with the NavBar component. The problem arises when I first load the navbar - everything appears as expected, https://i.stack.imgur.com/R9guE.png but upon refreshing the page, CS ...

Sorting content using jQuery local storage

I've been grappling with how to effectively sort the localstorage data output for quite some time now. Specifically, I'm looking to sort the output in ascending order based on the stored product IDs. Does anyone have a solution to tackle this iss ...

Pass a JavaScript variable to a PHP script using AJAX when a button is clicked, with a dynamically set href attribute

This is the current situation: There is a checkbox on each row of a table that represents some information An initially disabled button becomes enabled when any checkbox is checked The button is enclosed by an <a></a> tag as shown: <a>&l ...

Touchwipe incorporation - single-page website script

Today has been dedicated to troubleshooting and searching for a solution regarding the integration of TouchWipe () on a custom one-page-site script based on Parallax that I found perfect for my latest project (). The script itself performs beautifully wit ...

Replace old content with new content by removing or hiding the outdated information

I need to update the displayed content when a new link is clicked index html file <a href="" class="content-board" > <a href="" class="content-listing" > content html file <div class="content-board"> <div class="content-lis ...

JQuery class for swapping elements upon scrolling

I am currently working on a navigation bar that changes classes to create a fading effect for the background. The approach I have taken involves targeting the window itself and monitoring the scroll position of the user. If the user scrolls down more than ...

Challenges with implementing CSS transitions

I've been delving into CSS3 transitions, and I'm encountering some confusion. I can't tell if I'm misunderstanding something or if it's the browsers causing issues. Initially, I believed Opera had transition support starting from ...

How can we assign various class names depending on the value of an object?

I have a set of objects stored in my component. I need to dynamically apply different classes based on the value of the ErrorLevel property within each object. If an object has ErrorLevel equal to 1, I want to assign the following classes to various elemen ...

Is there a way to apply -webkit-line-clamp to this JavaScript content using CSS?

i have a random-posts script for my blogger website <div class="noop-random-posts"><script type="text/javascript"> var randarray = new Array(); var l=0; var flag; var numofpost=10; function nooprandomposts(json){ var total = ...

Challenges with Media Queries post Integration of MUI Library in React

I have been scratching my head for the last few hours. I needed to utilize a react library to design my dog app for a project. Opting for the MUI library, it has been effective for the grid layout. However, the challenge arises when attempting to implement ...

Tips for automatically scrolling to the bottom of a div when new data is added

I'm working on a project with a table nested inside a div, and it has some unique styling: #data { overflow-x:hidden; overflow-y:visible; height:500px; } Currently, as the table fills up with data, a vertical scroll bar appears. But I wa ...

Dynamically generating fields in JavaScript causes the fields to mysteriously vanish

Below is the JavaScript code I am working with: <script language="javascript"> function addInput() { document.getElementById('text').innerHTML += "<input type='text' value='' name='a1[]' size='60&a ...

Show JSON array items

My php file (history.php) generates a JSON object $i=1; $q=mysql_query("select * from participants where phone='".mysql_real_escape_string($_GET['phone'])."' limit 10"); while($rs=mysql_fetch_array($q)){ $response[$i] = $rs[&ap ...

Unusual body padding found in the mobile design

When visiting on a mobile device and inspecting the elements in Google Chrome, try disabling the style rule overflow-x: hidden from the body element and then resizing the window. You may notice a white vertical stripe (padding) appearing on the right side ...