简体   繁体   English

JavaScript样式。鼠标悬停时姐姐div的高度

[英]JavaScript style.height of sister div on mouseover

I'm trying to simulate the look of an audio visualizer so that on mouseover, "this" div becomes the tallest div, while the sister divs shift heights as well. 我正在尝试模拟音频可视化器的外观,以便在鼠标悬停时,“ div”成为最高的div,而姐姐的div也会改变高度。 How do I specify which height the sister divs should change to? 如何指定姊妹div应该更改为哪个高度?

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> 

Thanks for your help! 谢谢你的帮助!

Here's a fiddle that should get you pointed in the right direction: https://jsfiddle.net/8p2yvro9/7/ 这是一个小提琴,应该让您指出正确的方向: https : //jsfiddle.net/8p2yvro9/7/

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%';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM