Keep Active Script Click

Asked

Viewed 82 times

0

I managed to execute a script where when making mouseover(Hover) on a given object, it executes the desired action, but I would like to execute the action with a click and I am unable to do neither with Javascript, nor Jquery...

<script>
        $('.dimmer_area').hover(function(){
            $('.dim_area').fadeIn(200);
        },function(){
            $('.dim_area').fadeOut(200);
        });
    </script>

Can someone help me ?

  • Please finish the question by choosing one of the answers, Do not leave the question open. Obg!

2 answers

1

If you’re using the same action, you can use .fadeToggle(200), which is best suited to the situation, within an event click:

$('.dimmer_area').click(function(){
   $('.dim_area').fadeToggle(200);
});
.dimmer_area{
   width: 200px;
   height: 20px;
   background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dimmer_area">clique aqui</div>
<div class="dim_area">dim_area</div>

0

To do with click just add a variable to help control the state, example...

let ativo = false;

$('.dimmer_area').click(function(){
            if(ativo)
            $('.dim_area').fadeIn(200);
            else
            $('.dim_area').fadeOut(200);
            
            ativo = !ativo;
        });
.dimmer_area {
  width: 200px;
  height: 100px;
  background: #424242;
  display: flex;
  align-items: center;
  justify-content: center;
}
.dim_area {
  width: 100px;
  height: 50px;
  background: #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div class="dimmer_area">
  <div class="dim_area">
    
  </div>
</div>

  • Thank you @Build-Up! Is there any way to do Hover and click together in one script ? I am using the scripts separately each one and I am having headaches. If there is an algorithm, I would be grateful.

  • @Eitaehnoiz can explain better, did not understand kkk

  • Hello @Facebook-Store, in the script you created, is it possible to do the Hover and click together ? By hovering the mouse over a given object, it does an action and clicking, it keeps this action active. It is possible ?

  • just duplicate the part where I do the click action to another with the Hover, ai ctz that will work because the variable will allow global control...

  • Okay, thanks for the suggestion.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.