Prevent additional content from displaying upon page load

I am experiencing an issue with my sidebar numbers that expand on hover to reveal text beside them. Upon refreshing the page, the 'hidden' content briefly displays before collapsing again. How can I prevent this unwanted behavior?

Below is the code in question:

<div class="sidebar">
<ul>
<li><a href=""><span>01</span> HomePage</a></li>
<li><a href=""><span>02</span> SubPage 1</a></li>
<li><a href=""><span>03</span> SubPage 2</a></li>
<li><a href=""><span>04</span> SubPage 3</a></li>
<li><a href=""><span>05</span> SubPage 4</a></li>
</ul>
</div>


.sidebar {
  position: fixed;
  width:80px;
  height:100vw;
  top:0;
  left:0;
  z-index:100;
  background:#111;
  color:#fff;
  overflow:hidden;
}
.sidebar ul {
  margin:0;
  padding:0;
  width:200px;
  margin:10px;
  list-style:none;
}
.sidebar a span {
  margin:0 10px 0 0;
}
.sidebar a {
  color:#fff;
  font-size:14px;
  text-decoration:none;
}

And here is the jQuery code:

$(document).ready(function () {
    $('.sidebar').hover(function(){
        $(this).animate({width:'110px'},500);
    },function(){
        $(this).animate({width:'35px'},500);
    }).trigger('mouseleave');
});

I would appreciate any guidance on what may have gone wrong and how I can rectify it. Thank you!

Answer №1

Give a new definition to the CSS attribute width: 35px and eliminate the line for $.trigger('mouseleave').

You can achieve the same result using pure CSS as well. Adding .stop() before each $.animate() will prevent the menu from repeatedly opening and closing if you hover over it rapidly.

$(document).ready(function() {
  $('.sidebar').hover(function() {
    $(this).animate({
      width: '110px'
    }, 500);
  }, function() {
    $(this).animate({
      width: '35px'
    }, 500);
  });
});
.sidebar {
  position: fixed;
  width:35px;
  height:100vw;
  top:0;
  left:0;
  z-index:100;
  background:#111;
  color:#fff;
  overflow:hidden;
}
.sidebar ul {
  margin:0;
  padding:0;
  width:200px;
  margin:10px;
  list-style:none;
}
.sidebar a span {
  margin:0 10px 0 0;
}
.sidebar a {
  color:#fff;
  font-size:14px;
  text-decoration:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sidebar">
<ul>
<li><a href=""><span>01</span> HomePage</a></li>
<li><a href=""><span>02</span> SubPage 1</a></li>
<li><a href=""><span>03</span> SubPage 2</a></li>
<li><a href=""><span>04</span> SubPage 3</a></li>
<li><a href=""><span>05</span> SubPage 4</a></li>
</ul>
</div>

Here's the simpler version with just CSS:

.sidebar {
  position: fixed;
  width:35px;
  height:100vw;
  top:0;
  left:0;
  z-index:100;
  background:#111;
  color:#fff;
  overflow:hidden;
  transition: width .5s;
}
.sidebar:hover {
  width: 110px;
}
.sidebar ul {
  margin:0;
  padding:0;
  width:200px;
  margin:10px;
  list-style:none;
}
.sidebar a span {
  margin:0 10px 0 0;
}
.sidebar a {
  color:#fff;
  font-size:14px;
  text-decoration:none;
}
<div class="sidebar">
  <ul>
    <li><a href=""><span>01</span> HomePage</a></li>
    <li><a href=""><span>02</span> SubPage 1</a></li>
    <li><a href=""><span>03</span> SubPage 2</a></li>
    <li><a href=""><span>04</span> SubPage 3</a></li>
    <li><a href=""><span>05</span> SubPage 4</a></li>
  </ul>
</div>

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

What could be causing the Javascript Swal Fire function to not work properly?

<script type="text/javascript"> function selectCustomer() { var customerList = document.getElementById("customerlist"); var selectedValue = customerList.options[customerList.selectedIndex].value; if (selectedValue == "add_ ...

Trouble importing the Node module

After diligently following the documentation on NPM site for a third party nodejs module, I encountered an error when executing a JS script with Node. The error message indicated that the node module was not found. The specific node-module being used is ...

Customize the Header Background Color in React Table

I'm working on customizing a re-useable table component using vanilla CSS. I have defined six color variables and want users to be able to select one of them to change the table header background color. However, I am unsure how to make this color choi ...

HTML and CSS - Overcoming Challenges with Vertically Centering Content

Having trouble with aligning code within floated divs? In my left div, a span and image are stuck at the bottom left, while in the right one, text is fixed to the top-right. Check out how it looks currently. If you can provide some guidance on fixing thi ...

PHP not displaying Javascript error message

Greetings all, I'm new to this forum so please bear with me. I've encountered an issue with my php script. Specifically, when it reaches the point where it checks if the variable $beds equals 'nopref', the only way I can get the message ...

Changing the number format in Python after applying styling

I am working with a dataframe that looks like this: Name Amount A 3,580,093,709.00 B 5,656,745,317.00 After applying some CSS styling, the Amount values turn into scientific notation, such as 3.58009e+09 and 5.39538e+07. Name Amount A 3. ...

Steps for creating a Java Script method within a Java Script constructor

Is having a method inside my constructor possible? This is how I call my constructor: new EnhancedTooltip($("taskPreview1")) and this is how it's defined: ///<var> Represents the controls</var> var EnhancedTooltip = function (toolTipObj ...

Utilizing vue.js router guards with asynchronous user information retrieval

Currently, I am developing a Vue.js application that requires the implementation of router guards to restrict access based on user roles and prevent unauthorized users from accessing the application. The login process involves integrating an external appli ...

One common issue popping up in Webpack logs is the error message "net::ERR_SSL_PROTOCOL_ERROR" caused by a call to sock

Using react on the front-end and .net core 3.1 on the back-end. Running webpack on localhost:8080 for client-side development. Configuring proxyToSpa in Startup.cs: applicationBuilder.UseSpa(spa => { spa.UseProxyTo ...

The TypeScript error states that the argument type 'string | undefined' cannot be assigned to the parameter type 'string'

Receiving TS error that says "Object is undefined" I am attempting to retrieve the "userid" from my headers. However, I keep encountering the error message "Argument of type 'string | undefined' is not assignable to parameter of type 'str ...

The Yii form does not show the save message

One of my forms shows the save message, but the other one doesn't display it. The first view confirms the data is saved, while the second one does not show any confirmation message, even though the data is stored in tables. For the code snippets rel ...

The arrangement of grid elements does not impact the visual result in CSS

Having issues organizing my HTML into a grid format where the elements are not aligning correctly or moving at all. I am using Firefox and an online server to edit and host the website. Additionally, I am receiving a warning that 'grid-template-area&a ...

Leverage the TypeScript-generated controller within an Angular directive

I currently have an Angular controller that was generated using TypeScript: class FileManagerController { ... constructor($scope) { $scope.vm = this; ... } ...functions... } Can you guide me on how to integrate this controller i ...

Pass an object along with the rendering of the page using ExpressJS and Mongoose

After reading through this post: mongoose find all not sending callback I am currently working on sending an Object along with a page in my nodejs/expressjs application, instead of just responding with JSON data. This is the route for my page: //Get lat ...

Having trouble with Sprite loading in Three.JS?

My sprite is not loading in Three.JS. Can someone assist me in troubleshooting my code? I'm uncertain of the exact issue... it must be a simple mistake that I have missed. The code seems to function properly but the sprite is not visible. <!DOCTY ...

Implementing a POST method in jQuery Mobile without using AJAX

How can I send a post request without using ajax in jQuery Mobile? I've tried sending the post request but it's not working on BlackBerry OS 5. Can anyone help me with this? I currently have a post method that uses ajax and it works fine. Howeve ...

Ensuring data complies with specific criteria prior to inserting into a database

I am in the process of developing a new invoicing system that requires inserting multiple invoice items into the database. Before inserting these items, I need to ensure that they already exist in the items table. However, the current code alerts the user ...

Tips for structuring JSON data obtained from an API

When fetching data from an API and attempting to display it in my Angular application, I am able to retrieve and show the data, but it is not formatting correctly. { "countryCode": "BS", "countryName": "BAHAMAS", "publishedDate": "2020-03-30T0 ...

What could be causing the malfunction of my bootstrap collapse code?

Is there a way to showcase a collapsed div within a table cell? <td> <a class="text-decoration-none" data-bs-toggle="collapse" href="{{ '#collapseMsg' ~ message.id }}" role="button" aria-expand ...

Navigating to external URLs using a Form-based approach in .NET Framework

Is it possible to direct to an external website using a URL as a Command Argument? I attempted to redirect to facebook.com, but it resulted in http://localhost:58709/www.facebook.com%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20% ...