How to remove focus from an element using jQuery

I have a button with the class "scrollToTop" that, when clicked on, smoothly scrolls to the top of the page. Below is the JavaScript file for this functionality:

$(document).ready(function(){

  // Check if the window is scrolled down and display the button
  $(window).scroll(function(){
    if ($(this).scrollTop() > 100) {
        $('.scrollToTop').fadeIn();
    } else {
        $('.scrollToTop').fadeOut();
    }
  });

  // Click event to scroll back to the top
  $('.scrollToTop').click(function(){
    $('html, body').animate({scrollTop : 0},800);
    return false;
  });


});

Additionally, this button has a hover effect defined as follows:

.scrollToTop:hover{
  opacity: 1.0;
  box-shadow: 0 10px 20px rgba(0,0,0,0.25), 0 8px 8px rgba(0,0,0,0.22);
  -webkit-transition: all 0.4s ease-in-out;
  -moz-transition: all 0.4s ease-in-out;
  -o-transition: all 0.4s ease-in-out;
  transition: all 0.4s ease-in-out;
}

However, the hover effect does not work well on mobile devices where tapping is required to trigger the hover effects. I am looking for a way to automatically remove the hover effect and set the opacity back to 0.5 once the page is scrolled back to the top without requiring any additional taps from the user. Is there a way to achieve this through jQuery or any other methods? Thank you in advance!

Answer №1

It seems like the solution you need involves using the following code:

 $('.scrollToTop').blur();

You can implement it like this:

$('.scrollToTop').click(function(){
    // Remove focus from the button
    $('.scrollToTop').blur();
    $('html, body').animate({scrollTop : 0},800);        
    return 
});

Alternatively, another suggestion on StackOverflow here proposes using an ontouchend event and then removing and re-appending the element to the DOM:

$('.scrollToTop').on('touchend', function(){
    var el = this;
    var par = el.parentNode;
    var next = el.nextSibling;
    par.removeChild(el)
    setTimeout(function() {
        par.insertBefore(el, next);
    },0);
});

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

When attempting to execute a function when a hidden div is not displayed, JQuery encounters an

I'm currently attempting to modify the text color of the h2 title with the id ticketSearchTitle when the ticketSearch div is not visible. However, the code I have been using to achieve this seems to cause issues (such as the absence of a pointer icon ...

Challenges with Vuex and updating arrays using axios

I am currently facing a challenge with VueJS Vuex and Axios: The issue arises when I retrieve an array with Axios and loop through it to populate its children this way: "Rubriques" has many self-relations, so one rubrique can have multiple child rubriques ...

Is there a way to adjust the SVG level so that it aligns with the center of the polygon instead

Looking to align a simple svg icon for a dropdown button with the text in the button so that the centers match up. Currently, the icon is aligned with the bottom of the text instead. Here's the code for the svg: <svg xmlns='http://www.w3.org ...

JavaScript returns the value 'undefined' when a function variable is used with an array of objects

Here is an example of an array of objects: var theArray = [ {theId:'1', num: 34}, {theId:'2', num: 23}, {theId:'5', num: 26} ]; This function successfully loops through the array: function printValues() { va ...

Sending data using Ajax in a Spring application

When using a post Ajax to call a controller, I am encountering an issue where I do not receive a response and instead get an error. Upon further investigation, it appears that the controller is receiving null for the user name, even though the ajax functi ...

Spin picture 90 degrees using HTML5 Canvas

I'm struggling to properly rotate an image on an HTML5 canvas. I suspect that my math is off, and I would greatly appreciate assistance in correcting this issue. While working on a mobile device, I am collecting a user's signature on a 150px by ...

Issues with the visibility of inlined styles within the Angular component library

After developing a custom library with components for an Angular 4 application, I encountered an issue where the CSS styling from the components was not being applied when using them in an application. Although the functionality worked fine, the visual asp ...

Developing an unchanging structure for HTML pages

I need assistance in designing an HTML layout with a fixed toolbar at the top and bottom, along with a single centered DIV that should be responsive when the user resizes the window both vertically and horizontally. I have attached a mockup/screenshot fo ...

What measures can be taken to avoid the sibling state controller from constantly refreshing?

Check out the code here Within my main dashboard parent state, I have two child states: tags and tickers. When a button in the tickers state is clicked, only the tags state should refresh. Currently, both states are refreshing. https://i.sstatic.net/tP6 ...

Having difficulties setting up the Jasmine Concurrent Grunt Task

I recently configured a number of tasks that are all functioning properly, except for one - the tests task. Although Jasmine-node is set up and running smoothly from the command line, I'm facing issues with getting the grunt task to work. Here is a s ...

Tips for utilizing the "Enter" key rather than the "Tab" key in OpenERP 7 for a one2many field

I am currently working on the sale order line functionality, and I have a specific requirement. When I press the "Enter" key in a one2many field, a new line is created. However, I would like to customize this behavior so that the "Enter" key functions as t ...

Blending components from two arrays in a designated sequence

Currently, I am dealing with 2 arrays selectedERO[] = {"Device A, Device B, Device C"} selectedLinks[] = { "Port A1 - Port B1, Port B2 - Port C1" ) I am looking to create a pathway from device A to device C, and I want it to be presented in an array for ...

Using Vue.js to dynamically toggle CSS classes based on data using a method

Here is the code snippet I am working with: <template> <div id="projects"> <Header /> <b-container> <div class="row"> <div :class="colSize" v-for="(data, index) in projects" :key="data._id"> <b- ...

Switching between various quantities of inputs in each row using Bootstrap V3

My Bootstrap V3 form has a mix of rows with two inputs and rows with one input, as per the designer's request. Check out my fiddle here: https://jsfiddle.net/06qk4wh4/ <div class="form-group col-sm-12"> <label class="control-label col- ...

Transferring data between JavaScript and LESS in Angular

I'm in the process of updating my website for mobile compatibility. I planned on using media queries to apply CSS to smaller divs on the site. However, I also need to adjust the content of some arrays that dictate layout based on screen size. My ini ...

Modifying data in Django models upon selection of a choice field

Asking for people's hobbies on a form used to be a Charfield until I decided to upgrade it to multiple choices. Now, I want to update the old data values for hobbies - for instance, if someone entered "football", "skiing", it should be changed to &apo ...

Avoiding Overlapping in Recharts Scatter Plot

I am encountering an issue where the data I'm inputting into recharts is not overlapping, instead it is being stacked horizontally. This "stacked" data all has the same date on the x-axis and the same count on the y-axis. What I actually want is for t ...

Refresh the page and navigate to the last active tab using PHP and jQuery jTable

After a user clicks the submit button on a jquery-jtable form, I am trying to reload the page and navigate to the tab that was active before the reload. However, my current solution only reloads the page without navigating to the previous tab. The tabs are ...

Tips on how to automatically resize the browser window to a specific width and height once it has been minimized

If a JSP page is minimized onload, we need to find a way to restore it through a JavaScript call. Are there any other methods available for achieving this? The restoration should be to the height and width selected by the user. I have attempted the followi ...

Deducting time from the present moment

I am facing a scenario where I have 2 strings representing the minute and hour for a specific function to execute at. I aim to validate if the specified minute and hour, present in string format and retrieved from my database, are within a 5-minute window ...