Skip to content Skip to sidebar Skip to footer

How To Change Css Background-image On Click?

I'd like to change the css 'background-image:' when someone clicks a button. I'm not sure if I'm able to change it through css or if I would need to incorporate java script. Also,

Solution 1:

I'd approach it like 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, you can do anything you need. There are also a variety of other states you 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 are css only alternatives, but they're not really great on support: http://tympanus.net/codrops/2012/12/17/css-click-events/


Solution 2:

You could use javascript for change the background. The following website javascripter is an example of changing background color and manipulating CSS by Javascript. I hope this can help you.


Solution 3:

1. CSS pseudo-class selector:active

If you didn't care about persistence you could always use the the pseudo-class ":active". The image will only be affected as long as your mouse is down. As soon as you mouse-up it'll revert. At this moment, that's about as close as you can get in CSS.

.hello-button:active {
    background-image: url("image.jpg");
}

JSFiddle Example: http://jsfiddle.net/pkrWV/

2. Change Style Attribute with JavaScript

JavaScript is just about the only way you're going to be able to click on an object, mouse-up and the background is still changed. JavaScript gives you a couple ways to do it too.

You can use JavaScript to change the object's style attribute to update the 'background-image'.

obj.style.backgroundImage = 'url("image.jpg")';

JSFiddle: http://jsfiddle.net/pkrWV/1/

3. Change Class Attribute with JavaScript

Or similarly, you could create two classes in your CSS, and use JavaScript to update the object's class attribute.

/* JavaScript */
obj.className = 'imageOneClassName';

/* CSS */
.imageOneClassName {
    background-image: url("image.jpg");
}

JSFiddle: http://jsfiddle.net/pkrWV/2/

My personal favorite method is the third option where you still use CSS to style your obj in different states, and then you use JavaScript to change the class name to update those states. It's less JavaScript, more CSS, and you're keeping everything in their appropriate places.


Solution 4:

$(function() {
  $('.home').click(function() {
    $(this).css('background-image', 'url(images/hello.png)');
  });
}):

you have to do like this, there was a relative question see this i hope i helped you...

jquery onclick change css background image


Solution 5:

There's no way to do this in pure HTML/CSS, but in javascript you can do it like so:

var button = document.getElementsByClassName("hello-button")[0];
button.addEventListener("click", function(){
    button.style.backgroundImage = "url(bye.png)";
});

You can either include this in a <script></script> tag or add it to a .js file and include that by adding <script src="scriptName.js"></script>


Post a Comment for "How To Change Css Background-image On Click?"