I'm not trying to compare CSS3 features over Javascript,I'm trying to know how to decide when stuck on a situation like this. I know three ways to achieve a single interactive effect, in this example, When a user hover on a div
, the inner element will expand it's width and height.
In this case scenario, I wanted to know which approach would be ideal. Things to consider, I will be targeting modern browser, I'm supporting mobile devices (iPad and iPhone), I will be using jQuery and Boostrap.
1. Using jQuery, it can be coded to be something like this:
$('#js').hover(function(){
$('.inner',this).animate({'height':'200px', 'width':'200px'})
},function(){
$('.inner',this).animate({'height':'0px', 'width':'0px'})
})
2. Using CSS, It will look something like this:
#css .inner {
-webkit-transition: width .3s, height .3s;
transition: width .3s, height .3s;
}
#css:hover .inner {
width: 200px;
height: 200px;
}
3. And lastly, using both JS and CSS
CSS
#css.hover .inner {
-webkit-transition: width .3s, height .3s;
transition: width .3s, height .3s;
}
#css.hover .inner {
width: 200px;
height: 200px;
}
JS
$('#css_js').hover(function(){
$(this).addClass('hover');
},function(){
$(this).removeClass('hover');
})
My fiddle here: http://jsfiddle.net/pk0cf25u/