Having a slight hiccup with pixel alignment and browser compatibility in my jQuery animation

To emphasize a specific paragraph element, I developed a JavaScript function that displays it above a darkened background.

My approach involved using jQuery to generate an overlay and then duplicating the targeted paragraph element while positioning it absolutely over the overlay.

Due to some CSS properties being dropped (as they were not inherited in the new position on the page), I utilized jQuery to add them back dynamically.

Although it works almost perfectly, I noticed a minor pixel discrepancy when it fades away on my Firefox 3.5.6 browser running on Mac OS X. While I acknowledge this may seem like nitpicking, I aim for a seamless user experience without any visible differences.

You can test the functionality here:

Below is the jQuery function code:

/* jQuery function for highlighting form success */

var highlightFormSuccess = function() {
    var fadeTo = 0.6;
    var $info = $('p.information');

    if ($info.length) {
        // create overlay
        var $overlay = $('<div />')
            .css({
                display: 'none',
                width: '100%',
                height: $(document).height(),
                position: 'absolute',
                top: 0,
                left: 0,
                zIndex: 32767,
                opacity: 0
            })
            .attr({
                id: 'overlay'
            })
            .appendTo('body');
        
        /* extract success block and position above the overlay */
        var left = $info.position().left,
            top = $info.position().top,
            fontSize = $info.css('font-size'),
            width = $info.width(),
            color = $info.css('color'),
            lineHeight = $info.css('line-height');
        
        var $newInfo = $info.clone()
                            .css({
                                position: 'absolute',
                                top: top,
                                left: left,
                                'font-size': fontSize,
                                'line-height': lineHeight,
                                width: width,
                                color: color,
                                zIndex: 32767
                            })
                            .appendTo('body');
        
        $overlay
        .show()
        .fadeTo(1000, fadeTo);
        
        // wait then fade out
        setTimeout(function() {
            $($overlay, $newInfo).fadeOut(1000, function() {
                $newInfo.fadeOut(250, function() { $(this).remove(); } );
            });
        }, 2500);
    };
};

Answer №1

If you want to simplify things a bit, here's how I achieved the desired effect:

p.highlight {
  position:relative;
  background-color:#ffffff;
  z-index:10;
}

For the overlay, I used the following style:

div.overlay {
  position:fixed;
  background-color:#000000;
  z-index:5; // lower than my paragraph, higher than all else
  top:0; left:0; width:100%; height:100%;
}

--

<body>
  <div class="overlay"></div>
  <p>This is a paragraph.</p>
  <p class="highlight">Another paragraph with highlighting.</p>
</body>

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

Mastering the art of bi-directional data binding with nested arrays in Angular

Imagine you have a to-do list with various tasks, each containing multiple subtasks. You want the ability to change the subtask data, but why is Angular not properly two-way binding the data for the subtasks? HTML <div *ngFor="let task of tasks"> ...

Uploading multiple images using Cordova's File Transfer feature

Working on a project using Cordova and jQuery Mobile, my goal is to upload multiple images with the file transfer plugin. After successfully uploading one image, I am now attempting to upload 2 or 3 images in succession. Below is the HTML code snippet: ...

The icons from MaterializeCSS are not displaying correctly on the navbar within an Angular 7 project

Having an issue implementing MaterializeCSS Icons on the navbar. The arrow-drop_down icon is not displaying correctly, showing only text instead. Oddly enough, the icons render properly on other pages except for the app.component.html file. I attempted to ...

The chosen options will automatically populate the text-boxes that require dynamic summation

I am encountering an issue with a select option that should dynamically populate specific values in text-boxes (different prices) based on the selected option. The goal is to sum up the values in the text-boxes and display the total in another text-box. Ho ...

Troubleshooting a malfunctioning PHP form that uses jQuery and AJAX

Snippet of HTML code: <form class="contact_form" action="" name="contact_form"> <ul><li> <input type="email" name="email" placeholder="Please enter your email here" required /> </li><li> <button class="submit" type=" ...

Is the memory efficiency of Object.keys().forEach() in JavaScript lower compared to a basic for...in loop?

Picture a scenario where you have an extremely large JS object filled with millions of key/value pairs, and your task is to loop through each of them. Check out this jsPerf example that demonstrates the different techniques for accomplishing this, highlig ...

Using AngularJS to bind a dynamically created form built in JavaScript

I am looking to dynamically build a form using JavaScript and utilize it within an AngularJS form controller. In the example provided below, the form is not rendering as HTML, and I aim to bind it to the model variable. http://jsfiddle.net/g6m09eb7/ ...

Consolidate all data connected to the specified key from a JSON dataset

Looking at the json string presented below [{"_id":"9/17/2015","amt1":0,"amt2":13276.5},{"_id":"9/18/2015","amt1":8075,"amt2":6445.5}] The expected outcome is: [{"_id": ["9/17/2015", "9/18/2015"], "amt1": [0, 8075], "amt2": [13276.5, 6445.5]}] Is there ...

Can you explain the distinction between using useContext and Redux?

Can you explain the distinction between useContext and Redux? Is Redux similar to using useContext? Do I no longer require useContext when implementing Redux in my project? ...

Direct your attention to an input right after it has been generated

After clicking on a text input, I want to replace it with a password input. The code I have creates the new input, but it does not automatically have focus like I would like. Below is my current code snippet. $('.input_center input:text').click ...

HTML Error: The result is unspecified

I have written a function that is supposed to return 105; 6.5 and 110; 4.5, but instead it returns undefined: undefinedundefined: undefined. Can someone please advise me on what changes I need to make to get the correct results? I have heard about asynchro ...

Unable to retrieve the data through ajax GET due to a URL error

While attempting to pass an ID to the Quizz page and use AJAX to retrieve data when the page loads, I encountered an error message. HTTP404: NOT FOUND - The server did not find anything that matches the requested URI (Uniform Resource Identifier). (XHR)G ...

Ways to eliminate unnecessary spacing in CSS text-stroke

I am trying to achieve a text-stroke effect with transparent text over an image. However, I am facing an issue where the text-stroke also displays lines within the text itself. I am using the Montserrat font from Google Fonts. Can anyone provide assistance ...

Adding multiple classes with jQuery and activating them using other jQuery functions

How can I use jQuery to add an active class to menu elements that have multiple classes? The code I have tried so far is not working as expected: JQUERY $('#foo_menu li a').click(function(){ $('#foo_menu li a').removeClass(&apos ...

Sending information from an AngularJS selected item to an edit form

Currently, I am working on an add/edit form using angularJS. Within this project, there are two templates - one for displaying a list of items and another for the form itself. While adding new items works smoothly, I have encountered some challenges when i ...

I am currently working on developing an HTML and JavaScript audio player that can play audio at specific scheduled times

Looking to create a custom HTML/JavaScript audio player that adjusts playback based on the time of day. For instance, if it's 1:00 pm, the file will start playing from 0:00 minutes; and if it's 1:01 pm, the file will begin playing from 1:00 minut ...

Is there a way to apply a blur effect to just the div itself without affecting its contents

<style type="text/css"> .inner-container{ width:400px; height:400px; position:absolute; top:calc(50vh - 200px); left:calc(50vw - 200px); overflow:hidden; } #no-blur{ z-index: 20; } .box{ position:absolute; height: ...

Height of Hover Background Color in WordPress Menu

Greetings! Welcome to my website I'm in the process of modifying the hover color for the top menu, and I've managed to change it successfully. However, I would like the hover effect to cover the entire height of the menu, as right now it's ...

Apache conf file configured with CSP not functioning properly when serving PHP files

While configuring the Apache CSP lockdown for a site, I encountered an unusual behavior when opening the same file as a PHP script compared to opening it as an HTML file. The HTML file looks like this: <html> <head> <meta http-equiv= ...

What is the best way to place a button within a line of text

I am trying to achieve a layout similar to this: -------------------button------------------ I can add text inside the line, but it doesn't look good when I add a button. Any suggestions on how I can improve this? ...