Slow mouse hover element highlighting in jQuery is causing issues

I'm currently working on creating a simple chrome extension that allows me to highlight an element when hovering over it with the mouse. I've been trying to implement a feature where the element is highlighted by adding a border shadow, but this seems to significantly slow down the browser when there are many elements on the website. Therefore, I am seeking a more efficient solution.

Here is the CSS file:

.highlight {
    -moz-box-shadow: inset 0 0 5px 5px #FF0000;
    -webkit-box-shadow: inset 0 0 5px 5px #FF0000;
    box-shadow: inset 0 0 5px 5px #FF0000;
    transition: box-shadow 0.5s;
    z-index: 99999;
}

This is the JavaScript code:

$(document).ready(function(){
$('*').on('mouseover',function (e) {
    e.stopPropagation();
    $(e.target).addClass('highlight');
}).on('mouseout', function (e) {
        e.stopPropagation();
        $(e.target).removeClass('highlight');
    });
});

Answer №1

To improve efficiency, consider attaching the event to the document rather than each element individually:

$(document).ready(function() {
    $(document).on('mouseover',function (e) {
        e.stopPropagation();
        $(e.target).addClass('highlight');
    }).on('mouseout', function (e) {
        e.stopPropagation();
        $(e.target).removeClass('highlight');
    });
});

This response offers valuable insights into event delegation. In your scenario, linking the event handler to the document object is definitely a sensible choice.

Answer №2

To add a hover effect using only CSS:

*:hover{
    box-shadow: inset 0 0 5px 5px #FF0000;
    transition: box-shadow 1s;
}

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

Why isn't the z-index functioning properly in Vue.js styling?

I am currently developing a simple component that displays four steps, indicating the current step and allowing users to navigate through them. I want to create a design where these numbers are enclosed within circles that are evenly distributed in a line ...

Is it possible to toggle between hide and show functions using Jquery?

I have a question about two jquery functions that I've been struggling with. $(document).ready(init); function init() { $(".alphaleft").hover(function (g) { $(".boxy,.yee").show(); }, function (g) { $(".boxy,.yee").hide(); }); } ...

An unusual icon with three varying sizes in a single file

I have come across an interesting favicon file. Click here to download the favicon file Unfortunately, I am unable to upload this image file as it does not meet the required format standards. Upon viewing the file in Windows 7, a fascinating discovery w ...

I'm utilizing bootstrap to design a slider, but the length of the second div inside it is longer than the slider div

https://i.sstatic.net/0iIPb.png To ensure that the second div appears above the slide div, I have applied the position:absolute property to the second div. Since the first div is a slide, I had to position the second div outside of the first div. Do you h ...

What is the secret to keeping a hover effect active?

As I hover over the buttons in my stack, a text box appears on the right side. The intention is for this text box to act as a scroll box, but it disappears when I move my mouse to scroll because it needs to be continuously hovered upon to stay active. To ...

Accessing database fields through JSON

I am facing an issue with my database that consists of 4 columns (id-symbol-name-contractnumber). While all 4 columns are being displayed on the user interface using JSON, I recently added a new column (countrycode) successfully to the database. However, I ...

Check Image Dimensions prior to Image Loading

I've been working with JQuery Masonry and would like to incorporate Lazy Load using a WordPress plugin to load images only when they come into view. The issue I'm facing is that when Lazy Load is used, the masonry elements don't recognize t ...

The two items are not situated side by side

Trying to make my page responsive, but when I inspect and look into mobile view, the elements do not align next to each other like they do on desktop. https://i.sstatic.net/8WhxD.png I want the shopping cart icon and button to be positioned next to each o ...

Performance issues with jquery addClass and removeClass functions observed in Internet Explorer 11

I am currently working on an application that monitors nodes within a cluster, and I have created a visual state example to demonstrate this. Each small box in the grid represents a node, and when hovering over a node, the rest of the nodes in that particu ...

Guide to creating a line break with a break tag in HTML and copying it to the clipboard

Is it possible to copy text to the clipboard and have it appear on a new line? When I click the copy button and paste the text (on a post, website, blogger, forum, notepad, etc.), I want it to maintain its original formatting. Here is how the text looks b ...

Utilizing jQuery and Isotope for intricate filtering

My isotope instance contains elements with multiple parameters, as shown below: <element data-a="1 2 3 4 5" data-b="1 2 3 4 5" data-c="1 2 3 4 5" Filtering for an element that has A1, B2, and C3 is straightforward: .isotope({ filter: '[data-a~=1 ...

Image link in HTML / CSS not functional on one half of the image

My webpage has an image thumbnail with accompanying text positioned to the right. I have a hyperlink on the image that should open the full-size version when clicked. However, there seems to be an issue where the hyper link only activates on the lower hal ...

Php/JavaScript Error: Identifier Not Found

I've double-checked my code multiple times, but it still doesn't seem to be working properly. When the PHP runs, the browser console displays the following error: Uncaught SyntaxError: Unexpected identifier. I'm not sure if this is a si ...

Tips for ensuring that the elements within the flexbox are centered and aligned to the left

New proposed layout I need assistance with centering all flexbox items in a 3-column layout. Additionally, I want the items in rows that are not full to be centered and aligned to the left. Any suggestions for achieving this? Below is my current code, bu ...

What could be the reason for the lack of functionality in this jQuery code when combining a for loop with the $

<!DOCTYPE html> <html> <head> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ for(let i = 0 ...

Having trouble adjusting the refresh timer based on user focus with setTimeout

For the past few days, I've been utilizing an ajax call to dynamically refresh specific elements of my webapp every 5 seconds. The implementation with setInterval(refreshElements, 5000) worked perfectly fine. However, now I am considering whether the ...

Is it possible to create a hidden scrollbar that functions seamlessly on a wide range of web browsers?

I'm currently working on a bootstrap website that features two columns that stretch to the height of the viewport, with any overflow being managed by scrollbars. This allows for the columns to be scrollable without affecting the overall page height. ...

Is it possible to customize the background color of the 'rows per page' selector box in Bootstrap 4 bootstrap-table?

The text is set in white on a dark grey background by default and appears just below the formatted table. Best regards, Phil Please refer to the attached image:Section of table showing rows per page selector box ...

The size of the generated file from tailwind build is compact

I've been working with TailwindCSS and have followed the installation process outlined below. However, after completing the steps, I ended up with a tailwind.css file that only contains 369 lines. This seems to be fewer lines than what others have in ...

Tips for implementing personalized command buttons in Kendo Grid with AJAX request utilizing JavaScript

I am struggling with implementing custom command buttons in a Kendo grid that makes an AJAX call using JavaScript to call a web API post action with dynamic parameters (start, stop, restart) behind button clicks. datasource dataSource = new ken ...