Adjust width when hovering or moving the mouse in and out

Apologies for the lack of a creative title...

I'm sharing the following code snippet:

.wrapper {
  display: flex;
  border: 1px solid red;
  padding: 10px;
}
.groups {
  display: flex;
  border: 2px solid blue;
  width: 70%;
}
.leftColumn {
  width: 30%;
  background-color: lightgrey;
}
.group1 {
  width: 85%;
  background-color: lightblue;
}
.group2 {
  width: 15%;
  background-color: lightyellow
}
.group2:hover {
  cursor: pointer;
}
<div class="wrapper">
  <div class="leftColumn">Left</div>
  <div class="groups">
    <div class="group1">Group 1</div>
    <div class="group2" ng-click="toggleColumn()">Group 2</div>
  </div>
</div>

In the above snippet, there is a wrapper (with a red border) that contains three columns. The first column (light grey) has a fixed width of 30%. Next, you'll see another wrapper (with a blue border) containing two columns (light blue and light yellow). This wrapper also has a fixed width of 70% relative to its parent. The columns within have widths of 85% (light blue) and 15% (light yellow). I would like to dynamically expand/close the light yellow column when it is clicked. Specifically, when expanded, both columns should take up 50% of the wrapper's space. When closed, they should revert to their original widths of 85% and 15%. Any suggestions on how to achieve this dynamically will be greatly appreciated!

!!! EDIT: MY OWN SOLUTION WITH ANGULARJS/NG-CLASS !!!

Description: After some experimentation, I've come up with a solution using angularjs and its ng-class feature. By setting a boolean variable to false initially, I can check if the column is expanded or not. I then created a new CSS class with a width of 50%, which I can assign to the two columns that need to expand. When the column is expanded and the boolean is true, the columns adjust to the 50% width correctly. On closing the column, the class is removed, and the default widths are restored. This approach is simple yet effective. Here's the implementation:

angular.module("myApp", []).controller("myController", function($scope) {
  $scope.isExpanded = false;
});
.wrapper {
  display: flex;
  border: 1px solid red;
  padding: 10px;
}
.groups {
  display: flex;
  border: 2px solid blue;
  width: 70%;
}
.leftColumn {
  width: 30%;
  background-color: lightgrey;
}
.group1 {
  width: 85%;
  background-color: lightblue;
}
.group2 {
  width: 15%;
  background-color: lightyellow
}
.group2:hover {
  cursor: pointer;
}
.cExpandedWidth {
  width: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div class="wrapper" ng-app="myApp" ng-controller="myController">
  <div class="leftColumn">Left</div>
  <div class="groups">
    <div class="group1" ng-class="{cExpandedWidth: isExpanded}">Group 1</div>
    <div class="group2" ng-class="{cExpandedWidth: isExpanded}" ng-click="isExpanded = !isExpanded">Group 2</div>
  </div>
</div>

Answer №1

If you're looking for something similar to this...

Here's the explanation: When .group2 is clicked, a class of expanded is added. On the second click, that class is removed using toggleClass. The same process applies to .group1, but with the class shrink.

With CSS, I set the width of group2 with the class expanded to 50%, and its sibling .group1.shrink gets its width reduced to 50% as well.

I also included some transitions for smoother operation.

Please let me know if this meets your requirements.

$('.group2').click(function(){
     $(this).toggleClass("expanded")
     $(this).siblings(".group1").toggleClass("shrink")
})
.wrapper {
  display: flex;
  border: 1px solid red;
  padding: 10px;
}
.groups {
  display: flex;
  border: 2px solid blue;
  width: 70%;
}
.leftColumn {
  width: 30%;
  background-color: lightgrey;
}
.group1 {
  width: 85%;
  background-color: lightblue;
  transition:0.5s;
}
.group2 {
  width: 15%;
  background-color: lightyellow;
   transition:0.5s;
}
.group2:hover {
  cursor: pointer;
}
.group2.expanded{
  width:50%;
}
.group1.shrink {
  width:50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="leftColumn">Left</div>
  <div class="groups">
    <div class="group1">Group 1</div>
    <div class="group2">Group 2</div>
  </div>
</div>

Answer №2

$(document).ready(function()
{
count=0;
$('.group2').click(function()
{
if(count==0)
{
$(this).css("width","50%");
$('.group1').css("width","50%");
count++;
}
else
{
$(this).css("width","15%");
$('.group1').css("width","85%");
count=0;
}
});

});
.wrapper {
  display: flex;
  border: 1px solid red;
  padding: 10px;
}
.groups {
  display: flex;
  border: 2px solid blue;
  width: 70%;
}
.leftColumn {
  width: 30%;
  background-color: lightgrey;
}
.group1 {
  width: 85%;
  background-color: lightblue;
  transition-duration:1s;
}
.group2 {
  width: 15%;
  background-color: lightyellow;
  transition-duration:1s;
}
.group2:hover {
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="leftColumn">Left</div>
  <div class="groups">
    <div class="group1">Group 1</div>
    <div class="group2" ng-click="toggleColumn()">Group 2</div>
  </div>
</div>

This code uses jQuery and handles the click event.

I have implemented a transition duration in both group1 and group2 classes to create animations.

I hope this explanation is helpful.

Answer №3

One way to modify CSS using Jquery is by utilizing the :hover technique. For a demonstration, check out this example here

JQuery/JS

$( ".group2" ).hover(function() {
    $(".group2").css("width", "50%");
    $(".group1").css("width", "50%")
});

$( ".group2" ).mouseleave(function() {
    $(".group2").css("width", "15%");
    $(".group1").css("width", "85%");
});

You can also revert back the changes on mouse off. To add smooth animations, consider using the animate: property in CSS.

Answer №4

If I understand your query correctly, it seems like you are looking to toggle the visibility of the blue border groups by showing or hiding the leftColumn div. You could enhance this further by incorporating animations to slide the divs and enhance the overall user experience.

Feel free to review the code snippet provided below and let me know if you have any questions or need further clarification.

$('.group2').click(function(){
  var elem = $('.leftColumn');
  if(elem.is(':visible')) {
    elem.hide();
    $('.groups').css('width', '100%');
    $('.group1').css('width', '50%');
    $('.group2').css('width', '50%');
  } else {
    elem.show();
    $('.groups').css('width', '70%');
    $('.group1').css('width', '85%');
    $('.group2').css('width', '15%');
  }
});
.wrapper {
  display: flex;
  border: 1px solid red;
  padding: 10px;
}
.groups {
  display: flex;
  border: 2px solid blue;
  width: 70%;
}
.leftColumn {
  width: 30%;
  background-color: lightgrey;
}
.group1 {
  width: 85%;
  background-color: lightblue;
}
.group2 {
  width: 15%;
  background-color: lightyellow
}
.group2:hover {
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="leftColumn">Left</div>
  <div class="groups">
    <div class="group1">Group 1</div>
    <div class="group2" ng-click="toggleColumn()">Group 2</div>
  </div>
</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

Issue encountered while compiling CSS for a map with @each function in Sass

I am currently in the process of developing a set of classes to define image styles based on specific skills. Colors associated with each skill are stored in a map. $skills-color: ( default : #D8D8D8, 6 : #2E80EC, 5 : # ...

unique map customized in a separate browser window

Attempting to complete the code provided here but encountering issues as a novice in java-scripting. It seems like the map is not loading correctly. A new tab and text are generated when DIV is created. Seeking assistance on how to open a custom map in a ...

Navigating the World of CSS Selectors - Balancing Style Control Without Excessive Class Usage

I recently stumbled upon a responsive menu that I wanted to incorporate into my webpage design. However, as I delved deeper into the implementation process, I realized that the CSS code associated with the menu was targeting all ul, li, a elements on the e ...

The limitations of String.replace() when it comes to replacing newlines (or any other characters)

I'm currently facing an issue with implementing an "editable" block of text that seems to lose line breaks when edited. My setup involves two elements: a div and a textarea. When the user clicks on Edit, I use the following code to fill the textarea: ...

Converting a jQuery DOM element into a string representation

Currently, I am working with a textarea that contains the contents of an HTML file. This textarea includes all elements of my HTML page such as doctype, head, html, etc. My goal is to save the content of the textarea into a DOM variable using $.parseHTML: ...

Attempting to design a customized tooltip for an SVG element embedded within the HTML code

Recently, I've delved into Js with the goal of creating an interactive pronunciation guide using inline svg. If you're curious to see what I've done so far, check it out here. My current focus is on incorporating basic styled tooltips that ...

My XSLT document is not converting to valid HTML code

As a beginner, I've recently taken up XML and XSLT. A task was assigned to me to create an XSLT file based on the XML snippet provided below. <?xml version="1.0" encoding="UTF-8"?> <event> <title>Test 1</title> <descript ...

What is the procedure for retrieving an object's property?

In my coding project, I recently created a function called u() that is designed to transfer data from servers to the external environment. This function allows me to perform tasks outside of the success:function(). However, I encountered an issue while t ...

Copying elements from ng-repeat scope to outside

I am brand new to AngularJS and I've been trying to figure out a solution for quite some time now without any luck. Basically, I have a form where users can add new fields. These fields are then displayed in a list at the bottom using ng-repeat. When ...

Having trouble customizing the active state of a react router navlink using css modules in React?

Objective I am attempting to add styles to the active route in a sidebar using css modules while still maintaining the base styles by assigning 2 classes. This is the code I have tried: <NavLink to={path} className={` ${classes.nav_ ...

Is jQuery automatically loaded by WordPress?

jQuery(document).ready(function($){ alert("Hi"); }); I am looking to incorporate the jQuery script above into my WordPress site within the body section. My main query revolves around whether or not WordPress automatically loads the jQuery library, or i ...

The ajax-getJSON example ran without any issues on Microsoft Edge, however, it encountered problems when using Internet Explorer or Google Chrome on my Windows

When I came across this ajax-getJSON sample on a website, it worked perfectly on Internet Explorer. However, when I attempted to run the sample code locally on my workstation (win10), it only functioned on Microsoft Edge. Unfortunately, I encountered an er ...

Differentiating the appearance of an HTML datalist with color alterations

Is there a way to customize the color of the datalist element (black box shown in the image)? https://i.stack.imgur.com/GGJQ3.png https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_datalist EDIT: The default color is set by the system, although ...

Issue with decreasing the opacity in Sublime Text 2

I encountered an issue while compiling my less file with ST2. I have configured the following plugins: LESS LESS build SublimeOnSaveBuild less2css The code I am attempting to compile is as follows: .generate-columns(4); .generate-columns(@n, @i: 1) whe ...

Troubleshooting the Compatibility Issue of Typeahead.js and Bootstrap 3 in XPages

I've been attempting to implement typeahead.js functionality with bootstrap 3 in an Xpage. Within the database (webContent), I have added the Bootstrap 3 files, jQuery 2.4.3, and typeahead.js as resources. I've also included an input field/Editb ...

Creating column gaps that remain consistent when resizing a webpage is essential for maintaining a clean and organized layout

Visit my current site [This is the HTML code for my website:][2] <div class="column-box" style="width: 100%"></div> <div id="column1" style="float:left; margin:15; width:33%;"> <p>Sara Adams<br> I am a Web Developer and C ...

Struggling with adjusting the image dimensions on my HTML and CSS page

<div class="profile"> <img src="image/JaeHeadShot.jpg" alt="Head Shot"/> <h1> Jae Hong </h1> </div> .profile{ border: 2px solid rgba(0,0,0,0.3); text-align:center; padding: 30px; } img.profile{ width:423; height ...

Retrieving information from database with the help of PHP and AJAX

I am having trouble printing data from a PHP and Ajax request that is initially encoded into JSON. Instead of displaying the data properly on the screen, it is showing elements from four rows all in a single line with commas separating them. $(document ...

Utilizing the require function to implement an AngularJS Controller without having

I've been working with requireJS in my application. Every time I try to register a controller on my module, it gives me an error saying that the controller is not defined. Here's the code for my LoginController located in login.controller.js: f ...

Best practices for managing several models within the Ionic framework using PouchDB?

Hey there! I've been diving into PouchDB lately and have a question about handling multiple models. I've noticed that most examples focus on a single model, like the ones found in this tutorial and various to-do app demos where they use db.allDo ...