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

I'm having trouble with aligning my input checkbox within the form

My form includes multiple checkboxes, but I am having trouble aligning them properly. <!-- Multiple Checkboxes (inline) --> <div class="form-row"> <label class="chkdonar" for="donarchkform-0">Yes, I want to donate to AMIAB</label> ...

Angular Oops! We ran into a small hiccup: [$injector:modulerr]

I am facing an issue with an angular js error angular.js:36 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.2.19/$injector/modulerr?p0=app&p1=Error%3A%20…gleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.19%2Fangular.min.js%3A18%3A139) ...

Secondary menu supersedes primary dropdown menu

Having trouble getting a vertical dropdown menu to work properly, with the submenus overriding the main menu. I've searched everywhere for a solution but can't seem to find one. Anyone out there who could help me figure this out? Here's the ...

Is there a way to extract the "validade" value from the array and retrieve it exclusively?

The following array contains data: {"status":true,"data":[{"id":1,"pessoa_id":75505,"created_at":"2022-02- 01T17:42:46.000000Z","holder":"LEONARDO LIMA","validade&quo ...

Unable to properly vertically center multiple divs within a parent div and align their elements vertically within them

I'm diving into the world of HTML/CSS and grappling with various concepts. My goal is to vertically align 2 divs within a larger div, as well as center the contents within those divs. HTML: <html> <head> <link rel="stylesheet" typ ...

Is there a way to dynamically change the source of an image based on different screen sizes using Tailwind CSS?

Currently, I am utilizing the next/Image component for setting an image and applying styling through tailwindcss. However, I have encountered a roadblock at this juncture. My Objective My goal is to dynamically change the src attribute of the image based ...

Achieving 100% height with Flexbox in CSS

In my layout, I have a column on the right with <IndustryList />, and I want everything in the other <div> that contains <ParameterAndPostSearch /> and <SocialPostList /> to extend to the bottom. However, no matter what I try, setti ...

core.js:15723 ERROR TypeError: Unable to access the 'OBJECT' property because it is undefined

Whenever I attempt to run this function, I encounter an issue. My goal is to retrieve the latitude and longitude of an address from Google's API. This error message pops up: core.js:15723 ERROR TypeError: Cannot read property 'geometry' of ...

Mastering the art of integrating two applications in Django involves understanding how to properly

Hey there, I'm currently diving into Django and I have a question regarding making my two apps work together. Here's how my folders are structured: mysite/ --/app1 (blog app) --/app2 (a list of users that sign up with a form) --/mysite --db --m ...

Discovering the Nearest Textfield Value Upon Checkbox Selection Using JQuery

How can I retrieve the value of the nearest Textfield after selecting a checkbox? This HTML snippet serves as a simple example. In my actual project, I have dynamically generated lists with multiple checkboxes. I require a way to associate each checkbox wi ...

"Discrepancy between default checkbox and its current state

Currently, I am facing a challenge with setting the defaultChecked value of a checkbox to match the value stored in my database. Despite having the correct boolean value in the database, the defaultChecked always ends up being false. Here is how I have ta ...

What is the best way to pass card data between Vue.js components?

My application consists of two components: DisplayNotes.vue, which displays data in card format retrieved from the backend, and UpdateNotes.vue, which allows users to update existing data by opening a popup. The issue I'm facing is that when a user cl ...

Changing the text during a reset process

I've been grappling with this issue, but it seems to slip through my fingers every time. I can't quite put my finger on what's missing. My project involves clicking an image to trigger a translate effect and display a text description. The ...

What might prevent an onSubmit event from triggering the execution of "checkTheForm()"?

Despite consuming a substantial amount of information on the internet, I still find myself puzzled by why my code isn't functioning as expected. I acknowledge that there are numerous tutorials out there guiding me to use <form action="index.html" o ...

Discover the magic of using Polymer core-animated-pages transitions to create a stunning tile-cascade effect for your custom elements!

Trying to recreate the slide-up and tile-cascade transitions from this example using a snippet with the polymer designer tool. This time, custom elements are preferred over regular HTML tags. Custom elements have been created for each page and placed with ...

What is the best way to add animation to switch between table rows?

I am looking to add animation effects when table rows appear and disappear. Initially, I attempted using a CSS transition, but it did not work due to the change in the display property. Subsequently, I opted for an animation, which provided the desired o ...

Change the body selector in CssResource to apply custom styles

I have implemented a CssResource within a ClientBundle following the guidelines provided in the documentation at this link. To access the styles from my UiBinder xml, I have used ui:with as explained in the documentation available at this guide. While th ...

What causes CSS animations to suddenly halt?

Recently, I've been delving into the world of CSS animations and experimenting with some examples. Below is a snippet of code where two event handlers are set up for elements, both manipulating the animation property of the same element. Initially, th ...

What are the steps for encoding a value using jquery serialize?

I attempted to encode all values using the following code: encodeURIComponent($("#customer_details").serialize()); Unfortunately, it did not produce the desired results. Is there a method to retrieve all elements on a form and individually encode each v ...

When the Jqueryui dialog is closed, it effectively terminates the current JavaScript thread

Hello there, I'm currently facing an issue with closing my jQuery dialog box. The situation involves a comet connection that sends messages to my browser. My goal is to perform certain actions upon receiving a message, close the dialog, and then conti ...