Button to return to the top remains visible even when tapped on a mobile device

I am currently implementing a "back to top" button that is fixed in the bottom left corner of the page. It fades in when the user scrolls past a certain point and hides again either when clicked or when the user scrolls back to the top. The functionality works well on desktop, but I am encountering issues with touch functionality on mobile devices. When the button is tapped on mobile, it activates the hover pseudo-class and tooltip, which remains even after scrolling back to the top. I suspect there might be additional code needed to handle touch events, but I'm not sure what exactly needs to be done.

For reference, you can view one of the portfolio pages on my website that features this button: www.nickolder.com/banknote.html

JSFiddle Link

var $btt = $('.back_to_top');

// Scroll to top when user clicks back-to-top button
$btt.on('click', function (e) {

  $('html, body').animate({
    scrollTop: 0
  }, 1200);

  e.preventDefault();

});

// Show / hide back-to-top button depending on scroll position
$(window).on('scroll', function() {

  var self = $(this),
      height = self.height(),
      top = self.scrollTop();

  if ( top > height ) {
    if ($btt.css('opacity') !== 1) {
      $btt.removeClass('hide').addClass('show');
    }
  } else {
    $btt.removeClass('show').addClass('hide');
  }

});
* {
  margin: 0;
  padding: 0;
}

p {
  color: white;
}

p:last-of-type {
  position: absolute;
  bottom: 0;
}

div {
  background: linear-gradient(rgba(20,230,170,1), rgba(20, 170, 230, 1));
  height: 3000px;
  width: 100vw;
  position: relative;
}

.back_to_top {
position: fixed;
z-index: 3;
height: 40px;
width: 40px;
bottom: 20px;
left: 20px;
border-radius: 50%;
opacity: 0.7;
box-shadow: 0 2px 8px 0 rgba(0,0,0,0.3);
  background: -webkit-linear-gradient(top, rgba(204,27,48,1), rgba(109,13,199,1.00));
background: -moz-linear-gradient(top, rgba(204,27,48,1), rgba(109,13,199,1.00));
background: -o-linear-gradient(to top, rgba(204,27,48,1), rgba(109,13,199,1.00));
background: linear-gradient(180deg, rgba(204,27,48,1), rgba(109,13,199,1.00));
}

.back_to_top:hover {
opacity: 1;
box-shadow: 0 6px 12px 0 rgba(0,0,0,0.4);
transform: translateY(-3px);
}

.back_to_top,
.back_to_top:hover {
transition: 0.3s ease;
will-change: transform, opacity;
}

.back_to_top::before,
.back_to_top::after {
position: absolute;
opacity: 0;
pointer-events: none;
will-change: opacity, transform;
}

.back_to_top::before {
content: 'Back to top';
color: rgba(255,255,255,0.8);
background-color: rgba(20,25,30,1);
border-radius: 4px;
width: 100px;
padding: 8px;
text-align: center;
left: 150%;
top: 3px;
}

.back_to_top::after {
border-bottom: 6px solid transparent;
  border-right: 8px solid rgba(20,25,30,1);
  border-top: 6px solid transparent;
left: 130%;
bottom: 13px;
width: 0;
content: '';
}

.back_to_top:hover::before,
.back_to_top:hover::after {
opacity: 1;
transform: translateX(-6px);
transition: 0.4s 0.4s ease;
will-change: opacity, transform;
}

.hide {
opacity: 0;
pointer-events: none;
}

.show {
opacity: 0.7;
}

.hide,
.show {
transition: 0.6s ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <p>Top</p>
  <p>Bottom</p>
  <a href="#" class="back_to_top hide"></a>
</div>

Answer №1

To specifically target devices that fully support the :hover pseudo-class (such as those with a mouse or specific pointing devices), you can utilize the @media hover:hover media query.

Ensure to enclose all your hover-related styles within this media query block.

@media (hover:hover) {
  .back_to_top:hover {
    opacity: 1;
    box-shadow: 0 6px 12px 0 rgba(0,0,0,0.4);
    transform: translateY(-3px);
  }

  .back_to_top,
  .back_to_top:hover {
    transition: 0.3s ease;
    will-change: transform, opacity;
  }

  .back_to_top:hover::before,
  .back_to_top:hover::after {
    opacity: 1;
    transform: translateX(-6px);
    transition: 0.4s 0.4s ease;
    will-change: opacity, transform;
  }
}

Check out your updated Fiddle here

On the flip side, if you wish to target devices lacking complete support for :hover, you could use the following approach:

@media (hover:none), (hover:on-demand) { ... }

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

Encountered an Unpredictable SyntaxError: Is this a Cross-Domain Problem?

I have been attempting to connect with the Indian Railway API using Ajax in order to retrieve data in JSON format. Below is the code I am using: <!DOCTYPE> <html> <head> <meta charset="UTF-8"> <script src="https://ajax.googleap ...

Storing items in localStorage in the same order they were added

I am having an issue with sorting localStorage items by the order they were added. Despite my code working to add items to the localStorage array and display them as HTML, I am encountering a problem with the order of the items. When I add a 3rd item to t ...

The x-axis in c3.js is experiencing issues with plotting when there is interval data in DST time

Trying to create a graph using the c3.js library with an x-axis interval of time. The intervals are determined by selecting a date range in the date picker. For example, if we select the dates 2016-03-13 00:00 to 2016-03-13 04:00, we add 15 minutes to ...

Unable to make colors appear in HTML5 canvas using .fillStyle

Trying my hand at canvas for the first time to create a game. I have an image displaying, but strangely the fillStyle method doesn't seem to be working as expected (the canvas background remains white in Google Chrome). Just a note that in my code, t ...

Creating adaptable horizontal grids is straightforward with these steps

Is it possible to create a 3 horizontal responsive grid using only CSS and HTML, without relying on any framework? I've attempted it but am struggling to come up with a solution. ...

Creating a default option in a Select tag with React when iterating over elements using the map method

After learning that each element in the dropdown must be given by the Option tag when using Select, I created an array of values for the dropdown: a = ['hai','hello','what'] To optimize my code, I wrote it in the following ...

Express router is unable to process POST requests, resulting in a 404 error

I am having trouble fetching POST requests to my Express router. While my many GET requests work fine, this is my first POST request and it is not functioning correctly. Here is a snippet of my frontend code: export async function postHamster(name, age) ...

Is there a way to customize flexigrid's grid contents by passing extra values with jQuery?

My current challenge involves passing timestamp range values along with the regular flexigrid values (page, qtype, query, rp, sortname, sortorder) via JSON to a servlet. I need to include startTime and stopTime with String values like "09/12/2013 16:03" in ...

Arrange elements both at the beginning and the end of a line using FlexLayout in Angular

Using the Angular flexlayout library in angular 7/8, I am trying to align 4 buttons on the left side (flex-start) and two checkboxes on the right side (flex-end) at the same level. I would prefer to achieve this layout using the flexlayout Angular library. ...

Using Selenium to scrape eBay's website

Hello, I am completely new to coding and currently working on a project that involves scraping data from eBay using selenium. Throughout this process, I have encountered two main challenges. The issue arises with the XPath for the listing name, which ...

Selenium is unable to interact with hyperlink buttons in Python

I am currently working on a project where an automated program logs into an account and navigates through various webpages. My current struggle lies in the inability to click on any hyperlink buttons on a specific webpage to progress to the next page. I h ...

Should data objects be loaded from backend API all at once or individually for each object?

Imagine having a form used to modify customer information. This form includes various input fields as well as multiple dropdown lists for fields such as country, category, and status. Each dropdown list requires data from the backend in order to populate i ...

MySQL unable to access information entered into form

After creating a new log in / register template using CSS3 and HTML, I now have a more visually appealing form compared to the basic one I had before. To achieve this look, I referenced the following tutorial: http://www.script-tutorials.com/css 3-modal-po ...

How can I modify the fill color of a Cell when hovering over a Bar Chart in Recharts?

I have been rendering the chart in the following manner: <BarChart width={868} height={40} data={data} margin={{top:0, bottom: 10, left:0, right:0}} barSize={5}> <Tooltip labelStyle={{ textAlign: &apo ...

What is the process for moving a tap-target button with materialize css?

Looking for tips on how to relocate a tap-target button that's currently stuck in the bottom right corner of the screen. I've experimented with various methods like adjusting margins, padding, and even nesting it in an outer div, but nothing seem ...

Ensure that the spacing between rows matches the spacing between columns

I am working on creating a 3x3 grid using Bootstrap. My goal is to have consistent spacing between the rows and columns. How can I achieve this effectively? ...

Troubleshooting the "missing checks.state argument" error in AUTH0 debugging

I recently launched my new NextJs blog application at on Vercel, but I'm encountering some issues with getting auth0 to function properly. Upon clicking the login button located in the top right corner of the screen, users are redirected to auth0 to ...

Tips on adjusting a standard CSS layout with JavaScript and jQuery to generate a unique ID for the updated style

My CSS contains IDs that look like this: <style> .HIDE-DISPLAY-k { background-color: orange; position: fixed; width: 100%; height: 100%; top: 10px; bottom: 0px; left: 10px; right: 0px; overflow: hidden; } #SHOW- ...

Exploring how process.argv in NodeJS can be utilized within JavaScript code compiled by

I'm having trouble compiling a basic TypeScript file using webpack (with 'awesome-typescript-loader') that needs to access command line arguments. It seems like the compiled JavaScript is causing a problem by overriding the Node 'proce ...

Tips for focusing on a background image without being distracted by its child elements

I am trying to create a zoom effect on the background-image with an animation but I am encountering issues where all child elements are also animated and zoomed. When hovering over the element, I want only the background part to be animated and zoomed, and ...