Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Hover Effects with pseudo class :active or on click #63

Open
davideng14 opened this issue Apr 25, 2015 · 2 comments
Open

Using Hover Effects with pseudo class :active or on click #63

davideng14 opened this issue Apr 25, 2015 · 2 comments

Comments

@davideng14
Copy link

Hi, how couId I apply hover effects when it's clicked some div has some class hover.css, I tried copy the css rules and added the pseudo class :active but it didn't work, Thanks

@davideng14 davideng14 changed the title Hi, how couId I apply hover effects when it's clicked some div, I tried copy the css rules and added the pseudo class :active but it didn't work, Thanks Hi, how couId I apply hover effects when it's clicked some div has some class hover.css, I tried copy the css rules and added the pseudo class :active but it didn't work, Thanks Apr 25, 2015
@sixcious
Copy link

sixcious commented Jun 5, 2015

Unfortunately, :active is only in effect while the click (mouse button) is held by the user, so the animations will go very quickly or may not work.

There are a few CSS-only hacks to trigger animations that bypass JavaScript (using :target and anchors, dummy checkboxes, etc), but the cleanest and best solution I've found is really just using vanilla JS to attach a click event listener and toggling the hover class.

Hover.css

In hover.css, comment out the :hover :focus :active pseudo-class names, and instead define that CSS as a separate class. E.g. for hvr-push you could add a -click to the name:

/*.hvr-push:hover, .hvr-push:focus, .hvr-push:active */ .hvr-push-click { ... }

JavaScript

Attach the click event listener to the element:

document.getElementById("my-button").addEventListener("click", clickButton, false);

Define the click function that will trigger (toggle) the -click class, but make sure to use the setTimeout() to force a tiny delay when adding it back.

function clickButton() {
  var that = this;
  this.classList.remove("hvr-push-click");
  setTimeout(function() {  that.classList.add("hvr-push-click"); }, 0);
}

HTML

Make sure to give the element the regular (not click) hvr-push class:
<input id="my-button" class="hvr-push" type="image" src="img/button.png" alt=""/>

@ghost
Copy link

ghost commented Dec 16, 2015

So would this be the way to assign the same css styles used for the hover effect to the selected menu item when the item selected for a nav menu?

For example. I am using the hvr-underline-from-center effect and renamed them to .menu-item class, and would like the same underline style to be the static style for the .current-menu-item class.

@davideng14 davideng14 changed the title Hi, how couId I apply hover effects when it's clicked some div has some class hover.css, I tried copy the css rules and added the pseudo class :active but it didn't work, Thanks Using Hover Effects with pseudo class :active Jan 5, 2016
@IanLunn IanLunn changed the title Using Hover Effects with pseudo class :active Using Hover Effects with pseudo class :active or on click Oct 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants