Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 Transitions: is there an on click option without using JQuery?

This works great:

<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>

But does anyone know of a way to do this using click instead of hover? And not involving JQuery?

Thanks!

like image 619
MeltingDog Avatar asked Jun 23 '12 03:06

MeltingDog


People also ask

How do you transition on button clicks?

Use JavaScript. Using JavaScript, you can switch the element's class on-click, which will allow you to change the appearance of the element (including any transitions you have set on it). Something like: $('. selector').

How do you add transition effects in CSS3?

To make the transition occur, you must specify at least two things — the name of the CSS property to which you want to apply the transition effect using the transition-property CSS property, and the duration of the transition effect (greater than 0) using the transition-duration CSS property.

How do transitions work in CSS?

CSS Transitions allow property changes in CSS values to occur smoothly over a specified duration. This smoothing animates the changing of a CSS value when triggered by a mouse click, focus or active state, or any changes to the element (including even a change on the element's class attribute).


1 Answers

You can write like this:

CSS

input{display:none}
.ani
    {
    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 */
    display:block;
    }
input:checked + .ani{width:300px;}

HTML

<input type="checkbox" id="button">
<label class="ani" for="button"></label>

Check this http://jsfiddle.net/nMNJE/

like image 155
sandeep Avatar answered Oct 23 '22 16:10

sandeep