javascript - How to change css background-image on click? -


i'd change css "background-image:" when clicks button.

i'm not sure if i'm able change through css or if need incorporate java script. also, if need java script type of code need?

my current work around css , looks like:

.hello-button { background-image: url("hello.png"); background-repeat: no-repeat; background-attachment: inherit; background-position: center; -webkit-transition: 2s ease-out; -moz-transition: 2s ease-out; -o-transition: 2s ease-out; transition: 2s ease-out; }  .hello-button:hover { background-image: url("bye.png"); background-repeat: no-repeat; background-attachment: inherit; background-position: center; transition-delay: .7s; -webkit-transition-delay: .7s; -moz-transition-delay: .7s; -o-transition-delay: .7s; } 

i'd approach this. http://jsfiddle.net/darcher/6ex7h/

jquery

   $('.img').on({     click: function(){         $(this).addclass('new-bg').removeclass('bg') // changes background on click     },     mousedown: function() {         // :active state     },     mouseup: function() {         // on click release     },     mouseenter: function() {         // on hover     },     mouseleave: function() {         // hover exit     }     /*        , hover: function(){            // or hover instead of enter/leave         }     */ }) 

with these varying states, can need. there variety of other states can use http://api.jquery.com/category/events/mouse-events/

html

<div href="#" class="img bg"></div> 

css

.img{     background-repeat:no-repeat;     background-size:cover;     background-position:center;     display:block;     height:200px; } .bg{     background-image:url(http://placehold.it/300x200/white/black); } .new-bg{     background-image:url(http://placehold.it/300x200/black/white); } 

there css alternatives, they're not great on support: http://tympanus.net/codrops/2012/12/17/css-click-events/


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -