Ways to make all elements appear darker with the exception of one specific element

I am currently developing a Google Chrome extension and working on the right side, specifically in my friend's stories section. I am looking for a particular friend's story by searching for their name entered in the input field after clicking the button. I want to dim or darken all elements within the black border in the picture except for the one that matches the search until the user presses the X button.

I am struggling to figure out how to target all other elements except for one! :(

Here is the picture:

Answer №1

My treasure trove of code has been discovered! Behold as a canvas is summoned forth on the screen, meticulously carving out the area surrounding the chosen element. All it takes is to beckon openOverlay(elem), where elem symbolizes the very object you wish to illuminate.

var padding = 5;  //A buffer of space around the canvas
var animDelay = 250;  //Time delay utilized in CSS animation and transition for canvas.highlight

var divs = document.getElementsByTagName("div");

for (var i = 0; i < divs.length; i++) {
  divs[i].addEventListener("click", function() {
    openOverlay(this);
  }, true);
}

function openOverlay(elem) {
  var loc = elem.getBoundingClientRect();
  var canvas = document.createElement("canvas");
  canvas.className = "highlight";
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
  var ctx = canvas.getContext("2d");
  ctx.fillRect(0, 0, canvas.width, canvas.height);
  ctx.clearRect(loc.left - padding, loc.top - padding, loc.width + padding * 2, loc.height + padding * 2);
  document.body.appendChild(canvas);
  window.overlayCanvas = canvas;
  canvas.onclick = closeOverlay;
}

function closeOverlay() {
  delete window.overlayCanvas;
  this.style.opacity = 0;
  var self = this;
  setTimeout(function() {
    self.parentNode.removeChild(self);
  }, animDelay);
}

//Hit X to exit?
window.addEventListener("keypress", function(e) {
  //120 denotes X
  if (e.which === 120 && window.overlayCanvas) closeOverlay.call(overlayCanvas);
}, false);
/* Vital Component */
canvas.highlight {
  position: absolute;
  display: block;
  top: 0;
  left: 0;
  opacity: 0.5;
  -webkit-transition: opacity 250ms ease;
  transition: opacity 250ms ease;
  -webkit-animation: canvasFade 250ms ease;
  animation: canvasFade 250ms ease;
}
@-webkit-keyframes canvasFade {
  from { opacity: 0; }
  to { opacity: 0.5; }
}
@keyframes canvasFade {
  from { opacity: 0; }
  to { opacity: 0.5; }
}

/* Miscellaneous Styling */
div {
  margin: 10px;
  width: 100px;
  height: 100px;
  display: inline-block;
  background: green;
  cursor: pointer;
}
div.funky {
  padding: 3px 8px;
  margin-top: 5.2px;
  border-bottom: 10px solid red;
}
<div>One</div>
<div>One</div>
<div class="funky">One</div>
<div>One</div>

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

Two neighboring sections containing dynamic elements. One section automatically adjusts its size to fit the content, while the other allows

I need to display 2 adjacent boxes with dynamic content rendered using Angular. The Container should have the same height as Box1. The height of Box2 may vary due to its dynamic nature, but it should never be taller than Box1. If it does become higher, a s ...

jQuery Mobile: issue with positioning an external panel on the right side

I am looking to incorporate an external header and two external panels, one on the left and one on the right. The challenge is to ensure that these panels remain visible at all times on larger screens. I encountered some difficulties positioning the panel ...

Best practice for displaying image data from PHP for use in Jquery

Struggling with a PHP function called by a jQuery function to display an image on a webpage. The problem seems to be in sending the data back to the jQuery function correctly. Here's how the image is retrieved: if(mysql_query("insert into Personal_P ...

Enhance and expand an array that is deeply nested within an object

My Data Structure Example: var tree = { name: "docs", type: "dir", full: "/home/docs", children: [ { name: "folder2", type: "dir", full: "/home/docs/folder2", c ...

Creating a list in React and adding a delay using setTimeout() method

I'm a beginner when it comes to working with React and I've been attempting to display a list of posts using a JSON array. My goal is to have the list render after a certain number of seconds, but for some reason, the list isn't rendering us ...

Having trouble getting an HTML form to function with Ajax and PHP?

Seeking assistance from anyone who can lend a hand. I am delving into the complexities of Ajax, and I'm encountering issues where it seems like the script is being completely ignored or perhaps I'm just making a rookie mistake. Prior to display ...

What is the best way to expand my navigation bar to fill the entire width of my div?

A big shoutout to @seva.rubbo for fixing the issue! Check out the updated code on JSFiddle I recently designed a layout with a fixed width div of width: 900px;. I centered this div, leaving blank spaces on either side. Now, I'm looking to add a navig ...

Tips for expanding the boundary of a Font Awesome icon

The image is in a h1 editing margin of h1 only edits margin-left increases the margin on the left of the image. trying to increase margin of i element does nothing either on i element does nothing either. <h1 class="display-4"><i class="fas fa- ...

Guide for manually initiating the mouseleave event on a smartphone or tablet

Is there a way to change the color of a link to orange when it's hovered over? On mobile devices, the link should turn orange when touched and stay that way until the user clicks away. I'd like to manually trigger the mouseout event to remove th ...

Scrolling horizontally using the WinJS list view

In my project, I incorporated a WinJS list view with the display style of ms-flexbox. However, I am facing an issue where the list is scrolling horizontally beyond its width even though I have set overflow to hidden. Is there a way for me to eliminate th ...

How can we develop a NodeJS function that returns a query result using a callback method?

I'm currently working on verifying the proper operation of a database using mocha/chai in NodeJS. I am looking for a solution to run a SQL query and then validate its execution. The issue I'm facing is that when I reach the assertion, the result ...

Error encountered while using jQuery Ajax - unable to properly handle the error

Currently, I am having issues with my ajax query when trying to retrieve the login status. The function seems to be malfunctioning. function getdetails(playlistname) { alert(playlistname); $.ajax({ type: "POST", url: "model/login_details1.php", ...

Searching for a specific element using Python and Selenium

Is there a way to locate an element using Selenium even if the XPath changes occasionally? Here is the code snippet: <input type="submit" value="Convert"> ...

The anchor tag <a> is configured for inline display but is unexpectedly occupying the entire space

I am currently incorporating Bootstrap into my HTML, however, the <a> tag is behaving like a block display despite the fact that I have specified the CSS to be display:inline; <!doctype html> <html> <head> <link rel="styleshee ...

When attempting to make a JSONP call to Instagram's website, an error was encountered stating "Uncaught Syntax

I'm attempting to fetch user information from this Instagram website: . To avoid CORS issues, I am using ajax with JSONP instead of JSON. Here is my approach based on the response to a related question: <script> $(document).ready(function( ...

What is the method for shifting a value within an array using Jquery?

Looking to prioritize my name in a list of online chat members but unsure how to achieve the shortest method possible. Here is my JQuery code snippet: // Sorting Online Members List with My Name at the Top var list = [ {"userid":"19380","username":"User ...

What is the process for loading a .x model in Three.js?

Could anyone provide guidance on how to successfully load an animated .x model in Three.js? I am currently struggling with the process and have attempted to convert the .x model to collada without success. Unfortunately, my search for a free program to c ...

Personalized tooltip for React ChartJs

I am currently facing an issue with creating a custom tooltip using the react-chartjs-2 library. Whenever I hover over the chart and change the state of the tooltip's future data, my chart rerenders. At the moment, there is no tooltip present; I am on ...

If someone installs our chat widget on their website using an <iframe> script, I would like the widget to be deactivated when our website is experiencing downtime

We utilize an iframe to create our Chat Widget. One issue we face is that when our main website is down, it causes errors on client websites where the widget is embedded. I am looking for a way to automatically disable the "Chat widget" when our website ...

Tips on passing a variable into a controller using jQuery AJAX from the URL

Can you help me figure out how to pass an id to a function in the controller? I keep getting an error saying it cannot find the method in the controller. Here's my view: function Delete1(id) { if (confirm("Are you sure?")) { $. ...