The JQuery mouseover and mouseout functionality for CSS is not functioning properly

Struggling to create an expanding navigation bar using CSS. Managed to implement it, but ran into issues when it kept resetting after navigating to a new page. Resorted to using jQuery to resolve the problem by utilizing mouse enter and mouse leave events. While alerts worked fine, applying CSS via jQuery failed to produce the desired outcome.

Here is the jQuery code:


$(function(){
    $("nav").mouseenter(function(){
        .css({"width": "200px", "overflow": "visible"});
    });

    $("nav").mouseleave(function(){
        .css({"width": "60px", "overflow": "hidden"});
    });
});

And here is the HTML code:


<body>

<div class="container">
    <header>  
        <h1>Chemistry Home</h1>
    </header>

    <nav>
        <div id="logo"> 
            <img src="Images/Logo.png">
        </div>                  

        <ul>
            <li>
                <a href="Index.HTML">
                    <i class="fa fa-HOME fa-2x" id="on"></i>
                    <span class="nav-text" id="on">HOME</span>
                </a>                      
            </li>
            <li>
                <a href="standards_&_info.HTML">
                    <i class="fa fa-info fa-2x"></i>
                    <span class="nav-text">STANDARDS & INFORMATION</span>
                </a>
            </li>
            <li>
                <a href="interesting_info.HTML">
                    <i class="fa fa-magic fa-2x"></i>
                    <span class="nav-text">INTERESTING INFORMATION</span>
                </a>
            </li>
            <li>
                <a href="JOBS.HTML">
                    <i class="fa fa-briefcase fa-2x"></i>
                    <span class="nav-text">JOBS</span>
                </a>
            </li>
            <li>
                <a href="photo_&_video.HTML">
                    <i class="fa fa-picture-o fa-2x"></i>
                    <span class="nav-text">PHOTOS & VIDEOS</span>
                </a>
            </li>
        </ul>
    </nav>

    <div id="cover"></div>
    
    <div class="content">
        <div class="two-box">
            <div class="title">
                <h2>Why</h2>
            </div>

            <div class="info">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit... </p>
            </div>
        </div>

        <div class="two-box">
            <div class="title">
                <h2>Quotes</h2>
            </div>

            <div class="info">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit... </p>
            </div>
        </div>              
    </div>

    <footer> 
        rueregrewghe
    </footer>
</div>          

</body>

Link to the JSFiddle

Being new to jQuery, it's possible I made a rookie mistake. Any help or guidance on resolving this issue would be greatly appreciated. Thank you!

Answer №1

You cannot use .css as a standalone expression, you must use $(this).css instead. Your CSS code is incorrect and your fiddle does not have jQuery included...

            $(function(){
                $("nav").mouseenter(function(){
                    $(this).css({"width": "200px", "overflow": "visible"});
                });

                $("nav").mouseleave(function(){
                    $(this).css({"width": "60px", "overflow": "hidden"});
                });
            });

Here is the corrected version: http://jsfiddle.net/h8nro6s3/2/

Answer №2

Instead of using jQuery, I opted for CSS for simpler tasks. Utilizing the :hover pseudo-class is a great option for these types of tasks. Take a look at the example below.

nav:hover {
    width:200px;
    overflow:visible;
    transition:ease 1s;
}

Check out the working example on JSFiddle

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

Tips on clearing all cookies using jQuery

Similar Question: Clearing all cookies with javascript I'm looking to create a checkbox that can clear all previously stored cookies in my forms with just one click. How can I achieve this using the jquery cookie plugin? I've searched both K ...

Utilize $.Ajax() to send JSON data using a POST request

I am experiencing an issue while trying to send a JSON object using $.ajax() in jQuery with a POST method from my pure HTML page to a datapower endpoint. The response header in Firebug displays "internal server error." Can anyone help me identify the mis ...

`Implementing a dynamic list with image and button using Bootstrap 5`

I've been exploring ways to create a container with 4 images aligned next to each other, accompanied by a button beneath each image. My goal is for the layout to be responsive, meaning that as the screen size adjusts, the elements inside should also a ...

Omit the tag from the submission section

Currently, I am utilizing a <p> tag as a submit button/area by setting a specific id in jquery. <p id="xyz"></p> My requirement is to insert an input field inside this <p> tag to include a particular value that will be submitted u ...

Prevent the text within the textarea from wrapping onto the next line

My challenge involves a textarea where text overflows onto the next line when it exceeds the maximum characters. However, I want the text to continue on the same line with a scroll bar for better visibility. Here's an illustration: text here flows on ...

The CSS scale property is not working as expected when used in a React.js application, specifically

working environment ・next.js ・react ・typescript https://www.youtube.com/watch?v=ujlpzTyJp-M A Toolchip was developed based on the referenced video. However, the --scale: 1; property is not being applied. import React, { FunctionComponent ...

How can you style a two-item list in Material-UI React to display the items side by side?

I have a list that contains two items: 'label' and 'value'. This is the current layout: https://i.stack.imgur.com/HufX7.png How can I rearrange the items on the right to be positioned next to the label on the left? https://i.stack.im ...

How to achieve divs with see-through text using CSS?

How can I create a CSS (non-HTML5) based website with a filled div that has a cutout revealing an image underneath? There are various overlays and images involved, making the use of static images inconvenient. Additionally, I anticipate needing to scale th ...

Tips for positioning the navbar to avoid covering the logo:

I am experiencing difficulty in aligning my navbar. When I resize the window, the navbar overlaps with the logo... Here is the Fiddle Here is the code: <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation" style="background: rgba(0,0, ...

Unable to access external API through Jquery due to CORS error when making multipart formposts

There exists an external REST API used for uploading resources to a repository, which can be accessed via the curl command: curl -k -X POST -u username:password \ -F package_id="1234" \ -F file=@"path_to_a_file" \ https:/ ...

What is preventing me from setting the height of a span to 0 pixels?

It seems that when the height is set to 0px, the element doesn't visually shrink... <div id="bg"> <div id="animate"><span>WINNER ALERT! Click here to get a million dollars!!!</span></div> </div> #bg { back ...

Searching for a post by content or title on Flask can be done by utilizing the search functionality built

I've created routes and forms, but when I tried to search, it showed "cannot be found." Here is my code: routes.py: @app.route('/search',methods=['GET','POST']) def posts_lists(): q = request.args.get('q') ...

What is the best way to uncheck a checkbox once both of my input fields [type=text] have been cleared or are empty

Is there a way to uncheck the checkbox input if both text inputs are empty, and check it if one of the text inputs is filled? $('input[name="t2"],input[name="t3"]').keyup(function() { $('input[name="t1"]').prop("checked", $.trim($( ...

Html elements remain stationary and do not shift positions

I am attempting to customize a web page by repositioning an input text element. Below is the code I'm using: <body> <h1>Shopping List</h1> <input id="search" type="search"> <input id='newItem' type="t ...

Combining various functions into a single button

I am currently working on creating a full-screen menu that functions like a modal. Everything seems to be working fine, except for the fadeOut animation. Can someone please help me understand what is causing issues with my scripts/codes? I want the content ...

To horizontally center a fixed element in HTML and set its width to match the parent's,

Is there a way to center a fixed div inside its parent element? I'm trying to make the fixed div have the same width as its parent, but it's not working. What could be causing this issue? Check out this example on jsFiddle Here is the HTML code ...

Sending a string array to MVC controllers through ajax

I've been struggling to pass a list of strings from a multiple select to the controller. Despite seeming like a simple requirement, I've spent hours trying to figure it out without success. I've done some research on this but haven't be ...

Successful jQuery JSON request to ASP .NET site in Internet Explorer, but facing issues in Firefox and Chrome

Currently, I am retrieving JSON data from my ASP .NET website by utilizing the following jQuery code: $.ajax({ type: 'POST', url: '/blah/default.aspx/GetTestData', contentType: 'application/json; charset=utf-8', dataT ...

What is the best way to populate missing days in an array up to the current date that do not already contain the "Present" element?

Consider the array below which contains attendance data for an employee (Retrieved from Mongo using Ajax): [{"_id":"5fcdcd49c3657d1e05b846f5","title":"Present","allDay":true,"start":"2020-11- ...

A revolutionary approach - Empowering websites with individual scrolling panels

I have a unique design layout that includes a top navigation, side navigation, and content. Both the side navigation and the content sections are taller than the page. My goal is to eliminate the need for a scrollbar for the entire page, but still mainta ...