Css effect when mouse clicking

Asked

Viewed 3,422 times

1

I have a css effect on a div using the property Hover , I wonder if it is possible to activate this effect only when it is clicked .

<div class="original">
<div class="spin">
<div class="box">Texto</div>
</div>

CSS:

body {
color: #fff;
margin: 30px;
}

.original {
background: #d2d2d2;
margin: 10px 15px;
width: 95px;
}

.box {
background: red;
height: 95px;
width: 95px;
line-height: 95px;
text-align: center;
transform: rotate(-55deg);

  }
 .spin {
  cursor: pointer;
  }
 .spin:hover {
   animation: spin 3s linear infinite;
 }

  @keyframes spin {
  0% {
  transform: rotateY(0deg);
    }
   100% {
  transform: rotateY(360deg);
   }
   }

2 answers

3


You could try using the dial :target for elements with Ids combined with the URL HASH, for example:

.spin:target {
     animation: spin 3s linear infinite;
}

The code must be something like:

Click on the button below execute and then on the link whole page

body {
     color: #fff;
     margin: 30px;
}

.original {
     background: #d2d2d2;
     margin: 10px 15px;
     width: 95px;
}

.box {
     background: red;
     height: 95px;
     width: 95px;
     line-height: 95px;
     text-align: center;
     display: block;
     transform: rotate(-55deg);
}

.spin {
     cursor: pointer;
}

.spin:target {
     animation: spin 3s linear infinite;
}

@keyframes spin {
    0% {
         transform: rotateY(0deg);
    }
    100% {
         transform: rotateY(360deg);
    }
}
<div class="original">

    <div class="spin" id="elemento1">
        <a class="box" href="#elemento1">Texto</a>
    </div>

</div>

<div class="original">

    <div class="spin" id="elemento2">
        <a class="box" href="#elemento2">Texto</a>
    </div>

</div>

But ai complicates, because it depends on the hash in the url to animate, as:

http://site.com/pagina.html#elemento1
http://site.com/pagina.html#elemento2

So if the hash is more of a problem than a solution for you, just using javascript in with a class for this. In CSS it should look like this:

.spin.animar {
     animation: spin 3s linear infinite;
}

The whole code can be something like (read the comments to understand the code):

//Detecta quando a página já foi carregada (menos os recursos externos)
window.addEventListener('DOMContentLoaded', function () {

    //Pega todos elementos com a classe spin
    var els = document.querySelectorAll('.spin');
    
    //Itera todos elementos
    for (var i = els.length - 1; i >= 0; i--) {

        //Aplica o evento onclick
        els[i].onclick = animarElement;
    }
});

function animarElement()
{
    var els = document.querySelectorAll('.spin');

    for (var i = els.length - 1; i >= 0; i--) {
        var el = els[i];

        //Remove a classe CSS animar de todos elementos, menos o elemento clicado (se já tiver a classe nele)
        if (el !== this && el.classList.contains('animar')) {
            el.classList.remove('animar');
        }
    }
    
    //Se o elemento clicado não tiver a classe animar ainda então aplicamos ela
    if (!this.classList.contains('animar')) {
        this.classList.add('animar');
    }
}
body {
     color: #fff;
     margin: 30px;
}

.original {
     background: #d2d2d2;
     margin: 10px 15px;
     width: 95px;
}

.box {
     background: red;
     height: 95px;
     width: 95px;
     line-height: 95px;
     text-align: center;
     display: block;
     transform: rotate(-55deg);
}

.spin {
     cursor: pointer;
}

.spin.animar {
     animation: spin 3s linear infinite;
}

@keyframes spin {
    0% {
         transform: rotateY(0deg);
    }
    100% {
         transform: rotateY(360deg);
    }
}
<div class="original">

    <div class="spin">
        <div class="box">Texto</div>
    </div>

</div>

<div class="original">

    <div class="spin">
        <div class="box">Texto</div>
    </div>

</div>


Note that the use of :active, :focus and similar work only with elements "clickable" or "focusable", Divs won’t stand for this, so do this:

.spin:active {
    animation: spin 3s linear infinite;
}

For Divs elements will not work.

Following are some examples of them are the elements that support the :active:

  • <a href=""></a> (shall contain the attribute href=)
  • <button></button>
  • And the elements associated with a <label></label>

0

It would make tb using radio-button, you can put a input:radio that when :checked will rotate the .spin. But for that you need to transform the element div.spin in a label.spin as a for="" to the ID of radio-button, looks complicated, but it’s not as you can see in the code.

inserir a descrição da imagem aqui

body {
	color: #fff;
	margin: 30px;
}

.original {
	background: #d2d2d2;
	margin: 10px 15px;
	width: 95px;
}

.box {
	background: red;
	height: 95px;
	width: 95px;
	line-height: 95px;
	text-align: center;
	transform: rotate(-55deg);
	display: block;
}

.spin {
	cursor: pointer;
	display: block;
}

@keyframes spin {
	0% {
		transform: rotateY(0deg);
	}

	100% {
		transform: rotateY(360deg);
	}
}
[id^="btn"]:checked + .spin {
	animation: spin 3s linear infinite;
}
[id^="btn"] {
	display: none; 
}
<div class="original">
	<input type="radio" name="btns" id="btn1">
	<label for="btn1" class="spin">
		<span class="box">Texto</span>
	</label>
</div>
<div class="original">
	<input type="radio" name="btns" id="btn2">
	<label for="btn2" class="spin">
		<span class="box">Texto</span>
	</label>
</div>


To activate individually

If you want the user to be able to mark and uncheck the element just change the input of type="radio" for type="checkbox". Then it can enable and disable . spin individually.

inserir a descrição da imagem aqui

body {
	color: #fff;
	margin: 30px;
}

.original {
	background: #d2d2d2;
	margin: 10px 15px;
	width: 95px;
}

.box {
	background: red;
	height: 95px;
	width: 95px;
	line-height: 95px;
	text-align: center;
	transform: rotate(-55deg);
	display: block;
}

.spin {
	cursor: pointer;
	display: block;
}

@keyframes spin {
	0% {
		transform: rotateY(0deg);
	}

	100% {
		transform: rotateY(360deg);
	}
}
[id^="btn"]:checked + .spin {
	animation: spin 3s linear infinite;
}
[id^="btn"] {
	display: none;
}
<div class="original">
	<input type="checkbox" id="btn1">
	<label for="btn1" class="spin">
		<span class="box">Texto</span>
	</label>
</div>
<div class="original">
	<input type="checkbox" id="btn2">
	<label for="btn2" class="spin">
		<span class="box">Texto</span>
	</label>
</div>

Browser other questions tagged

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