Replace hover effect by active and focus

Joined
Feb 16, 2024
Messages
17
Reaction score
0
Hi Coding Forums,

I was wondering how you deal with hover effect on smartphone? You have to tap and maintain your finger on the screen to have an hover effect but I assume most users will just tap it briefly, so I use the code bellow but it does not work. So how would solve this? What is your approach for responsive design?

@media (max-width: 768px) {
.container {
padding: 20px;
}
.intro .button:focus,
.intro .button:active {
background-color: white;
color: dimgrey;
}
.intro {
padding: 30px;
}
.intro h1 {
font-size: 1.5em;
}
.intro p {
font-size: 1.45em;
}
.intro .button {
font-size: 0.9em;
padding: 15px 20px;
margin-top: 10px;
}
@keyframes pulse {
to {
box-shadow: 0 0 0 15px rgba(232, 76, 61, 0);
}
}
footer {
font-size: 0.90rem;
}
}
.intro .button:focus,
.intro .button:active {
background-color: white;
color: dimgrey;
}



1724424288376.png



VBService comes to my help! 😁
 
Joined
Jul 4, 2023
Messages
453
Reaction score
54
Have you tried writing the code some like this?
To test run this code on a touchscreen device.
HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Effect: Hover over touch screen</title>
    <style>
      .button {
        padding: 10px 20px;
        background-color: lightblue;
        border: 1px solid #000;
        text-align: center;
        transition: background-color .15s;
        user-select: none;
      }
      .button:hover,
      .button:active,
      .hovered {
        background-color: deepskyblue;
      }
      .space50 {
        background-color: wheat;
        height: 50vh;       
      }
      .space300 {
        background-color: wheat;
        height: 300vh;
      }

    </style>
  </head>
  <body>
    <p class="space50"></p>
    <div class="button">Click or touch me</div>
    <p class="space300"></p>

    <script>
      const button = document.querySelector('.button');

      button.addEventListener('touchstart', function() {
        this.classList.add('hovered');
        setTimeout(() => { button.classList.remove('hovered'); }, 150);
      });   
    </script>

  </body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,879
Messages
2,569,939
Members
46,232
Latest member
DeniseMcVi

Latest Threads

Top