i’m trying to build a row of divs where a div will expand when a mouse hovers over it.
It got that so far but i want that the expanding div is not moving the other divs around (changing there size) but to expand over his neigbours, partly covering them.
i hope you get what i mean..
i also would like to keep the js part because there should happen more stuff when hovering over the elements.
here is my code so far:
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8" />
<style>
.wrap{
font-size: 160px;
font-weight: bold;
height: 450px;
width: 1100px;
display: flex;
flex-direction: row;
align-items: center;
border-style: solid;
border-width: 1px;
}
.a{
width: 220px;
height: 350px;
border-style: solid;
border-width: 1px;
}
.b{
width: 220px;
height: 350px;
border-style: solid;
border-width: 1px;
}
.c{
width: 220px;
height: 350px;
border-style: solid;
border-width: 1px;
}
.d{
width: 220px;
height: 350px;
border-style: solid;
border-width: 1px;
}
.e{
width: 220px;
height: 350px;
border-style: solid;
border-width: 1px;
}
</style>
<script type="application/javascript">
function enlarge(id){
/*console.log("enlarge: " + id);*/
var element = document.getElementById(id);
element.style.height = "440px";
element.style.width = "260px";
element.style.borderWidth = "1px";
element.style.borderStyle = "solid";
}
function reduce(id){
/*console.log("reduce: " + id);*/
var element = document.getElementById(id);
element.style.height = "350px";
element.style.width = "220px";
element.style.borderWidth = "1px";
element.style.borderStyle = "solid";
}
</script>
</head>
<body>
<center>
<div class="wrap">
<div class="a" id="a" onmouseover="enlarge(this.id)" onmouseout="reduce(this.id)">A</div>
<div class="b" id="b" onmouseover="enlarge(this.id)" onmouseout="reduce(this.id)">B</div>
<div class="c" id="c" onmouseover="enlarge(this.id)" onmouseout="reduce(this.id)">C</div>
<div class="d" id="d" onmouseover="enlarge(this.id)" onmouseout="reduce(this.id)">D</div>
<div class="e" id="e" onmouseover="enlarge(this.id)" onmouseout="reduce(this.id)">E</div>
</div>
</center>
</body>
</html>
also on jsfiddle:
https://jsfiddle.net/fkdvptuz/
2
Answers
Simply add a wrapper
div
that stores the real size for each, set its position torelative
then set the position of each expandingdiv
toabsolute
.Something like this:
html
Also if you only want to increase the size of the element while hovering, you use
transform:scale(1.5)
CSS properties. You do not need to have a long CSS hassle code.Hope this helps 🙂
To achieve behaviour you desire, you need to change particular div’s size using CSS’s property
translate: scale()
instead of changing width and height directly, you can still keeponmouseover
andonmouseout
event handlers for other logic, but scaling will be done using CSS.Here is the link to jsfiddle, I just have added
block
class to every div: