简体   繁体   English

使用变量通过jQuery color.js触发颜色更改

[英]Using a variable to trigger a colour change using jQuery color.js

I would like to change the colour of an element (in this example a 100px x 100px square called "block" from transparent to black when the variable trigger= 1. The code listed below works with a button. I have other javascript (that this code will be incorporated into that does work with trigger. 我想更改元素的颜色(在此示例中,当变量trigger = 1时,称为“块”的100px x 100px正方形从透明更改为黑色。下面列出的代码与按钮一起使用。我有其他javascript(代码将与触发器一起使用。

Any help would be appreciated. 任何帮助,将不胜感激。

   <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
      <link rel="stylesheet" href="css/colourswap.css">
    <title>Untitled Document</title>

    <script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
    <script src="_js/jquery.color.js"></script>
    </head>
    <body>
    <div id="block">Hello!</div>
    <!--button id="go">Simple</button-->
    <script>
    var trigger = 1;
        (function(){
        if (trigger == 1) {
      jQuery("#block").animate({
          backgroundColor: "#000"
      }, 1500 );
      };
    });
    </script>
    </body>
    </html>

You can actually just use .show() or .toggle() if the block is transparent otherwise, eg 如果块是透明的,实际上您可以只使用.show().toggle() ,例如

//say if you wanted the block to show when button is clicked
$("#go").click(function(){
    $("#block").toggle();
});

And then set the block's colour to black by default. 然后默认将块的颜色设置为黑色。

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

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