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

Ajax Complete adds Jquery two times in a row

I have a simple ajax complete call that is designed to add some text after an ajax load finishes. However, I'm encountering an issue where the information is sometimes displayed multiple times. I suspect there might be something missing in my approach ...

I'm having trouble getting my HTML to scroll properly

Apologies for asking a common question, but I'm facing a specific issue with scrolling that I can't seem to figure out. Below is the code snippet: * { font-family: Arial, Helvetica, sans-serif; text-align: center; font-size: 40px; } < ...

What is the best way to tally data-ids within an anchor tag using jQuery or plain JavaScript?

find total number of data-id attributes within tag a I am looking for a way to calculate the count of my id attribute, whether it is in data-id or another form, using JavaScript. Edit ...

What approach can be taken to establish a dependency between an AngularJS controller and a value that is retrieved through ajax and loaded onto the root

I have an app that loads like this: app.js file: angular.module('App', []).run(['$rootScope', '$q', 'SessionManager', 'EndpointService', function ($rootScope, $q, SessionManager, EndpointService) { $r ...

Coordinating multiple API requests for optimal performance

I have a task where I need to retrieve data from two different API endpoints. Once both sets of data are fetched, I need to compare the information obtained from each source. I am familiar with fetching data from a single API endpoint and using a callback ...

Navigating through objects within arrays within objects in Angular

I seem to be encountering some difficulty in displaying data from an API call on Mapbox. Only one marker is showing up on the map instead of the expected 10 markers. I suspect there might be an issue with my ng-repeat implementation, but I can't pinpo ...

Eliminate numerous items from an array containing objects

Looking for a way to remove specific objects from an array of objects within another object. I want to delete multiple items simultaneously, but when attempting with splice, it shows up as undefined. This is the snippet of code I'm working with: ...

The call signatures for `node-fetch -- typeof import("[...]/node-fetch/index")'` are not properly defined

Originated from this source: https://github.com/node-fetch/node-fetch#json ... my personal code: const fetch = require('node-fetch'); async function doFetch() { const response = await fetch('https://api.github.com/users/github'); ...

simulated xhr server along with the locales in polymer appLocalizeBehavior

Currently, I am in the process of developing a web frontend utilizing Polymer. Within my web component, I incorporate various other components such as paper-input or custom web components. To facilitate testing for demonstration purposes, I have integrated ...

Recursive instruction malfunctioning

I'm currently trying to develop a custom recursion directive, but unfortunately it's not functioning as expected. I have followed the instructions outlined here: Recursion in Angular directives For reference, you can view the fiddle here: http ...

Searching for entries using an array of _id along with other possible column values

const query_id = [1,2,3,4,5,6]; const query_type = "A"; let queries = await Query.find({ _id: query_id, query_type: query_type }); The current code functions as intended, but there may be a better and more elegant way to achieve t ...

Unusual outcome observed when inputting a random number into HTML using JavaScript

In my HTML file, I am trying to input a number within 50% of a target level into the "Attribute" field. Here is the code: <!DOCTYPE html> <html> <body> <input type = "number" name = "playerLevel" onchan ...

Rails offers a unique hybrid approach that falls between Ember and traditional JavaScript responses

My current project is a standard rails application that has primarily utilized HTML without any AJAX. However, I am planning to gradually incorporate "remote" links and support for JS responses to improve the user experience. While I acknowledge that gener ...

Is there a way to merge two functions that are triggered by the same class name?

Whenever I click on an item inside my jQuery datatable, a function is triggered. $('.table tbody').on( 'click', '.item', function (e) { alert("clicked"); }); There is also another item with the same class outside the data ...

Alter the color of Material UI Raised Button when it is hovered over

I am trying to change the background color of a raised button on hover in Material UI. I attempted the following, but it did not work. Is there a way to modify the background color in Material UI for a raised button? Example.JS <RaisedButton ...

Experiencing issues with ng-repeat in AngularJs?

I am currently facing an issue with two tables that are rendering data through AngularJS from two separate C# methods. Both tables have almost identical structures, with the first one being used as a search field and the second one to display names. The pr ...

Accessing composable variables in Vue 3 without having to redefine refs from within a function

Currently, I am implementing a parent companyList component along with a reusable Table component and an useFetch composable in vue 3.2. Prior to integrating the Table component, my code looked like this: companyList <script setup> import { com ...

webdriverIO encountered an unhandled promise rejection, resulting in a NoSuchSessionError with the message "invalid session id

I am currently learning how to conduct UI testing using Jasmine and WebdriverIO in conjunction with NodeJS. Below is a snippet of my test code: const projectsPage = require('../../lib/pages/projects.page'); const by = require('selenium-we ...

What is the correct way to specify the type in my functional component?

I was trying to define the type in my component in a different way, I know it can be done using classes but is there a way to achieve this with functional components without exporting the interface? Despite my extensive research, I couldn't find any r ...

Tips for retrieving information from a highstock chart

Imagine I have a sample highstock chart on my website, similar to the one at this link. Is there a way to extract the data from the chart itself, even if the data used for creating the chart is not accessible to others? <img src="http://www.highchart ...