简体   繁体   English

如何在点击时更改css背景图片?

[英]How to change css background-image on click?

I'd like to change the css "background-image:" when someone clicks a button. 当有人点击按钮时,我想更改css“background-image:”。

I'm not sure if I'm able to change it through css or if I would need to incorporate java script. 我不确定我是否能够通过CSS更改它,或者我是否需要合并java脚本。 Also, if I need java script what type of code would I need? 另外,如果我需要java脚本,我需要什么类型的代码?

My current work around is with css and it looks like: 我目前的工作是使用CSS,它看起来像:

.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 it like this. 我会像这样接近它。 http://jsfiddle.net/darcher/6Ex7h/ http://jsfiddle.net/darcher/6Ex7h/

jquery 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/ 您还可以使用各种其他状态http://api.jquery.com/category/events/mouse-events/

html HTML

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

css 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/ 有css唯一的选择,但他们在支持方面并不是很好: http//tympanus.net/codrops/2012/12/17/css-click-events/

You could use javascript for change the background. 你可以用javascript来改变背景。 The following website javascripter is an example of changing background color and manipulating CSS by Javascript. 以下网站javascripter是一个通过Javascript更改背景颜色和操作CSS的示例。 I hope this can help you. 我希望这可以帮到你。

1. CSS pseudo-class selector:active 1. CSS伪类选择器:活动

If you didn't care about persistence you could always use the the pseudo-class ":active". 如果你不关心持久性,你总是可以使用伪类“: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. 在这一刻,这就像你可以在CSS中获得的那样接近。

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

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

2. Change Style Attribute with JavaScript 2.使用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只是你能够点击一个对象的唯一方法,鼠标向上,背景仍然会改变。 JavaScript gives you a couple ways to do it too. JavaScript也为您提供了几种方法。

You can use JavaScript to change the object's style attribute to update the 'background-image'. 您可以使用JavaScript更改对象的样式属性以更新“背景图像”。

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

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

3. Change Class Attribute with JavaScript 3.使用JavaScript更改类属性

Or similarly, you could create two classes in your CSS, and use JavaScript to update the object's class attribute. 或者类似地,您可以在CSS中创建两个类,并使用JavaScript来更新对象的类属性。

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

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

JSFiddle: http://jsfiddle.net/pkrWV/2/ 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. 我个人最喜欢的方法是第三个选项,你仍然使用CSS来设置不同状态的obj,然后使用JavaScript来更改类名来更新这些状态。 It's less JavaScript, more CSS, and you're keeping everything in their appropriate places. 它更少的JavaScript,更多的CSS,并且你将所有东西保存在适当的位置。

$(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 jquery onclick改变css背景图像

There's no way to do this in pure HTML/CSS, but in javascript you can do it like so: 在纯HTML / CSS中无法做到这一点,但在javascript中你可以这样做:

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> 您可以将其包含在<script></script>标记中,也可以将其添加到.js文件中,并通过添加<script src="scriptName.js"></script>

Here's a CSS-only solution: http://jsfiddle.net/VVj6w/ 这是一个仅限CSS的解决方案: http//jsfiddle.net/VVj6w/

HTML HTML

<input type = "checkbox" id = "backgroundToggle" />
<label for = "backgroundToggle">Switch Background</label>
<div id = "wrapper"></div>

CSS CSS

* {
    padding: 0;
    margin: 0;
    border: 0;
}

html, body {
    height: 100%;
    -webkit-user-select: none;
    -moz-user-select: none;
    -o-user-select: none;
    user-select: none;
}

input[type = "checkbox"] {
    display: none;
}

label {
    position: absolute; 
    top: 10px;
    left: 10px;
    background-color: #eee;
    border: 2px solid #ccc;
    padding: 5px;
    border-radius: 10px;
    font-family: Arial, Sans-Serif;
    cursor: pointer;
}

#wrapper {
    background-color: hsla(0, 100%, 50%, 1);
    height: 100%;
    width: 100%;
}

input[type = "checkbox"]:checked ~ #wrapper {
    background-color: hsla(0, 100%, 50%, 0.1);
}

If you only want it to change while you are clicking, you should be able to use 如果您只想在点击时更改它,则应该可以使用

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

If you want it to remain that way after the click (after the mouse button has been released), you will have to use javascript. 如果您希望它在单击后保持这种状态(在释放鼠标按钮之后),则必须使用javascript。 Something like the following 像下面这样的东西

document.getElementsByClassName("hello-button")[0].addEventListener("click", function(el) {
    el.classList.add("clicked");
});

Then in the CSS, update your selector to 然后在CSS中,将选择器更新为

.hello-button.clicked

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM