Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want a Smooth Animation When Resizing DIV - JavaScript

When I am hovering over a DIV, it increases in both height and width but I have no idea how to make it animate smoother instead of resize all at once

function chg()
{
  document.getElementById("d1").innerHTML="Great Job!";
  document.getElementById("d1").style.width="300px";
  document.getElementById("d1").style.height="200px";
}

function chg2()
{
  document.getElementById("d1").innerHTML="Hover Over Me!";
  document.getElementById("d1").style.width="230px";
  document.getElementById("d1").style.height="160px";
}
div
{
  background-color:RGB(177,0,0);
  color:white;
  font-size:large;
  height:160px;
  width:230px;
  text-align:center;
  line-height:150px;
}
<!DOCTYPE html>
<html>
<body>
<div onmouseover="chg()" onmouseout="chg2()" id="d1">Hover Over Me!</div>
</body>
</html>
like image 604
hurtbox Avatar asked Oct 02 '15 20:10

hurtbox


People also ask

Is it better to animate with CSS or JavaScript?

TL;DR # Use CSS animations for simpler "one-shot" transitions, like toggling UI element states. Use JavaScript animations when you want to have advanced effects like bouncing, stop, pause, rewind, or slow down.

Is CSS animation faster than JavaScript?

CSS has fairly good performance as it offloads animation logic onto the browser itself. This lets the browser optimize DOM interaction and memory consumption and most importantly, uses the GPU to improve performance. On the other hand, Javascript performance can range from reasonably faster to much slower than CSS.

How do you animate in JavaScript?

To make an animation possible, the animated element must be animated relative to a "parent container". The container element should be created with style = "position: relative". The animation element should be created with style = "position: absolute".


3 Answers

Explanation

You should check out the transition property in CSS3.

Resources

Check the following links for more information:

  • CSS3 Transitions
  • Using CSS transitions

Example

function chg() {
  document.getElementById("d1").innerHTML = "Great Job!";
  document.getElementById("d1").style.width = "300px";
  document.getElementById("d1").style.height = "200px";
}

function chg2() {
  document.getElementById("d1").innerHTML = "Hover Over Me!";
  document.getElementById("d1").style.width = "230px";
  document.getElementById("d1").style.height = "160px";
}
div {
  background-color: RGB(177, 0, 0);
  color: white;
  font-size: large;
  height: 160px;
  width: 230px;
  text-align: center;
  line-height: 150px;
  transition: all .5s linear;
}
<div onmouseover="chg()" onmouseout="chg2()" id="d1">Hover Over Me!</div>
like image 108
Romulo Avatar answered Oct 18 '22 02:10

Romulo


The best way to accomplish this would be to do a CSS3 transition when the width changes.

Such as the following:

div
{
    transition:width 1s ease-in-out;
}
like image 30
Jake Chasan Avatar answered Oct 18 '22 02:10

Jake Chasan


with CSS3 you don't need javascript to create that effect..

div {
    background-color:RGB(177,0,0);
    color:white;
    font-size:large;
    height:160px;
    width:230px;
    text-align:center;
    line-height:150px;
    transition:all 1s;
}
div:hover {
    height:200px;
    width:300px;
}
like image 3
Dedi Suhaidi Avatar answered Oct 18 '22 02:10

Dedi Suhaidi