jQuery Style How to - Change Opacity with onclick, onmousedown and onmouseup








Question

We would like to know how to change Opacity with onclick, onmousedown and onmouseup.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.9.1.js'></script>
<style type='text/css'>
.button {<!--  ww w .j a v  a2s  .c o  m-->
  width: 200px;
  height: 100px;
  background-color: #000;
  opacity: 1.0;
  transition: opacity 2s linear;
  -webkit-transition: opacity 0.5s linear;
  -o-transition: opacity 0.5s linear;
  -moz-transition: opacity 0.5s linear;
  -ms-transition: opacity 0.5s linear;
}

.button:hover {
  opacity: 0.7;
}
</style>
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$(function() {
    $(".button").click(function(){
        $(this).css("opacity", "0.2");
    });
});
});//]]>  
</script>
</head>
<body>
  <div class="button"></div>
</body>
</html>

The code above is rendered as follows: