jquery - Triggering an animation on a div by clicking a button using addClass/removeClass with Javascript -
so i'm trying trigger animation clicking on button using addclass , removeclass javascript.
i'm not bad @ html/css strating learn javascript editing snipets.
so here's mine can tell me why div won't rotate when click black button ?
thanks in advance !
<button class="menu-circle"></button> <img class='imgblock' src="http://lorempixel.com/400/200/" alt="" /> .menu-circle { height: 100px; width: 100px; background-color: #000000; border-radius: 50%; position: absolute; top: 50%; left: 50%; transform: translatex(-50%) translatey(-50%); transition: .1s; z-index: 100; border: none; } .menu-circle:hover { height: 115px; width: 115px; } .menu-circle:active { height: 100px; width: 100px; } .imgblock { display: block; margin: 20px; -webkit-transition-duration: 1s; -moz-transition-duration: 1s; -o-transition-duration: 1s; transition-duration: 1s; } .rotate { -webkit-transform: rotate(45deg); -moz-transform: rotate(45deg); -o-transform: rotate(45deg); transform: rotate(45deg); } $('.menu-circle').on('click', function(){ $('img .imgblock').addclass('rotate'); $('img .imgblock .rotate').removeclass('rotate'); });
working fiddle :
you have several problems fiddle:
- wrong class names: in js
$('.menucircle')
, in html -class="menu-circle"
- you don't need space between img , class in jquery selector
$('img .imgblock')
- space means you're looking.imgblock
class insideimg
tag (which impossible). .current
class not defined nor in html, nor in css, appear in js
here's fiddle fixed problems , works: demo
js:
$('.menu-circle').on('click', function(){ var $img = $('.crossrotate'); if (!$img.hasclass('rotate')) { $img.addclass('rotate'); } else { $img.removeclass('rotate'); } });
Comments
Post a Comment