简体   繁体   中英

Simulate a shine effect on a button that is executed on hover event, but with other event

this is my first question

I can't find an answer. I have been looking on various topics, like this ones (for example):

I want to simulate a shining effect on a button that triggers on hover (The effect is just CSS and i took it from this site: https://codemyui.com/pure-css-shining-button-hover-animation/ ), but with other event, like clicking some other button or whatever. According to my previous topics research, is not possible to trigger a hover event, so the workaround is assign a class to that hover CSS style, and thats what i tried to do, but i can't make it work.

I think the problem is because the CSS it has pseudo-elements like :after , :before and :hover and sometimes they are combined.

Here is the code:

CSS:

@import url(https://fonts.googleapis.com/css?family=Raleway:300);
.btnShine {
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translateX(-50%) translateY(-50%);
          transform: translateX(-50%) translateY(-50%);
}

.btnShine {
  background: #333;
  color: #ccc;
  width: 200px;
  height: 60px;
  border: 0;
  font-size: 18px;
  border-radius: 4px;
  font-family: 'Raleway', sans-serif;
  -webkit-transition: .6s;
  transition: .6s;
  overflow: hidden;
}
.btnShine:focus {
  outline: 0;
}
.test1, .btnShine:before {
  content: '';
  display: block;
  position: absolute;
  background: rgba(255, 255, 255, 0.5);
  width: 60px;
  height: 100%;
  left: 0;
  top: 0;
  opacity: .5;
  -webkit-filter: blur(30px);
          filter: blur(30px);
  -webkit-transform: translateX(-100px) skewX(-15deg);
          transform: translateX(-100px) skewX(-15deg);
}
.test2, .btnShine:after {
  content: '';
  display: block;
  position: absolute;
  background: rgba(255, 255, 255, 0.2);
  width: 30px;
  height: 100%;
  left: 30px;
  top: 0;
  opacity: 0;
  -webkit-filter: blur(5px);
          filter: blur(5px);
  -webkit-transform: translateX(-100px) skewX(-15deg);
          transform: translateX(-100px) skewX(-15deg);
}
.test4, .btnShine:hover {
  background: #338033;
  cursor: pointer;
}
.test3, .btnShine:hover:before {
  -webkit-transform: translateX(300px) skewX(-15deg);
          transform: translateX(300px) skewX(-15deg);
  opacity: 0.6;
  -webkit-transition: .7s;
  transition: .7s;
}
.test5, .btnShine:hover:after {
  -webkit-transform: translateX(300px) skewX(-15deg);
          transform: translateX(300px) skewX(-15deg);
  opacity: 1;
  -webkit-transition: .7s;
  transition: .7s;
}

HTML:

<button id="btn">Simulate a hover on the Stylized button</button>
<!--this shine effect was taken from this url https://codemyui.com/pure-css-shining-button-hover-animation/-->
<button class="btnShine">hover</button>

JS / JQuery:

$("#btn").on("click", function(){
    //$(".btnShine").trigger("mouseenter");
  //$(".btnShine").trigger("hover");
  //$(".btnShine").trigger("mouseover");
  //$(".btnShine").mouseover();
  $('.btnShine').addClass("test3"); 
  $('.btnShine').addClass("test4"); 
  $('.btnShine').addClass("test5"); 
/*
  $('.btnShine').removeClass("test5");
  $('.btnShine').removeClass("test4");
  $('.btnShine').removeClass("test3");
  */
});

I made this JSFiddle, so you guys can understand what i'm trying to do.

https://jsfiddle.net/kaedfegn/2/

PS: I'm Chilean so my native language is Spanish.

You forgot to add the :before and :after to all your test classes.

So, this:

.test1, .btnShine:before

should be this:

.test1:before, .btnShine:before

See the updated fiddle

I would consider adding all your "test" classes into one class and writing the styles around that 1 class. I combined all of them and called it .shine in this fiddle .

so the selector would look something like this:

.btnShine.shine, .btnShine:hover {
  background: #338033;
  cursor: pointer;
}

.btnShine.shine:before, .btnShine:hover:before {
  -webkit-transform: translateX(300px) skewX(-15deg);
          transform: translateX(300px) skewX(-15deg);
  opacity: 0.6;
  -webkit-transition: .7s;
  transition: .7s;
}

.btnShine.shine:after, .btnShine:hover:after {
  -webkit-transform: translateX(300px) skewX(-15deg);
          transform: translateX(300px) skewX(-15deg);
  opacity: 1;
  -webkit-transition: .7s;
  transition: .7s;
}

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