What is the proper way to activate the stickynavbar functionality within the navigation wrapper in an HTML document?

As I delve into enhancing my website, I've made the choice to incorporate jquery, despite my lack of proficiency in Javascript. However, I've hit a roadblock when attempting to execute the stickyNavbar function on either the navigation wrapper, nav tag, or ul tag. Below is the snippet of code that's causing me some confusion:

$(function () {
    $('.header').stickyNavbar({
    activeClass: "active",          // Class to be added to highlight nav elements
    sectionSelector: "scrollto",    // Class of the section that is interconnected with nav links
    animDuration: 250,              // Duration of jQuery animation
    startAt: 0,                     // Stick the menu at XXXpx from the top of the this() (nav container)
    easing: "linear",               // Easing type if jqueryEffects = true, use jQuery Easing plugin to extend easing types - gsgd.co.uk/sandbox/jquery/easing
    animateCSS: true,               // AnimateCSS effect on/off
    animateCSSRepeat: false,        // Repeat animation everytime user scrolls
    cssAnimation: "fadeInDown",     // AnimateCSS class that will be added to selector
    jqueryEffects: false,           // jQuery animation on/off
    jqueryAnim: "slideDown",        // jQuery animation type: fadeIn, show or slideDown
    selector: "a",                  // Selector to which activeClass will be added, either "a" or "li"
    mobile: false,                  // If false nav will not stick under 480px width of window
    mobileWidth: 480,               // The viewport width (without scrollbar) under which stickyNavbar will not be applied (due usability on mobile devices)
    zindex: 9999,                   // The zindex value to apply to the element: default 9999, other option is "auto"
    stickyModeClass: "sticky",      // Class that will be applied to 'this' in sticky mode
    unstickyModeClass: "unsticky"   // Class that will be applied to 'this' in non-sticky mode
  });
});
And now I'm uncertain about where exactly in the code I should insert it.

<!DOCTYPE html>
<html>
    
    <head>
        
    <title>Sunny Bistro</title>
   
    <link href="//cdnjs.cloudflare.com/ajax/libs/animate.css/3.0.0/animate.min.css" rel="stylesheet" type="text/css">

        
    <meta charset="UTF-8">
 
            
            
        
        <link rel="stylesheet" type="text/css" href="CSS/Base%20CSS.css"> 
        
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  
    
        <script src="jquery-3.3.1.min.js"></script>
        

    </head>
    
    
    <body>
 
        <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
<script src="//cdn.jsdelivr.net/stickynavbar.js/1.3.4/jquery.stickyNavbar.min.js"></script>
        
        <script>var _nearplace=_nearplace||{};_nearplace.organization='0615ec61-9279-49ee-9d05-b021594eef39';_nearplace.widget='d2b48791-d4a8-439c-8a2f-c59d214275a3';window.nearplace||function(){var s=document.createElement('script');s.type='text/javascript';s.async=!0;s.src='https://widget.nearplace.com/loader.js';document.addEventListener("DOMContentLoaded",function(){(document.scripts[0]||document.head).appendChild(s)})}();</script>

<div id="header" class="header">
   <nav id="nav">
       <ul class="nav navbar-nav">
           <li>
             <a href="#home">Home</a>
           </li>
           <li>
             <a href="#about">About</a>
           </li>
           <li>
             <a href="#services">Services</a>
           </li>
           <li>
             <a href="#contact">Contact</a>
           </li>
       </ul>
   </nav>
</div>
        
        
           <section id="bottle">
        <article>
        <h6><img src="CSS/Images/SunnyBistro-8.png" ></h6>
        <h1>The Sunny Bistro</h1>   
        <h2>The sunny bistro</h2>   
        </article>
        <div class="parallax" data-velocity="-.3"></div>
    </section>
    <section id="story-freext">
        <article>
        
        </article>
        <div class="parallax" data-velocity="-.1"></div>
        <div class="parallax" data-velocity="-.5" data-fit="525"></div>
    </section>
    <section></section>

<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="../jquery.scrolly.js"></script>
<script>
    $(document).ready(function(){
       $('.parallax').scrolly({bgParallax: true});
    });
</script>


    </body>


</html>
I'm still fairly new to all of this, so any help would be greatly appreciated :) Image 1: 1 Current state: 2

Answer №1

First things first, it's important to tidy up your HTML code by removing unnecessary elements.

Next step is to ensure jQuery is imported right before the closing </body> tag. I have removed the lines importing jQuery from both the <head> and <body> sections as they are not required.

Regarding the function stickynavbar, where did you come across this function?

<!DOCTYPE html>
<html>
<head>
    <title>Sunny Bistro</title>
    <meta charset="UTF-8">

    <link rel="stylesheet" type="text/css" href="CSS/Base%20CSS.css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.0.0/animate.min.css" rel="stylesheet" type="text/css">
</head>

<body>

    <div class="header">
        <nav id="nav">
            <ul class="nav navbar-nav">
                <li>
                    <a href="#home">Home</a>
                </li>
                <li>
                    <a href="#about">About</a>
                </li>
                <li>
                    <a href="#services">Services</a>
                </li>
                <li>
                    <a href="#contact">Contact</a>
                </li>
            </ul>
        </nav>
    </div>



    <section id="bottle">
        <article>
            <h6><img src="CSS/Images/SunnyBistro-8.png"></h6>
            <h1>The Sunny Bistro</h1>
            <h2>The sunny bistro</h2>
        </article>
        <div class="parallax" data-velocity="-.3"></div>
    </section>
    <section id="story-freext">
        <article>

        </article>
        <div class="parallax" data-velocity="-.1"></div>
        <div class="parallax" data-velocity="-.5" data-fit="525"></div>
    </section>
    <section></section>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
<script src="https://cdn.jsdelivr.net/stickynavbar.js/1.3.4/jquery.stickyNavbar.min.js"></script>
<script>
    $(document).ready(function () {
        $('.header').stickyNavbar();
    })
</script>
</body>

</html>

Answer №2

@vlad instead of relying on a plugin, I recommend using a simple script to achieve the desired effect. Simply insert the following script at the bottom and include the corresponding CSS in the stylesheet.

<style>
.stickey-header{
position:fixed;
width:100%;
height:40px;
}
</style>
$(window).scroll(function(){
if ($(this).scrollTop() > 50) {
   $('.header').addClass('sticky-header');
} else {
   $('.header').removeClass('newClass');
}
});

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

Leveraging Ajax for executing a PHP function

Hello, I am currently working on creating a button that will trigger a PHP function to add products to Shopify directly from a web application. As of now, my result.php file is successfully displaying Amazon products. You can view the code here: In this ...

Tips for placing a button on top of a Div element with overflow set to hidden?

My goal is to have a button placed over a box, but I am facing difficulty achieving this. I have experimented with z-index, grid-content, and other methods, but the button refuses to show over the box as desired. The placement of the button inside the box ...

Issues with React Native imports not functioning properly following recent upgrade

Hey there, I’ve been tasked with updating an old React-Native iOS project from version 0.25.1 to 0.48.0. However, I’m encountering several compiler issues and struggling to navigate through the code updates. The project includes an index.ios.js file s ...

Checking authorization with CognitoUser solely for "code" provided by CognitoUser (npm: amazon-cognito-identity-js)

Currently, I am in the process of implementing the "forget password" feature. However, I have encountered an issue where I would like to verify the "verification code" first before setting a new "password". Unfortunately, the system currently requires bo ...

Storing knockout view model data in a database and fetching it back

I am currently working on a web form that utilizes knockout, and I need to add a new feature that allows users to save the form as a draft in the database. Later on, they should be able to load it again to make modifications or submit it. Is there a built ...

What is the best way to incorporate my light/dark mode theme into index.js and have my header serve as the switch location?

Could someone assist me with setting up a theme around my index.js components and using a switch state in my header.js to toggle between light and dark themes? I'm looking for a way to simplify the process to minimize the amount of code needed. As a ...

How to update deeply nested subdocuments in Mongoose

I am faced with an issue while attempting to update sub documents using mongoose by utilizing the request.body without explicitly passing the _id of the sub documents. Despite successfully updating, mongoose is deleting the _id from the sub documents. Con ...

The Bootstrap navigation pills do not function properly when using a forward slash in the tab link

I am currently working with Bootstrap and using nav pills. I have noticed that if I include a / sign in the link of a tab, it causes that specific tab to malfunction and triggers a JavaScript error in jQuery's own code when clicked on. How can I go ab ...

Tick the checkbox to indicate that I want to insert an image in the adjacent column for the same row

Every time I click on the checkbox, an image should appear in the adjacent column for that specific row. Despite using the addClass() method and targeting the td, the image is appearing in all rows instead of just the selected one. Could somebody help me ...

Struggling to manage errors within React ApolloClient

As I delve into comparing a real-world application using this Authentication server example where data is fetched, I am facing difficulties in capturing client errors. This is because Apollo Client sends its own exceptions to the console. My sample server ...

Creating a CSS grid layout with a scrollbar positioned on the left side and content filling from left to right

How can I move the scrollbar to the left while keeping ng-repeat population from left to right? I'm using an ng-repeat to fill a grid that is 3 by X (Width by height). By default, the scroll bar appears on the right side. I want to position it on the ...

Steps for updating the value of a key/value pair in an array using a handleChange function

Managing a dynamic grid with varying numbers of rows and 3-5 cards per row can be complex, especially when using useSelector and .map to populate the data for each card. The initial state includes multiple key/value pairs, some unique to each record. Visua ...

Selenium error: Element is unclickable ... Another element is blocking the click, but go ahead and click

When attempting to utilize the code snippet below return this.driver.findElement(By.css("div[class*='img']")).click(); An error occurs: Uncaught WebDriverError: unknown error: Element is not clickable at point (525, 889). Other element would r ...

Utilizing Bootstrap for creating responsive CSS regulations

Is there a way for specific parts of my website to automatically resize on a mobile device without having to manually input responsive CSS rules based on page width? Can Bootstrap be used to trigger these changes instead? For instance, I noticed that Boot ...

Using CSS height 100% does not function properly when the content overflows

Here's what's going on with this code snippet: HTML <div class="one"> <div class="will-overflow"> </div> </div> CSS html, body { width: 100%; height: 100%; } .one { height: 100%; background: r ...

Send attributes to BoundField.as_widget() in Django

Is it possible to pass HTML attributes using BoundField.as_widget() by specifying them in the attrs parameter? According to the Django documentation, this method works as follows: BoundField.as_widget(widget=None, attrs=None, only_initial=False)¶ This fu ...

Using ng-if within ng-repeat in an AngularJs table structure

Hey there, I'm currently trying to utilize ng-if within ng-repeat. My condition is if {{dev[2]}} == "". If this condition is met, I want to display <td>Non Valide</td>; otherwise, I simply want to display the data inside 'dev'. I ...

A comprehensive guide on associating a JavaScript function with an element attribute

I am looking for a way to assign a JavaScript function to an HTML attribute. For example: <li data-ng-repeat="job in jobList" class= dynamicClass(str) data-filter = "dynamicFilter(str)"> The reason I want to do this is because the class name and ...

React.js, encountering unusual behavior while implementing a timer

I'm currently working on a React project to create a basic timer. The start button should initialize the timer, while the stop button should pause it. However, I've encountered some unexpected behavior. When I click on start for the first time, ...

Utilizing CSS3 Patterns: A Guide

Is there a way to utilize these patterns effectively? I attempted to implement them, but it was not successful. http://jsfiddle.net/ujmWH/ I also tried on tinkerbin with the same outcome. Appreciate any advice or guidance. ...