-1

So I tried adding some jquery to my website and linked the .js file to the HTML file right above the tag.

The goal of the jquery: To add a nav bar transparent to solid transition like in this website.

Here is a fiddle which shows how I'm trying to achieve this.

And finally, here's a snippet of my code:

$(document).ready(function() {
    $(window).scroll(function() {
        if ($(document).scrollTop() > 10) {
            $('#nav').addClass('shrink');
        } else {
            $('#nav').removeClass('shrink');
        }
    });
});
/**********BODY GENERAL**********/
body {
    margin: 0;
    height: 2500px;
    /* just to demonstrate how it will looks with content */
}

header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
}

/* Fix this one day */
.bg-img {
    background: url('Images/BkgImg/Abkimage.JPG') no-repeat center center;
    background-size: cover;
    height: 100vh;
}

strong {
    font-weight: bold;
}

/*********NAVIGATION*********/
@media screen and (max-width: 900px) {
    nav {
        grid-template-columns: 100%;
        grid-template-rows: auto;
        grid-gap: 1em;
    }
}

.menu1 {
    grid-column: 1;
}

.menu2 {
    grid-column: 2;
}

.logo {
    grid-column: 3;
    font-family: 'Montserrat', sans-serif;
    font-weight: lighter;
    font-size: 28px;
    width: 500px;
    background-position: center;
    background-size: contain;
    background-repeat: no-repeat;
    height: 7vh;
    margin-bottom: 25px;
    color: #000;
    text-transform: uppercase;
    letter-spacing: 3px;
}

.menu3 {
    grid-column: 4;
}

.menu4 {
    grid-column: 5;
}


/**************HOVER ANIMATION**************/
div>a {
    font-family: 'Raleway';
    text-transform: uppercase;
    text-decoration: none;
    color: #000;
    position: relative;
    font-size: 0.8rem;
}

div>a:hover {
    color: #000;
}

div>a:before {
    content: "";
    position: absolute;
    width: 100%;
    height: 1px;
    bottom: -4px;
    left: 0;
    background-color: #000;
    visibility: hidden;
    -webkit-transform: scaleX(0);
    transform: scaleX(0);
    -webkit-transition: all 0.3s ease-in-out 0s;
    transition: all 0.3s ease-in-out 0s;
}

div>a:hover:before {
    visibility: visible;
    -webkit-transform: scaleX(1);
    transform: scaleX(1);
}

/**********MAIN HEADER***********/
header {
    color: white;
    justify-content: center;
    align-content: center;
    top: 0;
    bottom: 0;
    left: 0;
}

/**********BODY*****************/
.Minfo {
    color: red;
    width: 100%;
    padding-top: 100px;
    font-family: 'Montserrat', sans-serif;
    font-weight: lighter;
}

.subtitle {
    padding-left: 4em;
    padding-top: 29em;
    font-size: 100%;
    color: #fff;
}

.title {
    font-size: 3em;
    text-align: left;
    color: #FFF;
    padding-bottom: 0px;
}

.subtext {
    padding-top: 0px;
    color: #FFF;
}

/************* NAV TRASPARENT TO OPAQUE ANIMATION *************/
nav {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    grid-gap: 1em;
    grid-auto-rows: auto;
    text-align: center;
    align-items: center;
    background: transparent;
    */ z-index: 100;
    transition: all ease .5s;
    height: 70px;
}

.navLinks {
    transition: all ease .5s;
}

#nav.shrink .navLinks {
    padding: 10px;
    transition: all ease .5s;
}
<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <title>Centennial It's Academic</title>
    <link href="/favicon.ico" rel="shortcut icon" type="image/x-icon">
    <link href="main.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Poiret+One" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400" rel="stylesheet">
</head>
<body>
    <div class="bg-img">
        <header>
            <div id="nav">
                <nav class="container">
                    <div class="menu1">
                        <a id="navLinks" href="#home">Home</a>
                    </div>
                    <div class="menu2">
                        <a id="navLinks" href="#upcoming">Tournaments</a>
                    </div>
                    <div class="logo">
                        <p>It's Academic</p>
                    </div>
                    <div class="menu3">
                        <a id="navLinks" href="#history">History</a>
                    </div>
                    <div class="menu4">
                        <a id="navLinks" href="#faq">Contact Info</a>
                    </div>
                </nav>
                <!-- This cluster of info -->
            </div>
        </header>

        <div class="Minfo">
            <div class="subtitle">
                CENTENNIAL<br>
                <div class="title">
                    It's Academic
                </div>
                <br>
                <div class="subtext">
                    Meets every Tuesday in Room 506
                </div>
            </div>
        </div>
    </div>
    <!-- Linking Jquery/Javascript -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</body>
</html>

If the snippet doesn't work properly, I also have a github pages link for this where the images and stuff will work properly.

Krupesh Kotecha
  • 2,396
  • 3
  • 21
  • 40
  • 1
    Jquery should be linked at the `head` and not in the `body`. – Paulie_D Jun 21 '18 at 14:21
  • https://jsfiddle.net/r8qn9we3/ – Paulie_D Jun 21 '18 at 14:23
  • 2
    Can you elaborate on what "not working" means in this case? – David Jun 21 '18 at 14:23
  • 1
    @Paulie_D aint that just absolutely fine: [Where to place JavaScript in an HTML file?](https://stackoverflow.com/questions/196702/where-to-place-javascript-in-an-html-file) – caramba Jun 21 '18 at 14:23
  • That didn't work :( –  Jun 21 '18 at 14:24
  • 1
    @Paulie_D Source? https://stackoverflow.com/questions/10994335/javascript-head-body-or-jquery - I've always understood that putting javascript at the end of the body (or making use of attributes like `async` etc) was best practice? – Tyler Roper Jun 21 '18 at 14:25
  • I generally place libraries in the head, while placing all the custom code at the end of the body. – Thomas Scheffer Jun 21 '18 at 14:33
  • 1
    The actual usage of Jquery requires that it be linked **before** any script you are running. So load JQ...then run the script. – Paulie_D Jun 21 '18 at 14:33
  • @Paulie_D right! Sorry I missed the part where jQuery code was included in the question. I would prefer saying include that part of code after you included jQuery, instead saying include it in the head. (In the head can still be after that part of code..) – caramba Jun 21 '18 at 15:03

1 Answers1

0

JQuery wasn't working because it was being loaded in the body and it should be loaded in the head of your html page. I also added the nav background transition by adding a new .background div that has a transition on it's height from 0 to 100%:

$(document).ready(function() {
    $(window).scroll(function() {
        if ($(document).scrollTop() > 10) {
            $('#nav').addClass('shrink');
        } else {
            $('#nav').removeClass('shrink');
        }
    });
});
/**********BODY GENERAL**********/
body {
    margin: 0;
    height: 2500px;
    /* just to demonstrate how it will looks with content */
}

header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
}

/* Fix this one day */
.bg-img {
    background: url('Images/BkgImg/Abkimage.JPG') no-repeat center center;
    background-size: cover;
    height: 100vh;
}

strong {
    font-weight: bold;
}

/*********NAVIGATION*********/
@media screen and (max-width: 900px) {
    nav {
        grid-template-columns: 100%;
        grid-template-rows: auto;
        grid-gap: 1em;
    }
}

.menu1 {
    grid-column: 1;
}

.menu2 {
    grid-column: 2;
}

.logo {
    grid-column: 3;
    font-family: 'Montserrat', sans-serif;
    font-weight: lighter;
    font-size: 28px;
    width: 500px;
    background-position: center;
    background-size: contain;
    background-repeat: no-repeat;
    height: 7vh;
    margin-bottom: 25px;
    color: #000;
    text-transform: uppercase;
    letter-spacing: 3px;
}

.menu3 {
    grid-column: 4;
}

.menu4 {
    grid-column: 5;
}


/**************HOVER ANIMATION**************/
div>a {
    font-family: 'Raleway';
    text-transform: uppercase;
    text-decoration: none;
    color: #000;
    position: relative;
    font-size: 0.8rem;
}

div>a:hover {
    color: #000;
}

div>a:before {
    content: "";
    position: absolute;
    width: 100%;
    height: 1px;
    bottom: -4px;
    left: 0;
    background-color: #000;
    visibility: hidden;
    -webkit-transform: scaleX(0);
    transform: scaleX(0);
    -webkit-transition: all 0.3s ease-in-out 0s;
    transition: all 0.3s ease-in-out 0s;
}

div>a:hover:before {
    visibility: visible;
    -webkit-transform: scaleX(1);
    transform: scaleX(1);
}

/**********MAIN HEADER***********/
header {
    color: white;
    justify-content: center;
    align-content: center;
    top: 0;
    bottom: 0;
    left: 0;
}

/**********BODY*****************/
.Minfo {
    color: red;
    width: 100%;
    padding-top: 100px;
    font-family: 'Montserrat', sans-serif;
    font-weight: lighter;
}

.subtitle {
    padding-left: 4em;
    padding-top: 29em;
    font-size: 100%;
    color: #fff;
}

.title {
    font-size: 3em;
    text-align: left;
    color: #FFF;
    padding-bottom: 0px;
}

.subtext {
    padding-top: 0px;
    color: #FFF;
}

/************* NAV TRASPARENT TO OPAQUE ANIMATION *************/
nav {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    grid-gap: 1em;
    grid-auto-rows: auto;
    text-align: center;
    align-items: center;
    background: transparent;
    */ z-index: 100;
    transition: all ease .5s;
    height: 70px;
    position: relative;
}

.navLinks {
    transition: all ease .5s;
}

#nav.shrink .navLinks {
    padding: 10px;
    transition: all ease .5s;
}


/*============= NEW CSS RULES ============*/

#nav {
  position: relative;
}

#nav .logo p {
  margin: 10px 0;
}

#nav .background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  background: #ededed;
  height: 0;
  transition: height 800ms ease;
}

#nav.shrink .background {
  height: 100%;
}
<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <title>Centennial It's Academic</title>
    <link href="/favicon.ico" rel="shortcut icon" type="image/x-icon">
    <link href="main.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Poiret+One" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400" rel="stylesheet">
    <!-- Linking Jquery/Javascript -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
    <div class="bg-img">
        <header>
            <div id="nav">
            <!---- NEW BACKGROUND ELEMENT HERE ---->
                <div class="background"></div>
                <nav class="container">
                    <div class="menu1">
                        <a id="navLinks" href="#home">Home</a>
                    </div>
                    <div class="menu2">
                        <a id="navLinks" href="#upcoming">Tournaments</a>
                    </div>
                    <div class="logo">
                        <p>It's Academic</p>
                    </div>
                    <div class="menu3">
                        <a id="navLinks" href="#history">History</a>
                    </div>
                    <div class="menu4">
                        <a id="navLinks" href="#faq">Contact Info</a>
                    </div>
                </nav>
                <!-- This cluster of info -->
            </div>
        </header>

        <div class="Minfo">
            <div class="subtitle">
                CENTENNIAL<br>
                <div class="title">
                    It's Academic
                </div>
                <br>
                <div class="subtext">
                    Meets every Tuesday in Room 506
                </div>
            </div>
        </div>
    </div>
</body>
</html>
Chase DeAnda
  • 15,963
  • 3
  • 30
  • 41
  • Hey man, this works on every platform but for some reason, the Jquery/Javascript won't work on Brackets.io. Do you think you know why? –  Jun 22 '18 at 13:57
  • No, I've never used brackets. A quick google search turned up with this though: https://stackoverflow.com/questions/22827704/jquery-wont-work-on-brackets-editor – Chase DeAnda Jun 22 '18 at 14:22