I have a button with a :active
class instruction to scale down to produce a nice effect, it works fine on pc, but on my mobile device I found that the hover effect remained there after I pressed the button, which is of course not desired. This is how it becomes after I click the button,
Also with the :acitve
pseudo-class, the button becomes smaller in scale when I click it and it works as expected on pc, but on mobile device it just doesn’t work and the active effect only works when I keep on pressing the button for sometime which is not the user would do.
Note: I am using the active pseudo-class to show an effect when the user clicks the button.
Note 2: I am not actually using a <btn>
tag instead a <p>
tag for the button, I just now tried using the <btn>
tag but it doesnt scale up or down while using it.
This is the snippet of the button,
.queueAdd {
background-color: #008d3fed;
transition: 0.2s;
user-select: none;
-webkit-user-select: none;
}
.queueAdd:hover {
scale: 1.1;
cursor: pointer;
}
.queueAdd:active {
scale: 0.7;
background-color: #00853ced;
}
<!-- bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha3840evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
<!-- The button -->
<p style="width: fit-content" class="fs-4 px-3 m-3 rounded-2 queueAdd text-center">+</p>
2
Answers
If anyone ever stumbles by this post and is looking for a solution, I have a work around for the button click effect, in general this can be applied to produce any needed effect not just the one I was looking for,
This is how I did it:
created a new function and added that to the
onclick
of my button/element, passedthis
in the function's argument which basically just means the element which is actually triggering the function is itself passed inside.Made the function add a class to the element, the class is just a animated keyframe which produces my desired effect, and then after some delay remove the class so the button is back to normal.
You could also have directly styled and chnaged the appearance with javascript, but it just didnt give a smooth transition that i wanted so I went for the class approach
Note/Tip: You can add multiple functions by seprating each funciton with a
;
(semicolon)Here is the code:
HTML
CSS
JS
I think this is by design, so that on
:hover
menus would work on mobile as well. Also the delay before:active
is to differentiate between regular:hover
.I think you can work around this using
mouse
events and some javascript. Maybe usingtouch
events would be more precise.This might work: