Home > Back-end >  How to solve the problem of button flickering caused by css transition?
How to solve the problem of button flickering caused by css transition?

Time:03-02

I have seen some solutions but they actually affect the design intended. Is there any way to stop this flicker without altering the design? I understand that the problem is that While hovering over a moving (or animated) element, it may just un-hover from the element because it moves beneath my cursor.

    <!DOCTYPE html>
<html>
<head>
<style>
body{
 background-color: #FFEE32;
}
.button {
box-shadow: -10px 10px;
    background-color: #FFEE32;
    border: 3px solid;
    display: inline-block;
    cursor: pointer;
    color: #202020;
    font-size: 30px;
    font-weight: bold;
    padding: 20px 30px;
      transition: all 0.15s linear 0s;
    text-decoration: none;
    position: relative;
    box-sizing: border-box;
    
}

.button:hover {
    top: 3px;
      transition: all 0.15s linear 0s;
    left: -3px;
    color: #FFEE32;
    background-color: #202020;
    border: #FFEE32;
    box-shadow: 0px 0px 0 #ffe800 !important;
    position: relative;

}

</style>
</head>
<body>

<h2>Animated Button - "Pressed Effect"</h2>
<div >
<button >Click Me</button>
</div>
</body>
</html>

check it out here, try to hover on edges from right and bottom

CodePudding user response:

I would add an ::after pseudo element on your .button:hover, that will be slightly bigger than your actual button's area. As a result while you're hovering, the pseudo element will 'jump' under the cursor, that will prevent flickering - .button will not escape from your mouse :)

The below addition is not the perfect solution, but can be a working solution for your problem

.button:hover:after {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  margin-left:10px;
  margin-top:-10px;
  height: 130%;
  width: 110%;
  /*border: 3px solid blue;*/
  box-sizing: border-box;
  padding:10px;
}

Please note, by uncommenting the border: 3px solid blue rule you'll see the actual position and size of the pseudo element on hover.

CodePudding user response:

because of border size & top,left position button is "flickering" when hover on corner side.

Add "3px" in border & remove "top & left" position.

<!DOCTYPE html>
<html>
<head>
<style>
body{
 background-color: #FFEE32;
}
.button {
box-shadow: -10px 10px;
    background-color: #FFEE32;
    border: 3px solid;
    display: inline-block;
    cursor: pointer;
    color: #202020;
    font-size: 30px;
    font-weight: bold;
    padding: 20px 30px;
      transition: all 0.15s linear 0s;
    text-decoration: none;
    position: relative;
    box-sizing: border-box;
    
}

.button:hover {
    top: 0px;
      transition: all 0.15s linear 0s;
    left: 0px;
    color: #FFEE32;
    background-color: #202020;
    border:3px solid #FFEE32;
    box-shadow: 0px 0px 0 #ffe800 !important;
    position: relative;

}

</style>
</head>
<body>

<h2>Animated Button - "Pressed Effect"</h2>
<div >
<button >Click Me</button>
</div>
</body>
</html>

  • Related