This is a simple CSS transition.
div.simple_transition {
width: 100px;
height: 100px;
background: red;
-webkit-transition: width 5s;
/* For Safari 3.1 to 6.0 */
transition: width 5s;
&:hover {
width: 100%;
}
}
<h1>Simple Transition</h1>
<p>NoJS Animation starts when hovering the element</p>
<div class="simple_transition"></div>
<p>
<b>Note:</b> This example does not work in Internet Explorer 9 and earlier versions.</p>
So I want it to start the transition programmatically for example right after the browser loads the page without using the mouse hover css selector. I know that I can do this with javascript and I managed to do this with a simple code.
//Based on https://www.w3schools.com/css/tryit.asp?filename=trycss3_transition1
$(document).ready(function() {
var elm = $('div.simple_transition');
elm.attr('class', 'should-be-likethis');
console.log('working on it');
});
div.simple_transition {
width: 100px;
height: 100px;
background: red;
-webkit-transition: width 5s;
/* For Safari 3.1 to 6.0 */
transition: width 5s;
&:hover {
width: 100%;
}
}
.should-be-likethis {
-webkit-transition: width 5s;
/* For Safari 3.1 to 6.0 */
transition: width 5s;
width: 100%;
height: 100px;
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Simple Transition</h1>
<p id='text'>With jQuery animation starts without hovering the element</p>
<div class="simple_transition"></div>
<p><b>Note:</b> This example does not work in Internet Explorer 9 and earlier versions.</p>
Can I do this without using javascript only with CSS? In fact in my current project I am already using javascript (ajax) for handling some data-related queries and I am also using a sass compiler so I would prefer that to write everything that is related to the styles in a CSS.