简体   繁体   English

使用jQuery切换按钮时更改颜色

[英]Button change color on toggle using jQuery

I'm looking to change the color of the button or in this case the image of the button each time the button is selected on the toggle of the jQuery. 我正在寻找更改按钮的颜色,或者在这种情况下,每次在jQuery的切换上选择按钮时,更改按钮的图像。 Here's what I have so far for jQuery. 到目前为止,这是我对jQuery的了解。

jQuery(document).ready(function () {
    jQuery('#occupation').hide();
    jQuery('#occupationshow').on('click', function (event) {
        jQuery('#occupation').toggle();
    });
});

And here's what I have for the button: 这是按钮的用途:

<button id="occupationshow">
    <img src="../SiteAssets/images/RAC/askcut/Occupation.jpg">
</button>

How can I get it so another image is displayed on the button when the button has been clicked? 如何获得它,以便在单击按钮时在按钮上显示另一张图像?

The best way to do this is I think the following: 最好的方法是我认为以下几点:

  1. Create a sprite image of the two backgrounds for this button - so, one image file, with the two images side by side. 为此按钮创建两个背景的精灵图像-一个图像文件,两个图像并排放置。
  2. Set this image as the background-image of the element using CSS 使用CSS将此图像设置为元素的background-image
  3. Giving your button#occupationshow a fixed width/height, have the jquery modify the background-position of the image depending on the state of the button - simply put, depending on the current state of the button, the image will move left/right within button#occupationshow and you will only be able to see the relevant part at any one time. button#occupationshow一个固定的宽度/高度,让jquery根据按钮的状态修改图像的background-position -简单地说,根据按钮的当前状态,图像将在按钮内左右移动button#occupationshow ,您将只能一次查看相关部分。

You can as suggested modify the src attribute dynamically, but do bear in mind that with this approach the new image might take a moment to load once the button is clicked; 您可以按照建议的方式动态修改src属性,但请记住,使用这种方法,一旦单击按钮,可能需要花费一些时间来加载新图像。 with my approach both images are preloaded (as they are one image) and it's simply moving around, and so is instant. 用我的方法,两个图像都被预加载(因为它们是一个图像),并且只是在四处移动,因此是即时的。

Sprites are a great way of working, I'd recommend looking into them :-) 子画面是一种很好的工作方式,我建议您仔细研究它们:-)

您可以使用jquery的css函数来更改每次单击时按钮的背景图像。

toggle() by default is for showing/hiding. 默认情况下, toggle()用于显示/隐藏。 Use attr() with a callback function attr()与回调函数一起使用

jQuery('#occupationshow').on('click', function (event) {
    jQuery(this).find('img').attr('src', function(){
        var src = $(this).attr('src') == 'img1' ? 'img2' : 'img1';
        return src;
    });
});

I guess you only have two images, right? 我猜你只有两个图像,对不对? So what you can do is put two tags, corresponding to both of your images, ont being set as "display: none". 因此,您可以做的是放置两个分别对应于您的两个图像的标签,然后将其设置为“ display:none”。

<button id="occupationshow">
    <img src="../img1.jpg">
    <img src="../img2.jpg" style="display: none;">
</button>

And in jQuery your code will be : 在jQuery中,您的代码将是:

jQuery('#occupationshow').on('click', function (e) {
    jQuery('#occupationshow img').toggle();
});

Edit : I re-read the first post again and don't know what made me guess OP only had two images... (the fact he wants to use "toggle", I guess) 编辑 :我再次重新阅读了第一篇文章,不知道是什么让我猜测OP仅具有两个图像...(我想这是他想使用“ toggle”的事实)

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

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