Is there a way to create animated CSS box-shadow depth using jQuery or CSS3 transitions?

This code snippet applies delays but doesn't seem to update the style changes until the loop completes:

for (i=20;i>=0;i--) {            
    var boxShadow = i+"px "+i+"px "+i+"px #888";
    $('article').css("box-shadow", boxShadow);
    sleep(20);
}
function sleep(ms)
{
    var dt = new Date();
    dt.setTime(dt.getTime() + ms);
    while (new Date().getTime() < dt.getTime());
}

The following code doesn't apply delays at all:

for (i=20;i>=0;i--) {            
    var boxShadow = i+"px "+i+"px "+i+"px #888";
    $('article').delay(500).css("box-shadow", boxShadow);
}

Is there a simpler way to achieve this using CSS3 transitions? Could it be that I am making a small jQuery mistake in the delay example?

I appreciate any assistance with this issue.

Answer №1

To achieve an animation effect, you can incorporate CSS3 transitions using classes and the setTimeout function:

CSS --

#some-element {
    -webkit-transition : all 0.5s linear;
       -moz-transition : all 0.5s linear;
        -ms-transition : all 0.5s linear;
         -o-transition : all 0.5s linear;
            transition : all 0.5s linear;
}
#some-element.ani-state {
    -webkit-box-shadow : 0 0 24px #000;
       -moz-box-shadow : 0 0 24px #000;
            box-shadow : 0 0 24px #000;
}

The use of all in the transition declarations allows for flexibility across different versions of Chrome which may utilize different properties such as -webkit-box-shadow or box-shadow.

JS --

$(function () {

    var $someElement = $('#some-element');

    $someElement.on('click', function () {
        $someElement.addClass('ani-state');
        setTimeout(function () {
            $someElement.removeClass('ani-state');
        }, 500);
    });
});

Check out this demo: http://jsfiddle.net/jasper/tvfPq/1/

Take note that the demo features two box-shadows:

box-shadow : 0 0 24px #000, inset 0 0 24px #999;

Answer №2

Check out this new jQuery plugin that has just been launched for accomplishing the same task: Shadowmation

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

Angular custom filter applied only when a button is clicked

I have recently started exploring angular custom filters and I am curious to know if there is a way to trigger the filters in an ng-repeat only upon clicking a button. Check out this example on jsfiddle : http://jsfiddle.net/toddmotto/53Xuk/ <div ...

Tips on retaining label names for input boxes

Check out this Plunker example. I am trying to figure out how to keep labels on top of textboxes in this Plunker. Can anyone help me with that? <a class="btn btn-success btn-xs" data-nodrag ng-click="toggle(this)"><span class="glyphicon" ng-clas ...

Erase the border around the color selection field

Here's a demonstration: http://jsfiddle.net/WpJRk/ I've incorporated a color picker into my webpage using the new input type "color": <input type="color"> However, I've noticed that there is an unwanted black border within the eleme ...

I am experiencing difficulty getting my component to properly implement the bootstrap NavBar and NavIcon styles

I am new to using bootstrap with react and I am facing an issue where the dimensions are not being applied to my Navbar and Navbar Icon. I have already installed dependencies and used className instead of class, but still no changes are visible. import Re ...

Algorithm for detecting collisions in Javascript

I am looking to implement a collision detection function in JavaScript for canvas. Specifically, I have coin and piggy bank objects and want to create a function that triggers the disappearance of a coin object when it comes into contact with a piggy bank. ...

What are alternative methods for creating a table layout without relying on traditional table elements?

Is there a way to eliminate tables from my code and achieve the same layout in the name of progress and learning? Here is an example of the table I currently have: <table cellspacing="0px"> <tr> <td> <img src= ...

`Express.js Controllers: The Key to Context Binding`

I'm currently working on a project in Express.js that involves a UserController class with methods like getAllUsers and findUserById. When using these methods in my User router, I have to bind each method when creating an instance of the UserControlle ...

How to serialize an Asp net core resx file and send it back in an ajax request

I have a project in which I am heavily using jQuery. I am curious if it is possible to retrieve the appropriate .resx file (in JSON format) based on the current language through an AJAX call. This way, I can utilize these translations throughout my website ...

``There seems to be an issue with the functionality of Angular's $routeProvider

I'm currently facing an issue with my setup. I have a local angular front-end running on localhost using an Apache Server on Linux. When I try to access localhost, everything works fine and I can see my index.html. However, I have a link in the index. ...

Troubleshooting a WordPress Blog Homepage CSS Dilemma

My goal is to keep the featured image size consistent on the main blog page. I made adjustments to this code: HTML: <div class="featured-image-excerpt"> <img class="attachment-post-thumbnail size-post-thumbnail wp-post-image" src="http://mrod ...

Creating custom styles with makeStyles in Material UI

How can I properly write the following CSS value as a string in a makeStyles function within Material UI? background: linear-gradient(rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0.8)), url(../assets/pexels-pixabay-41949.jpg), I attempted to do it this way but i ...

How can you use JavaScript to identify resize or zoom changes across all ancestor DOM elements?

I have a DOM element that consists of a chart. <div id="plot1" class='plot-chart'></div> This specific DIV can be nested within various other DIVs. <div id="one" style="width:100%;height:100%;zoom:0.9;"> <div id="two"& ...

Tips for delaying page rendering until an AJAX call finishes

While AJAX calls are known for allowing the rest of a page to load before a certain element is ready, I have a unique business requirement that complicates things. I am mandated to use AJAX due to the architecture in place. However, I cannot give the appe ...

Prevent JavaScript from loading when using jQuery's .html() method

I am currently implementing smoothState.js to prefetch a page via ajax. However, when the html is updated with the new content, it reloads the javascript file as well. As a result, the page ends up running both instances simultaneously, leading to errors. ...

Having issues with closing a div tag using $.after() function

This issue can be better understood with an example: http://jsbin.com/lavonexuse The challenge here is to insert a full-width row after a specific column (identified by the class .insertion-point) when "Insert Row" is clicked. The problem I'm facing ...

Handling scroll events in a functional component using React

I'm having trouble understanding why the onScroll function isn't triggering the console.log statement. <table className="table" onScroll={()=>{console.log("Works")}> The console.log just doesn't seem to be exec ...

React components are failing to display data as expected

I need to display certain data based on the id provided in the url. When I use console.log() with res.json, I can see the data but I'm unsure how to pass it to the 'articleComponent'. const Articles = () => { const query = (id) => ...

Expanding the background color of a Div within a Grid System

Note: Utilizing the skeleton grid system. I'm trying to expand the background color of a specific div beyond the 960px container, but I'm facing some difficulties. Any ideas? Current: Desired outcome: HTML: <!DOCTYPE html> <!--[if l ...

Running JavaScript function from AJAX response containing both HTML and JavaScript code

For my first time using AJAX to prevent page refresh upon form submission, everything works flawlessly. The data is received in HTML form and placed into the designated div. However, I am encountering an issue with one of the JavaScript functions responsib ...

Combining Multiple Pie Charts with a Line Chart in Highcharts

Can anyone provide guidance on creating a chart similar to the one shown in the Highcharts library? https://i.sstatic.net/BoX4i.jpg ...