Achieve a seamless look by ensuring shadows are positioned behind every element

Need help with creating dynamic shadows on divs. Here is what I have so far:

HTML


  <div id="wrapper">
   <div id="div1" class="castsshadow">Div 1</div>
   <div id="div2" class="castsshadow">Div 2</div>
   <div id="div3" class="castsshadow">Div 3</div>
   <div id="div4" class="castsshadow">Div 4</div>
   <div id="div5" class="castsshadow">Div 5</div>
  </div>

CSS


#wrapper
{
    width: 960px;
    margin: 10px auto;
    outline: 1px solid black;
    padding: 5px;
}

.castsshadow
{
    position: relative;
    display: inline-block;
    border: 1px solid #50535C;
    background-color: white;
    width: 300px;
    height: 100px;
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    border-radius: 20px;
    overflow: hidden;
    padding: 10px;
}

Javascript/JQuery


$(document).ready(function(){

 $(document).mousemove(function(e){

  $(".castsshadow").each(function(i) {
   var offset = $(this).offset();
   var width = $(this).width();
   var height = $(this).height();
   $(this).html(
    "X Axis : " + e.pageX
    + " Y Axis : " + e.pageY + "<br>"
    + "X Offset : " + offset.left
    + " Y Offset : " + offset.top + "<br>"
    + "Height : " + height
    + " Width : " + width + "<br>"
    + "X Center : " + (offset.left + (width/2))
    + " Y Center : " + (offset.top + (height/2))
   );
   $(this).css({
    boxShadow: -(e.pageX-(offset.left+(width/2)))/10 + "px " + (-(e.pageY-(offset.top+(height/2)))/10) + "px 10px -10px #404040"
   });
  });

 });

});

Fiddle here: http://jsfiddle.net/feY6C/

Looking for a solution to prevent the shadows from overlapping other divs. Any suggestions?

Answer №1

If you want to add more divs, you'll need to create a new one for each existing div. For instance, duplicate what you already have and apply shadows to one set while adding content to the other set. Using JavaScript can help make this cloning process dynamic. I will provide you with a JSFiddle shortly.

UPDATE

Check out this example that illustrates my point: http://jsfiddle.net/feY6C/7/

Please note that I had to disable your JS because it was interfering with my child divs (I will fix it soon, but wanted to showcase my concept first - I included a shadow effect -15px 0) and that I removed the positioning from the parent divs. Each individual div now looks something like this:

<div id="div1" class="castsshadow"><div>Div 1</div></div>

And here are the corresponding CSS rules:

.castsshadow div {
z-index: 2;
position: absolute;
background: white;
width: inherit;
height: inherit;
border-radius: 20px;
margin: -10px;
padding: 10px;
}

Take a look at the updated version with live JS: http://jsfiddle.net/feY6C/31/

In summary, I enclosed each div's content within a child div, positioned it absolutely based on the parent's dimensions, and gave it a higher z-index. These divs are generated dynamically, eliminating any concerns about placement issues.

Answer №2

Unfortunately, I'm unable to provide a detailed explanation at this time; however, you could assign the div a z-index that is determined by the distance from the cursor to the center of the div.

Divs closest to the cursor would have lower z-index values, while those further away would have higher z-index values.

(An example formula: distance in pixels / 20 - to keep z-index values within a reasonable range)

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

The website at 'https://example.com' was successfully loaded using HTTPS, but there is a request for an insecure XMLHttpRequest Endpoint

Mixed Content Warning: The webpage at '' loaded securely via HTTPS, but attempted to access an insecure XMLHttpRequest endpoint ''. This request has been blocked as it needs to be served over a secure connection (HTTPS). ...

What is the best way to place a header in a specific location on a

As I continue working on my project, I am facing a challenge in positioning the headings in specific places. My ultimate goal is to achieve a look similar to Figure 1 for my headings. [Figure 1][1] However, as of now, my layout resembles Figure 2. I find ...

Update the class of the element that is currently selected using jQuery and the `this` keyword

Is there a way to change the class on hover only for the current element using 'this'? The code I have changes classes for all elements, but I need it to work individually. Here is the code snippet I'm currently using: https://codepen.io/ky ...

Enhancing my function with JQuery

One of my clients utilizes a WYSIWYG editor to compose new web pages. However, a common issue arises as the WYSIWYG editor sometimes automatically inserts empty p tags like this: <p>&nbsp;</p> This causes layout problems, so I developed a ...

Ensuring React's setupProxy functions properly in conjunction with express.js

I've encountered an issue while trying to pass images from express to react. My express.static is correctly set up, so when I visit localhost:5000/public/images/me.jpeg, the image loads in my browser. However, when I attempt to use <img src="/publi ...

Removing numerous columns from an array on the fly

Currently, I am exploring ways to eliminate multiple columns from an array of objects based on a dynamically selected list of values. If my goal is to delete a specific column, the approach would be as follows: Given the array: arr = [ { "THEDAT ...

JavaScript regular expressions: filtering out results from the output

I'm trying to extract a specific number from a dynamically added string. The text looks like this: nisi non text600 elit, where 600 is the number I need to retrieve. Is there a way to achieve this without replacing the word text? var str = ('nis ...

Flask not serving Favicon and images to a React application

I am currently working with a Flask server and have it set up in the following manner: app = Flask(__name__, static_folder="dist/assets", static_url_path='/assets', template_folder="dist") ...

Text-indent is the clever trick of shifting elements (specifically anchor tags) out of view on

I'm facing an issue with my navigation list that uses images instead of text. It seems like the images have moved to the background, making it difficult for me to fix. I want the images to be clickable and redirect me to another page, but I'm qui ...

What's the best way to refresh the pagination links during an AJAX request?

After successfully implementing AJAX pagination, I noticed that the pagination links are not updating because the <%= paginate @videos %> code is not located within the rendered partial. Can anyone suggest a jQuery solution to update the pagination l ...

What could be the reason for my post container not functioning properly?

I am facing an issue with my HTML and CSS code. The container is not displaying as expected, specifically the green bar that should appear below the image. I suspect that the container's auto attribute is causing it to overlook the content inside. Can ...

.NET MVC - Preventing Ajax.BeginForm from being compromised

I am facing security issues with an Ajax-based form in my .Net MVC Web Application. Despite implementing Google reCaptcha, the form is still vulnerable to hacks and spam messages. @using (Ajax.BeginForm("ContactSend", "Home", null, new ...

Is there a way to intercept and control mousewheel scrolling before it occurs?

I am in search of a way to intercept the event triggered by a mousewheel scroll right before it occurs, in order to prevent the scroll action, execute certain tasks, and then allow the user to regain control. Currently, I have the following code set up to ...

Embed the anchor tag within a JQuery JSON structure

I am using jQuery syntax to retrieve a JSON list from a database. Here is the code snippet: $(document).ready(function () { //function to fetch user-input result $("#btnsearch").click(function() { $("#posting").html(""); //showing ...

Transform HTML into a Word document format

When using word automation to generate a word document, how can I convert HTML tags to the word document format while preserving styles such as font, bold, tables, and other elements? ...

A conflict with the Ajax file is causing an automatic logout

In my Rails application, there is a page with a table that uses partial AJAX to increase the capacity attribute in a time entity. You can view the visual representation of the table here. By clicking the plus/minus button, the capacity attribute increases ...

Customizing the appearance of a Vue.js Single Page Application with

I have a custom-built Vue.js single page application and I am interested in implementing user-switchable dynamic themes. I envision a scenario where users can easily toggle between 'light' and 'dark' themes by clicking a button, and hav ...

Attempting to create a custom web user interface tree from the ground up

I noticed that yahoo ui uses div for each node in the tree structure. Is this the best practice, considering that every node, even the leaf nodes, are heavily nested with div, table, tr, and td elements? Is there a more efficient way to achieve the same re ...

How to reduce the default width of Bootstrap carousel components

Click here for the 1st image. To view the 2nd image, click here. This represents the carousel code used on this particular website: <div class="row testing"> <div id="carouselExampleMen" class="carousel carousel-dark s ...

Implementing Dynamic Checkbox Selection using JavaScript

Could someone please assist me with this coding challenge? HTML <div id="toggle"> <ul> <li class="active"><input type="checkbox" id="test" value="2014" name="test" checked/> 2014</li> <div style="display:block;" id ...