Adjust the height of a sibling div using JavaScript with mouseover event

I need assistance with creating an audio visualizer effect where on mouseover, "this" div becomes the tallest one and the other divs adjust their heights accordingly. How can I specify the specific height for each sister div to change to?

Code

window.onload = window.onscroll = function() {
  var bars = document.getElementsByClassName('bar');
  [].forEach.call(bars, function(bar) {
    bar.style.height = Math.random() * 50 + '%';
  });
}
.bars {
  position: fixed;
  top: 30px;
  right: 0;
  bottom: 40px;
  left: 0;
  margin: 10px auto;
  text-align: center
}

.bars::before {
  content: "";
  display: inline-block;
  height: 100%;
}

.bar {
  display: inline-block;
  vertical-align: bottom;
  width: 4rem;
  height: 25%;
  margin-right: .75em;
  background: #333;
  -webkit-transition: height 0.5s ease-out;
  transition: height 0.5s ease-out;
}
<div class="container">
  <div class="bars">
    <div class="bar" id="barOne"></div>
    <div class="bar" id="barTwo"></div>
    <div class="bar" id="barThree"></div>
    <div class="bar" id="barFour"></div>
    <div class="bar" id="barFive"></div>
  </div>
</div>

Thank you for any guidance you can provide!

Answer №1

Check out this fiddle that will steer you in the right direction: https://jsfiddle.net/8p2yvro9/7/

Grab your attention to this code snippet:

let container = document.getElementsByClassName('container')[0];
let bars = document.getElementsByClassName('bar');

[].forEach.call(bars, bar => {
  bar.addEventListener('mouseover', function() {
    shiftBars(bar);
  });
});

function shiftBars(barOver) {
  [].forEach.call(bars, function(bar) {
    bar.style.height = Math.random() * 50 + '%';
  });
  barOver.style.height = '100%';
}

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

Properly managing mouseover events on a flipped div: tips and tricks

I created a div that flips when clicked using some HTML and CSS code. It works perfectly in Firefox 39 and Chrome 43. Here is the markup: <div class="flip-wrapper flippable-wrapper" id="fliptest-01"> <div class="flip-wrapper flippable ...

"Unlocking the Power: Sending a Complex JSON Object to PHP from the Dojo

Trying to send a complex JSON object to the server using PHP, but encountering difficulties in sending it. Here is the code snippet: Snippet from DOJO: var ObjArry=[]; var test1 = {key:value, key:value, key:value, key:value}; var test2 = {key:value, ...

Unable to reach the margin-left properties of the elements

I am facing an issue in accessing the current margin-left CSS property of the class .circle in the code snippet below. A demonstration of this problem can be found on a website called PLUNKr. The reason I need to access this property is because I have to ...

Positioning a material UI dialog in the middle of the screen, taking into account variations in its height

Dealing with an MUI Dialog that has a dynamic height can be frustrating, especially when it starts to "jump around" the screen as it adjusts to fit the content filtered by the user. Take a look at this issue: https://i.stack.imgur.com/IndlU.gif An easy f ...

Steps for referencing an autogenerated id in a Firestore collection

I need assistance updating a 'progress' field in a document with an autogenerated ID (using Firestore) every time the progress button is clicked. https://i.stack.imgur.com/hZieN.png Despite my attempts, nothing seems to work. Here is the method ...

Ways to prevent Bootstrap column from aligning at the bottom of a row

<div class="row"> <div class="col-lg-10"> <div class="expandable-input-small" id="input_element" contenteditable="true" data-max-length="100"> ...

Counting each item with jQuery and assigning them numbers 02, 03, 04, etc., with the exception of the first item which will display as "Up Next

I'm still learning jQuery and here's the code I've put together after researching on stackoverflow and other platforms: var counter = 1; $('.next-page .nav-item').each(function () { if ($(this, ':gt(0)')) { $(this ...

What is the best way to handle a specific submit button using jQuery/Ajax?

I created a web page with 4 submit buttons that all call the same PHP page process.php using jQuery/Ajax. I trigger this PHP page using the onClick event as shown below: <div class="col-md-12"> <input type="hidden" name="_token" value="<?p ...

Implementing AJAX functionality to dynamically show a message on a webpage following an HTTP POST request

After submitting the form, I want to remain on the current page and show a message indicating whether the submission was successful or not. The HTTP POST request is processed like this: app.post('/insertdb', function(request, response) { // in ...

Proper techniques for storing element count in Protractor

I am a beginner when it comes to using Protractor and I am not entirely satisfied with the code snippet below, even though it is functional. My goal is to store the count in a variable (named "nb_before") and use it later on. However, due to the promi ...

Tips for styling my date using MomentJS

I am currently using moment.js for displaying dates in my application. The date format is determined based on the locale stored in the variable clientLanguage. I have tried various approaches, including the following: moment(myISODate).locale(clientLang ...

Clicking on various buttons will trigger the opening of different tabs within a single modal window

Imagine I have two distinct buttons, A and B. Within a single modal window, there exist two tabs labeled as a and b. My goal is to be able to click on A and have tab a displayed in the modal, while clicking on B should reveal tab b within that same modal. ...

Personalized HTML characteristics

Within the realm of AngularJS (and possibly jQuery UI), I've come across tags such as ui:some_random_name and ng:some_random_name. It seems that according to the HTML specification, non-standard attributes are not allowed. So how do these libraries m ...

When moving the child grid layout, it often results in unintentionally moving the parent grid layout along with

Currently, I am utilizing React Grid Layout from this repository: https://github.com/STRML/react-grid-layout. My task involves creating a draggable parent div along with a draggable child div. While the functionality works smoothly for the divisions indiv ...

Luxon DateTime TS Error: The 'DateTime' namespace cannot be used as a type in this context

I have encountered an issue while trying to set the type of a luxon 'DateTime' object in TypeScript. The error message DateTime: Cannot use namespace 'DateTime' as a type appears every time I attempt to assign DateTime as a type. Below ...

Use CSS to target elements with IDs that have been generated randomly

Unfamiliar with the specific IDs, this markup presents a challenge: #product-20625055 { background-color: #FC0; } #product-66980342 { background-color: #0CF; } #product-54722210 { background-color: #F0C; } <div class="product" id="product-20625055"&g ...

The setInterval() function is not functioning properly when used with PHP's file_get_contents

Currently, I'm encountering an issue while attempting to use the function get_file_contents() within a setInterval(). The objective is to continuously update some text that displays the contents of a file. Below is the code snippet: <script src="h ...

Is there a way to rigorously validate my HTML, CSS, and JavaScript files against specific standards?

Can modern browsers suppress errors in HTML, CSS, and JS sources? Is there a method to uncover all mistakes, no matter how small they may be? ...

tips for reducing the size of the recaptcha image to display just one word

The custom recaptcha theme I'm working with requires the image to be smaller in width but not in height. Is there a way to achieve this requested design? ...

What is the best way to apply focus to a list element using Javascript?

I recently created code to display a list of elements on my webpage. Additionally, I implemented JavaScript functionality to slice the elements. Initially, my page displays 5 elements and each time a user clicks on the "show more" link, an additional 5 ele ...