Is there a way to make this hamburger menu change into an "x" when clicked?

After trying numerous CSS and jQuery techniques without success, I am struggling to transform my hamburger menu into an X shape. Despite my efforts with rotating the lines using CSS or trying jQuery methods, I can't seem to get it right. The menu doesn't stay active and is often misaligned.

Below is a snippet of the HTML code:

<body>
    <div class="acetrnt-toggle" data-click-state="1">
        <span class="line-1"></span>
        <span class="line-2"></span>
        <span class="line-3"></span>
    <div/>
</body>

And here is the corresponding CSS code:

body {
    background: blue;
}

.acetrnt-toggle {
    cursor: pointer;
    padding: 15px;
    width: 60px;
}

.line-1, .line-2, .line-3 {
    cursor: pointer;
    border-radius: 4px;
    height: 5px;
    width: 60px;
    background: #000;
    position: relative;
    display: block;
    content: '';
}

.acetrnt-toggle:hover span {
    cursor: pointer;
    background: #e7e7e7;
}

.line-1, .line-2 {
    margin-bottom: 10px;
}

.line-1, .line-2, .line-3 {
    transition: all 300ms ease-in;
}

If you'd like to view the full codepen example, click here.

Answer №1

Revise your :active states to .active states and implement .toggleClass for the desired effect: https://codepen.io/anon/pen/ZjYZaj

$(".acetrnt-toggle").click(function(){
  $(this).toggleClass("active")
})
body {
  
  background: blue;
}

.acetrnt-toggle {

cursor: pointer;
padding: 15px;
width: 60px;
}

.line-1, .line-2, .line-3 {

cursor: pointer;
border-radius: 4px;
height: 5px;
width: 60px;
background: #000;
position: relative;
display: block;
content: '';

}

.acetrnt-toggle:hover span {

cursor: pointer;
background: #e7e7e7;

}

.line-1, .line-2 {

margin-bottom: 10px;

}

.line-1, .line-2, .line-3 {

transition: all 300ms ease-in;

}

.acetrnt-toggle.active .line-2 {

background-color: transparent;

}

.acetrnt-toggle.active .line-1 {

background: #fefefe;
  top: 10px;
  position: absolute;

}

.acetrnt-toggle.active .line-3 {

background: #fefefe;
  bottom: 10px
  position: absolute;

}

.acetrnt-toggle.active .line-1 {
transform: rotate(45deg);
  top: 37px;
}

.acetrnt-toggle.active .line-3 {
transform: rotate(-45deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<body>
  
  <div class="acetrnt-toggle" data-click-state="1">

<span class="line-1"></span>
<span class="line-2"></span>
<span class="line-3"></span>

<div/>
    
    </body>

Answer №2

After some trial and error, this is the solution I have found:

HTML:

<div class="acetrnt-toggle" data-click-state="1">

                    <span class="line-1 left-slash"></span>
                    <span class="line-2 hide"></span>
                    <span class="line-3 right-slash"></span>

                <div/>

CSS:

.left-slash {
  transform: rotate(45deg) translate(0px, 21px);
}

.hide {
  visibility: hidden;
}

.right-slash {
  transform: rotate(-45deg) translate(0px, -21px);
}

In terms of JavaScript, you will need to apply and remove each class on click in order to animate the hamburger into an X shape. One thing to note is that this solution relies heavily on the size of .acetrnt-toggle remaining constant.

Answer №3

even greater

$(document).ready(function() {
  $('#menu').click(function() {
    $(this).toggleClass('open');
  });
});
#menu {
  width: 70px;
  height: 55px;
  position: relative;
  margin: 60px auto;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
  -webkit-transition: .7s ease-in-out;
  -moz-transition: .7s ease-in-out;
  -o-transition: .7s ease-in-out;
  transition: .7s ease-in-out;
  cursor: pointer;
}

#menu span {
  display: block;
  position: absolute;
  height: 10px;
  width: 100%;
  background: green;
  border-radius: 9px;
  opacity: 1;
  left: 0;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
  -webkit-transition: .3s ease-in-out;
  -moz-transition: .3s ease-in-out;
  -o-transition: .3s ease-in-out;
  transition: .3s ease-in-out;
}

#menu span:nth-child(1) {
  top: 0px;
}

#menu span:nth-child(2),
#menu span:nth-child(3) {
  top: 20px;
}

#menu span:nth-child(4) {
  top: 40px;
}

#menu.open span:nth-child(1) {
  top: 20px;
  width: 0%;
  left: 50%;
}

#menu.open span:nth-child(2) {
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}

#menu.open span:nth-child(3) {
  -webkit-transform: rotate(-45deg);
  -moz-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  transform: rotate(-45deg);
}

#menu.open span:nth-child(4) {
  top: 20px;
  width: 0%;
  left: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="menu">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
</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

Jstree: Whenever I refresh my tree, the checkboxes do not reset to default and remain unchecked

After attempting to implement the following code: $("#jstree_demo_div").jstree("destroy").empty(); Although it successfully removes the checked nodes and reloads the tree, it fails to apply the new changes. Any advice on how to resolve this issue would b ...

Ways to align two columns at the center using CSS

I'm currently facing some challenges centering two columns on my website. Despite my efforts to make adjustments, the result always ends up with the columns positioned to the left (you can view the image). Could you please help me identify what I migh ...

Utilizing Tailwind CSS for Advanced DIV Placement

My goal is to arrange the DIVs inside the container in a way that the top DIV (containing text) overlaps the DIV next to it. Below is a sketch of what I have in mind: https://i.sstatic.net/kUVGT.png This is the HTML code I have written for this layout (Ta ...

Creating JOIN tables within the create action involves assigning related ids to each table

I am currently working on a room reservation system that involves including options for each room. Data related to the options and their join table, reservation_options, are successfully inserted into the params. However, I am facing an issue with assignin ...

Creating hyperlinks in HTML using a similar functionality to a LinkButton within a Repeater control in ASP.NET

I have a project where a list of questions is pulled from a database. These questions are asked to people who then vote on them. When I select a question, I want to display the votes on a chart along with the question itself. I don't want to use a rep ...

jQuery: Managing and modifying values for dynamically generated input elements

Hello, I am currently working with the latest version of jQuery and have a table set up where clicking on the "Add" button appends a row of inputs. My goal is to calculate the total price for each particular product based on keyup events (i.e., qty * price ...

Updating TimePicker Theme with JQuery Dynamically

Currently, I have integrated the jQuery Timepicker into one of my web pages. Now, for another page, I would like to use the same TimePicker but with a different theme, specifically 'smoothness`. Could you please guide me on how to achieve this? ...

Styling texts on Internet Explorer is possible with text-decoration

After completing the coding for my website, I encountered some issues on specific web browsers like Internet Explorer and Microsoft Edge. Despite conducting searches, I have been unable to find a solution. Here is an excerpt from the CSS code I am currentl ...

Enhance your horizontal stacked bar chart in chart.js by incorporating inner labels with the help of the 'chartjs-plugin-labels' plugin

Currently, I have integrated a package that allows me to incorporate labels into my charts - import * as pluginDataLabels from "chartjs-plugin-labels"; While it works perfectly fine with line charts, pie charts, and normal bar charts, I am facin ...

Combining two strings to form a single variable using PHP

Currently, I have a registration form set up for users to register successfully into my database. However, I am looking to enhance this functionality by restricting registration to only users with specific domain addresses. After some research, I found tha ...

Modify the color of a designated section of an image with a click of the mouse

As a novice following a tutorial, I am learning how to modify the color of a specific area within an image using various color options. I can successfully change one area, but I am struggling with implementing changes for other areas. My goal is to click ...

What is the best way to loop through a function using a for loop?

I need to transfer the 'y' variable to okayid in order for everything to work smoothly. However, I am encountering an issue with the looping process. The loop functions properly when using the first call of 'y' on okay.item(y), but it f ...

Error in Firefox when converting a string to a date in JavaScript using the format mm-dd-yyyy

Hi, I am encountering an issue with converting a string in the format mm-dd-yyyy into a date object. While it works perfectly fine in Internet Explorer and Chrome, it does not work in Firefox as it returns an invalid date at times. I have also tried using ...

Gather information from "div" tag with the class attribute using Python

I am looking to extract specific financial data (referred to as the Key data) that is housed within <div> </div> tags. I then aim to consolidate this extracted information into an excel sheet which already contains additional harvested data. Th ...

Is there a way to determine the bounding rectangle of a specific word within a paragraph when you only have its index?

I am currently developing a text-to-speech functionality for a react application that can emphasize the word being spoken by highlighting it with a background color. This feature closely resembles the layout of the Firefox reader view. However, my curren ...

Tips for creating an editable div that can also incorporate a textarea and the option to upload or delete photos

On my website, users can upload images, names, and descriptions which are saved in a MySQL database. The information is then fetched to display on the website. The code below allows users to enter this information and see it displayed when they click the s ...

Is there a way for me to update the placeholder text in my script from "CHANGE ME

Can anyone help me troubleshoot this code that's not working on my computer? I have a paragraph with the text CHANGE ME inside an element with the id "warning". I want to update this text when the login button is clicked, but it doesn't seem to ...

Display Navigation upon clicking a designated button

Seeking a way to automatically navigate to the second tab after submitting content in the first tab using nav-link. <ul class="nav nav-tabs" id="myTab" role="tablist"> <li class="nav-item"> <a class="nav-link active" id="home-tab" dat ...

What advantages and disadvantages come with using timeouts versus using countdowns in conjunction with an asynchronous content loading call in JavaScript/jQuery?

It seems to me that I may be overcomplicating things with the recursive approach. Wait for 2 seconds before loading the first set of modules: function loadFirstSet() { $('[data-content]:not(.loaded)').each( function() { $(this).load($(thi ...

Switching visual representation that appears upon clicking the dropdown menu

Issue with Duplicating Dropdown and Image Change const image = document.querySelector('.item__img'); const checkbox = document.querySelectorAll('.imgOption'); function handleChange() { let imgsrc = this.getAttribute("data-value ...