What is the best way to create a seamless background transition for buttons on a mobile device?

Currently, I am working on my random quote generator project and I am trying to make it so that when the button is touched on a mobile device, the background color becomes semi-transparent. Below is the code I am using:

#loadQuote {
  position: fixed;
  width: 12em;
  display: inline-block;
  left: 50%;
  margin-left: -6em;
  bottom: 50px;
  border-radius: 4px;
  border: 2px solid #fff;
  color: #fff;
  background-color: black;
  padding: 10px 0;
  transition: .5s ;
}

#loadQuote:active {
  background-color: rgba(255,255,255,.25);
  -webkit-transition: background-color 10ms linear;
     -ms-transition: background-color 10ms linear;
     transition: background-color 10ms linear;
}

I tested this on the Google Chrome emulator and it worked correctly. However, when I tried it on my iPhone, the effect did not work as expected. I am unsure why this is happening. Additionally, it was strange that when I initially set the transition duration to 1000ms, the background changed very quickly. But when I reduced it to 10ms, the change was slower and more noticeable. This puzzled me, as I would expect longer durations to result in longer animations.

If you want to see the live demo of the project, you can visit .

Thank you!

Answer №1

There are a couple of reasons why your transition might not work consistently:

a) Your transitions are only defined for the :active state of the element, so they will only apply when the element is in that specific state. If it's not in that state, the transition won't occur.

b) It's not recommended to rely on :active for mobile devices. It's better practice to add and remove a class instead. Set the transitions on the base element without a class, and use a class to control the on/off state.

To illustrate this point, I've included some JavaScript code below. However, if you're already familiar with JavaScript, you can ignore it.

let lQ = document.querySelector('#loadQuote');

lQ.addEventListener('click', activatelQ);
lQ.addEventListener('tap', activatelQ);

function activatelQ() {
  lQ.classList.add('active');
  deactivatelQ();
}

function deactivatelQ() {
  setTimeout(function(){
    lQ.classList.remove('active')
  },731)
}
#loadQuote {
  position: fixed;
  width: 12em;
  display: inline-block;
  left: 50%;
  margin-left: -6em;
  bottom: 50px;
  border-radius: 4px;
  border: 2px solid #fff;
  color: #fff;
  background-color: black;
  padding: 10px 0;
  transition: background-color .5s linear;

  text-align: center;
  cursor: pointer;
}

#loadQuote.active {
  background-color: rgba(255,255,255,.25);
}
<div id="loadQuote">Some quote</div>

Based on your website, I noticed you're using jQuery. In that case, you can make the following adjustments to your jQuery code instead of using the provided JavaScript:

  1. Replace
    $('#loadQuote').click(generateQuote);
    with
 $('#loadQuote').on('click tap', function(){
   $(this).addClass('active');
   generateQuote()
 }
  1. Add this line inside your renderQuote() function:
$('#loadQuote').removeClass('active');

On a different note, for future reference, try to include a minimal, complete, and verifiable example within your question. Providing this information upfront helps answerers respond more effectively and keeps your question relevant for other users over time. Additionally, including this information reduces the need for others to visit your website for context. Remember, helping others by creating clear and concise questions can lead to better assistance for yourself.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the method for including a TabIndex property in a JSON file?

I discussed the integration of HTML fields within a JSON file and highlighted how to utilize the TabIndex property effectively in JSON files. ...

Hide Bootstrap columns for Printing

I am currently working on a webpage layout that consists of two columns. <div class="container"> <div class="row"> <div class="col-md-8 "></div> <div class="d-print-none col-md-4"></div> </div ...

Enhancing Next.js Images with Custom CSS Styles

I am working with a Next.js component: import styles from '../styles/Navbar.module.css' import Image from 'next/image' export const Navbar = () => { <div> <Image className={styles["navbar-home-icon"]} ...

Incorporating Bootstrap's Inline Design Elements

I am having trouble aligning a series of elements horizontally using bootstrap as shown in the example image below: Whenever I add the "form-control" class to the input element, it gets pushed down below the other elements. I have experimented with differ ...

activate a "toggle" that allows selection of specific components to show

<span id='question'>{{QuestionText}}</span> <br> <form action={{address}}> {% for each in AnswerQuery %} <span>{{each.answer}}</span><input type='radio' name="answer"> <span>Votes:{{each.a ...

The function .classList.remove() is effective when applied to one element, but fails to work on a different element

I am facing an issue where only one element is getting affected when trying to remove classes from multiple elements if certain email input requirements are met. Can someone help me understand why this is happening? Here is the code snippet: const emailI ...

Leveraging jQuery, Ajax, and PHP for generating and organizing HTML blocks - requiring sound logic

I am in the process of developing a website that compares different products. I have implemented code that retrieves and scrapes data for comparison when a user performs a search. Right now, I am using an ajax request for each company (currently 6 compani ...

Craft a unique parallax effect using Framer Motion's clipping feature

To help visualize my concept, I have sketched it out using Figma. This idea involves a slide transitioning between pages. The goal is to cover the entire page with a sliding effect, and then place a sticker (dubbed the Parallax Box) on top of it. However ...

Tips for transferring information between two distinct pages utilizing the jQuery POST technique

I'm dealing with two PHP files called card_process.php and payment.php. My goal is to transfer data from the cart_process page to the payment page. Here's a snippet of the code: In cart_process.php: $paynow = "<button type='submit' ...

Creating a stationary div element solely with Javascript, excluding the use of CSS, jQuery, and HTML

I've been searching all day for a solution to this issue without any success. I apologize if I overlooked any relevant posts on the topic. Currently, I am working with Qualtrics and attempting to implement a textbox with scrolling instructions that fo ...

Organizing DIVs upon website initialization

My website features a layout with three columns: <div id="column1"></div> <div id="column2"></div> <div id="column3"></div> I currently have 3 divs on the webpage: <div id="1">aaa</div> <div id="2">b ...

Is it advisable to opt for window.webkitRequestAnimationFrame over setInterval?

Trying to figure out the best method for moving game characters in my JavaScript game - should I go with window.webkitRequestAnimationFrame or stick with setInterval? Any advice is appreciated! ...

Need help using JQuery to set the focus on the initial <select> element on a webpage?

Upon loading the page, I am looking to set focus on the initial element through JQuery. So far, I have managed to achieve this by using: $(":input:visible:first").focus(); or $(':input:enabled:visible:first').focus(); as described in a helpf ...

A time-tracking counter that tallies the amount of time users spend on the webpage

I'm currently working on a code that tracks the time spent on a page starting from when it was clicked. My challenge now is figuring out how to capture the time when the submit button is clicked and store it in the database. < script > var ...

Fill an HTML table with comma-separated values (CSV) information

After conducting some research on the internet, I attempted to modify a jQuery script in order to populate an HTML table with data from a CSV file. However, I encountered an issue as the table remained empty. Below, you can find the code that I used. Any a ...

repeating the identical content in the same setting

I am encountering an issue with jquery as I have 2 different sets of icons, each of which I want to do the same thing: My primary codes are <ul> <li> <a href=""> <i class="fa fa-facebook"></i> ...

The TreeView control displays as a <div> element, which results in an unexpected line break

I am currently working on designing a layout that includes an ASP.NET TreeView control on the left-hand side. However, I am facing an issue where the TreeView renders as a div, causing a line break. I have tried using display:inline, but it doesn't se ...

The color of the Angular md-switch .md-thumb is not showing up properly when switching the toggle

In Safari, the md-switch displays a yellow section on the md-thumb when toggled. However, other browsers handle the switch without any issues. The md-bar appears fine, but the md-thumb is yellow. I have tried setting everything to the blue color I am using ...

Exploring CSS Outlining in Visual Studio 2013

Back in the days of Visual Studio 2010, whenever you opened a CSS file, a window would automatically pop up displaying all the classes and IDs for easy navigation within the file. However, in Visual Studio 2013, I seem to be unable to locate this helpful ...

Distinguishing Font Sizes in Safari Compared to UIWebView

When the page is loaded in Safari, everything looks perfect. However, when viewed in a UIWebView, all the fonts appear enlarged. I've done my research and tried various solutions like: - Setting WebView.scalesPageToFit = true - Applying default con ...