Using jQuery to gradually fade out my sidebar (div)

I have written the following code to hide my sidebar when the screen width is less than 1024 pixels, but it doesn't seem to be working. It worked fine on a previous website, so I'm not sure what the issue is here. Any help would be greatly appreciated.

Below is the HTML code:

<html>
  <head>
  </head>
  <body>

<script type="text/javascript" src="script.js"></script>
<script src="jquery1.js"></script>



        <div class="content">


        <!-- Sidebar -->
        <div id="sidebar">
        <!-- Sidebar Search -->

            <form class = "search" method="get" action="">
                <input class = "search_box" type="text" value="Search..." onfocus="if(this.value=='Search...')this.value=''"; onblur="if(this.value=='')this.value='Search...';" >

                <input class = "search_submit" type = "submit" value = "Search">
            </form>



            <!-- Sidebar Menu -->
            <ul id="sidebar_menu">           
                <li><a id="" href="#">Link 1</a>
                <li><a id="" href="#">Link 2</a>
                <li><a id="" href="#">Link 3</a>
                <li><a id="" href="#">Link 4</a>
                <li><a id="" href="#">Link 5</a>
                <li><a id="" href="#">Link 6</a>
                <li><a id="" href="#">Link 7</a>
                <li><a id="" href="#">Link 8</a>
            </ul>     
        </div>

        <!-- Main Content -->

<div id="main_content">

<h3>Lorem Ipsum</h3>
                <p>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pretium odio vitae erat pharetra eleifend. Sed in consectetur odio, vel venenatis nunc. 
                Sed felis sapien, ullamcorper eget arcu eget, consectetur posuere nisl. Ut blandit metus quis lectus ultricies, eu dignissim mauris rhoncus.
                Aliquam est libero, molestie eget lacus ac, luctus interdum enim. Nulla ac dapibus tellus, in bibendum leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
                Mauris in mi lorem. Vivamus nec risus nec nisi tempus bibendum ut eget mi. Phasellus eget luctus massa. 
               Vivamus et libero turpis.
            </p>

        </div>


    </div>
  </body>

Here is the associated CSS code:

#sidebar {
float: right;
margin: 10px;
margin-top: 0;
margin-bottom: 0;
width: 300px;
height: 100%;
background: #168AFF;
border-top-right-radius: 15px;
}

#sidebar_menu {
margin: 20px;
}

#sidebar_menu li {
list-style-type: none;
margin: 10px;
padding-bottom: 10px;

width: 250px;

}

#sidebar_menu li a{
text-decoration: none;
font-family: 'Open Sans', sans-serif;
font-size: 15px;    
color: #FFFFFF;
border-style: solid;
border-radius: 2px;
border-color: #585858;
background-color: #585858;
height: 42px;
width: 250px;
float: right;
margin-left: auto;
margin-right: auto;
margin-bottom: 5px;

}

#sidebar_menu li a:hover { background-color: #777777; border-color: #777777; }

Finally, here is my JavaScript code:

//run on document load and on window resize
$(document).ready(function () {

//on load
hideDiv();

//on resize
$(window).resize(function(){
    hideDiv();
});

});

function hideDiv(){

if ($(window).width() < 1024) {

        $("#sidebar").fadeOut("slow");


}else{

    $("#sidebar").fadeIn("slow");


}

}

Answer №1

It appears that the issue may be caused by the order in which you are including your script and the jQuery library. I recommend changing the order of inclusion from:

<script type="text/javascript" src="script.js"></script>
<script src="jquery1.js"></script>

to:

<script src="jquery1.js"></script>
<script type="text/javascript" src="script.js"></script>

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

Displays a white screen once the code is executed

I am attempting to retrieve data from a database, and it appears that I am fetching the information correctly. However, when I run the code, I am presented with a blank page. Since I am new to PHP and MySQL, I would appreciate some guidance on this issue ...

Combining arrays of objects sharing a common key yet varying in structure

Currently, I am facing a challenge while working on this problem using Typescript. It has been quite some time since I started working on it and I am hoping that the helpful community at StackOverflow could provide assistance :) The scenario involves two ...

Is there a way to prevent the letters from moving when I hover over them?

What occurs when the numbers are hovered over: https://gyazo.com/20b6426d435551c5ee238241d3f96b4d Whenever I hover over the pagination numbers, they shift to the right, and I am unsure of what mistake I made in my code that's causing this. Below, I ...

Can anyone suggest a method to preview a specific image before uploading on Internet Explorer?

One issue I am encountering is that Internet Explorer does not support the Filereader function in JavaScript. My solution was to send the file via ajax to the server, which would then respond with the file's content. Do you think this approach could w ...

Maintain course focus when updating

I'm currently working on a to-do list where clicking on an item adds the "checked" class. However, when I refresh the page, everything reverts back to its original state without the checked class. How can I maintain the state of the checked items? I& ...

Is there a way to automatically display the detailsPanel in Material-table upon loading?

I am currently working on creating a subtable for the main React Material-Table. So far, everything is functioning correctly as expected, with the details panel (subtable) appearing when the toggle icon is pressed. Is there a way to have it displayed by d ...

Using JavaScript and Tampermonkey to convert strings from an HTML element into an array

I am trying to change the names of months into numbers and place them in a table cell on a website. I thought using an array would make it easier to reference the month names by their corresponding array numbers, allowing me to add table tags before and af ...

Error message displayed: "Validator.elementValue is not a recognized function in JQuery Validation."

I am facing an issue with my form where I have two fields and I need at least one of them to be filled. I tried using a JQuery validate plugin but it seems that the require_from_group function is not working properly. Upon inspecting the console, I encoun ...

Creating a redirect button using React Material UI and React Router

I've been exploring how to use Material-UI with React and I'm struggling to figure out the best way to redirect to other pages or components. After reading various articles on different methods of redirection using react-router-dom, I still have ...

Interactive PDFs that launch a new browser tab rather than automatically downloading

I am facing an issue with my web API that is returning a JSReport as an encoded byte array. Despite multiple attempts to read the byte array, I keep encountering either a black screen or an error message indicating "failed to download PDF". Interestingly, ...

Is there a way to update a div element with an image without having to refresh

Currently working on a project for my studies where I am reading the x and y coordinates from a touch pad and drawing it in real time using PHP. I have created a draw page that takes these coordinates and displays them on an image, which is then shown on ...

Issue with CSS positioning: Struggling to place images outside a div with floating properties

Here is what I need to design along with the code I have written, HTML: <div className='relative mt-64 mb-40 md:h-56 h-36 w-full gradient z-10'> <img className='bg-transparent w-20 h-28 absolute md:top-40 md:lef ...

JQuery submit event not triggering object setting

I'm looking to gather input values from a form and store them in an object for an offer. After trying the following code on submit: $(document).ready(function(){ $('#formOffre').on('submit', function(e) { e.preventDefault ...

Tips for customizing the cursor to avoid it reverting to the conventional scroll cursor when using the middle click

When you wish to navigate by scrolling with the middle mouse button instead of using the scroll wheel, simply middle-click and your cursor will change allowing for easy scrolling on web pages. Even though I am utilizing style="cursor: pointer" and @click. ...

Ensure that a child container automatically adjusts its width to fit within a parent container of varying width

There is a wrapping container that contains three floated containers inside. The width of the wrapping container is variable. The inner container on the left has a width of 100px, and the inner container on the right has a width of 500px. The center contai ...

recording the results of a Node.js program in PHP using exec

I'm attempting to retrieve the output from my node.js script using PHP exec wrapped within an ajax call. I am able to make the call and receive some feedback, but I can't seem to capture the console.log output in the variable. This is how I exec ...

Problem encountered while attempting to sort a table with JavaScript and jQuery

Having trouble sorting my table by the content in the first column. I've tried implementing code similar to the one found in this ANSWER, but unfortunately, it's not working as expected for me. When you run the snippet, you can see that the table ...

Obtain the VW/VH coordinates from an onclick action in JavaScript

With this code snippet, you'll be able to retrieve the x and y coordinates of a click in pixels: document.getElementById("game").addEventListener("click", function(event) { console.log(event.clientX, event.clientY); }); However ...

Tips for resizing images from Amazon S3 using Sharp

I'm encountering an issue while attempting to resize an uploaded image from S3 using @aws-sdk/v3 in a Node.js API. Initially, I retrieve the object (image) from S3 by following this example: https://github.com/awsdocs/aws-doc-sdk-examples/blob/master ...

Is it possible to increase the delay in the nth-child selector?

I'm attempting to create a compact checklist that displays in a single row with items appearing sequentially. To achieve this, I utilized the :nth-child() selector in CSS as shown below: .animated { -webkit-animation-duration: 0.8s; animation-d ...