Fade out the div element on either desktop or mobile devices

I have a div (fademe) that needs to be displayed whenever a user hovers the mouse over another div.

The issue is that mobile browsers do not support hover events and I am struggling to find a solution that works on all devices.

This is my code for desktop devices:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

$("body").on("mouseover touchstart", ".parent", function(e) {
  $("#fademe").addClass("show");
});
$("body").on("mouseout touchend", ".parent", function(e) {
  $("#fademe").removeClass("show");
});
#fademe {
  color:#000;
  text-align: center;
  font-size: 14px;
  float: right;
  margin: 0 25px;
  opacity: 0;
  -webkit-transition: opacity 1s;
  -moz-transition: opacity 1s;
  -o-transition: opacity 1s;
  transition: opacity 1s;
}

.show {
  opacity: 1 !important;
  -webkit-transition: opacity 1s;
  -moz-transition: opacity 1s;
  -o-transition: opacity 1s;
  transition: opacity 1s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<body>
  <div class="parent">

    <div id="fademe"> »» here «« </div>

  </div>
</body>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Eh, what's up doc?</title>
<style>
.fademe {text-align:center;
font-size:14px;
float:right;
margin:0 25px;
opacity:0;
-webkit-transition:opacity 5s;
-moz-transition:opacity 5s;
-o-transition:opacity 5s;
transition:opacity 5s;}

.show {opacity:1;
-webkit-transition:opacity 5s;
-moz-transition:opacity 5s;
-o-transition:opacity 5s;
transition:opacity 5s;}
</style>
</head>
<body>

<div class="fademe"> »» here «« </div>

<script>
$("body").hover(
  function () {
    $(".fademe").addClass("show"); 
  },
  function () {
    $(".fademe").removeClass("show");
  }
);
</script>

</body>
</html>

Answer №1

For optimal mobile experience, it is recommended to utilize the touchstart and touchend events.

 $('body').on('mouseover touchstart', '#idOfMyElement', function(e){ });   
 $('body').on('mouseout touchend', '#idOfMyElement', function(e){ });   

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

Is it advisable to incorporate await within Promise.all?

Currently, I am developing express middleware to conduct two asynchronous calls to the database in order to verify whether a username or email is already being used. The functions return promises without a catch block as I aim to keep the database logic se ...

Utilizing JavaScript AJAX GET to send parameters to an MVC controller

I'm facing an issue with passing a variable collection of data to my C# controller via AJAX from JavaScript. The data being passed may vary each time and it's important to note that there won't be any create, update, or delete operations inv ...

My initial junior UI/UX assignment: Can you confirm whether this form modal dialog is pixel-perfect, and offer any suggestions for improvements

Currently diving into my first project as a Junior UX/UI Designer. Coming from a background in software engineering, I decided to challenge myself by focusing on design. I'm seeking feedback on the pixel perfection of this modal window, which my seni ...

Convert your Coffeescript loop that uses the range method into ES6 syntax

Disclaimer: Although I acknowledge the impact Coffeescript has had on the ES6 spec, I personally look forward to moving on from it. The following Coffeescript loop (written by someone else) if @props.total>1 for page in [<a href="/cdn-cgi/l/email ...

"Incorporating Social Media Icons into Your Website using HTML and CSS

Looking to recreate a design featuring social media icons (LinkedIn, Facebook, Google Plus, Twitter) using HTML and CSS. https://i.sstatic.net/016im.png Although I have managed to create something similar in fiddle with font-awesome, it's not an exa ...

Restrict the vertical camera rotation

Utilizing JSModeler for showcasing OBJ files. The software integrates THREE.JS and generates a PerspectiveCamera. My goal is to restrict the camera's Y-axis movement to prevent it from going beneath the object. While I am familiar with achieving this ...

Retrieving unprocessed HTTP response using AJAX

Is it possible to retrieve the full raw HTTP response from the server using plain JavaScript AJAX in a web browser? When I say raw response, I mean both headers and body in a raw text format like the following: < HTTP/1.1 301 Moved Permanently < Lo ...

CKEditor textarea value is not being sent through POST after submitting the ajax form

In my form, there are text fields and a textarea with CKEditor. When the onclick button is triggered, the value from the art_title field is sent to the art_save.php page successfully. However, the value from the textarea is not being sent. <script src= ...

Display content at the bottom using CSS

I have scoured both SO and Google for an answer to my problem, but haven't had any luck. I am trying to achieve a simple task, but I'm unsure of how to do it. Here is the scenario: Currently, I am working on a website using HTML, CSS, and Bootst ...

determine the actual height of the window taking into account the height of the vertical scroll

Is there a way to accurately get the window inner height while considering the x-scroll bar? It appears that window.innerHeight always returns the same height even when the x-scroll bar is present. Are there alternative methods to obtain the window height ...

Vue email validation is failing to return a valid email address

I'm relatively new to Vue and have implemented email validation using the reg expression in my Vue script data for this project. By utilizing console.log(this.reg.test(this.email)) and observing the output while users input their email, the validation ...

Incorrect media type linked to Gmail API attachment error

I need help sending a message via the Gmail API in JavaScript, including a JPEG file attachment. My code so far looks like this: $.ajax({ type: "POST", url: "https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=multipart", he ...

Unexpected server failure when connecting via socket.io to Node.js http-proxy

I'm encountering an issue with my Nodejs proxy server related to the dreaded socket hang up error. The server crashes every time I refresh the browser on 'app.example.com'. The connection seems fine and the page loads correctly, but the cra ...

Validation of input using JQuery

I am currently working on validating a form, with a specific field that needs to be required. Here is the code for the field in question: <p> <label for="lf">Name: </label> <input class="lf" name="name" type="text"/> < ...

HTML is meant to contain PHP code, but in this case, it is not

Although I am new to PHP, I did some research and found a way to display a PHP Variable within HTML (in this case, it's an h1 element) if($current_month != $old_date) { $test = the_date( 'F' ); echo '<h1>'.$test.&apos ...

Converting a JSON array into a list of strings in a Spring application using the GSON library

Here is the JSON data I have: ["2848","241"] Using the jQuery code below, I am building a list from selected checkboxes: var list = []; $.each($("input[class='selected']:checked"), function(){ list.push($(this).val()); }); The ...

Unraveling and interpreting all incoming requests to my Node.js application

Looking for a simple method to identify and decipher all encoded characters in every URL received by my Node.js application? Is it possible to achieve this using a middleware that can retrieve and decode symbols such as & ? ...

Troubleshooting the deployment of a complete full-stack application on Heroku

I have been facing the challenge of deploying my full-stack chatkit messenger app from localhost to production on Heroku. I am encountering a 404 "Not Found" error and unsure about the necessary changes that need to be made in my code for it to run smoothl ...

textarea label align: center

I've been struggling to center-align the label for this textarea within the text box, but so far my attempts have failed. The resulting display resembles the following: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxx ...

Execute the controller function with the value as a parameter

I encountered an issue while attempting to call a function in the c# controller and passing a value. The error message I received was: `'Unable to get property 'then' of undefined or null reference'. I also included the Driver Model but ...