简体   繁体   中英

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.

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

//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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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