2

i have this Webpage on which with mouse over, the element rotates and goes back to original position on mouseout.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css"> 
div
{
width:100px;
height:100px;
background:red;
transition:width 2s;
-moz-transition:width 2s; /* Firefox 4 */
-webkit-transition:width 2s; /* Safari and Chrome */
-o-transition:width 2s; /* Opera */
}

div:hover
{
width:300px;
}
</style>
</head>
<body>

<p><b>Note:</b> This example does not work in Internet Explorer.</p>

<div></div>

<p>Hover over the div element above, to see the transition effect.</p>

</body>
</html>  

Now Is it possible to trigger this transition using JavaScript and not the defualt hover function. I tried by creating two different class names but it just dosent work even if i do it by adding delay and changing the class. Is there anyway to do such a transition with Javascript??

5 Answers 5

3

I believe all you need to do is fire a change the style of the element you want to modify when particular events happen. Also, I changed the DOCTYPE to use HTML5. Give this a try:

<!DOCTYPE HTML>
<html>
<head>
<style type="text/css"> 
div.sample1
{
width:100px;
height:100px;
background:red;
transition:width 2s;
-moz-transition:width 2s; /* Firefox 4 */
-webkit-transition:width 2s; /* Safari and Chrome */
-o-transition:width 2s; /* Opera */
}

div.sample1:hover
{
width:300px;
}


</style>
<script type="text/javascript">
function doStuff(obj,boolStateChange)
{
    console.log('test');
    if (boolStateChange==true)
    {
    obj.style.cssText = "width:300px;height:100px;background:green;transition:width 2s;-moz-transition:width 2s;-webkit-transition:width 2s;-o-transition:width 2s;";

    }
    else
    {
    obj.style.cssText = "width:100px;height:100px;background:green;transition:width 2s;-moz-transition:width 2s;-webkit-transition:width 2s;-o-transition:width 2s;";
    }

}
</script>
</head>
<body>

<p><b>Note:</b> This example does not work in Internet Explorer.</p>

<div class="sample1"></div>

<p>Hover over the div element above, to see the transition effect.</p>

<br/><br/><br/>

<div id="javaHover" style="background-color:green;width:100px;height:100px;" onMouseOver="doStuff(this,true);" onMouseOut="doStuff(this,false);"></div>

<p>Hover over the div element above, to see the Javascript transition effect.</p>

</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

you are a champion. Thank you a bazillion times, thank you so so much!!
1

You can do this:

document.getElementsByTagName("div")[0].style.width = '300px';

See fiddle.

Comments

1

what do you think about this: http://jsfiddle.net/scrRe/ ?

Here you go: http://jsfiddle.net/scrRe/3/

1 Comment

yes but is there anyway to avoid jQuery, Im sure it must be simple. I may be missing something. I just need to change the CSS from hover and call it from some javascript function but what is it exactly what i am missing??
1

Do not use :hover selector then. Use JavaScript events.

for example:

CSS:

#box
{
  background-color: steelblue;
  height: 100px;
  width: 100px;
  transition: height 1s, width 1s;
  -moz-transition: height 1s, width 1s;
  -webkit-transition: height 1s, width 1s; 
  -o-transition: height 1s, width 1s;
}

JavaScript:

 var element = document . getElementById ( "box" ) ;


 element . addEventListener
 (
     "click",
     function ()
     {
         this . style . width = "200px" ;
     }
 ) ;

 element . addEventListener
 (
    "dblclick",
     function ()
     {
         this . style . height = "200px" ;
     }
 ) ;

http://jsfiddle.net/6gSZD/

Comments

1

You may be able to use CSSAnimation, which is a JavaScript library that allows you to trigger animations with JavaScript while utilizing CSS to do the actual animation.

Here's an example from the documentation (try it):

var element = document.getElementById('some-el'),
    transition = new Transition(element),
    transform = new Transform(element);

transition.set({
  property: 'transform',
  'timing-function': 'ease-in',
  duration: '2s'
});

transform.rotate(720).scale(2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.