Glowing CSS Animation - Time-Based Shine Effect

I am currently working on a button feature that can be toggled between activated and deactivated states. My goal is to incorporate a shine effect that will trigger every five seconds when the button is in the activated state. After doing some research, I came across this specific code snippet:

http://jsfiddle.net/AntonTrollback/nqQc7/

In order to implement this effect, I have modified the code to toggle the div surrounding my button with a class called "icon-effect" to create the desired shine effect. The idea is for this class to be toggled on briefly, then off after about a second, and repeat this process every five seconds. Essentially, I created a red box by customizing the div with certain properties and incorporated the icon effect class from the original fiddle. Here is a snippet of the changes I made:

<div id='my_button_div>
<div>

Additionally, I implemented a time function as follows:

setInterval(function () {
                $('#my_ button_div').toggleClass('icon-effect');
                setTimeout(function () {
                     $('#my_ button_div').toggleClass('icon-effect');
                }, 1000);
            }, 5000);

You can view the updated fiddle here, however, despite my efforts, I have been unable to make it work correctly. Although I retained the hover functionalities in the code, the main intention is for the shine effect to trigger at specified intervals.

Answer №1

Upon reviewing the jsFiddle you provided, a few issues were identified:

  1. The absence of jQuery importation is causing $ to remain undefined
  2. You are toggling the icon-effect class on the main element instead of a sub-element
  3. There is a space in the ID of the main element which causes JavaScript reference errors
  4. The .icon-effect sub-element is defined separately from the #my_button_div element and should be contained within it

Please find the updated jsFiddle following this link: http://jsfiddle.net/nqQc7/289/

Answer №2

You must address some coding errors before proceeding:

Replace the following code snippet:

<div id = 'my_button_div>
<div>

With this corrected version:

<div id="my_button_div"></div>

Correct the following code segment:

$('#my_ button_div').toggleClass('…');

To this updated format:

$('#my_button_div').toggleClass('…');

To seamlessly incorporate styles from the Gallagher/Coyier example, add class="icon" to <div id="my_button_div"> and nest

<span class="icon-effect"></span>
within it.

Here is the correct structure:

<div class="icon" id="my_button_div">
    <span class="icon-effect"></span>
</div>

Instead of utilizing the :hover pseudo-class, apply a special "triggered" class using jQuery.

The original CSS:

.icon:hover .icon-effect {
    opacity: 1;
    top: -30%;
    left: -30%;
}

Should now be modified to:

.icon.triggered .icon-effect {
    opacity: 1;
    top: -30%;
    left: -30%;
}

Also change the JS code:

$('#my_ button_div').toggleClass('icon-effect');

To:

$('#my_ button_div').toggleClass('triggered');

setInterval(function () {
    $('#my_button_div').toggleClass('triggered');
    setTimeout(function () {
        $('#my_button_div').toggleClass('triggered');
    }, 1000);
}, 5000);
/* Icon */

.icon {
  display: block;
  width: 32px;
  height: 32px;
  background: red;
    
  position: relative;
  overflow: hidden;
}

/* "shine" element */
/* Could be a pseudo element but they lack support for CSS transitions in some browsers */

.icon .icon-effect {
  position: absolute;
  top: -110%;
  left: -210%;
  width: 200%;
  height: 200%;

  opacity: 0;
  
  background: rgba(255, 255, 255, 0.2);
  background: -moz-linear-gradient(
    left,
    rgba(255, 255, 255, 0.1) 0%,
    rgba(255, 255, 255, 0.1) 75%,
    rgba(255, 255, 255, 0.5) 90%,
    rgba(255, 255, 255, 0.0) 100%
  );
  background: -webkit-linear-gradient(
    top,
    rgba(255, 255, 255, 0.1) 0%,
    rgba(255, 255, 255, 0.1) 75%,
    rgba(255, 255, 255, 0.5) 90%,
    rgba(255, 255, 255, 0.0) 100%
  );
  background: -webkit-gradient(
    linear, left top, right top,
    color-stop(0%  ,rgba(255, 255, 255, 0.2)),
    color-stop(75% ,rgba(255, 255, 255, 0.2)),
    color-stop(90% ,rgba(255, 255, 255, 0.8)),
    color-stop(100%,rgba(255, 255, 255, 0.0))
  );
  background: -o-linear-gradient(
    top,
    rgba(255, 255, 255, 0.1) 0%,
    rgba(255, 255, 255, 0.1) 75%,
    rgba(255, 255, 255, 0.5) 90%,
    rgba(255, 255, 255, 0.0) 100%
  );
  background: -ms-linear-gradient(
    top,
    rgba(255, 255, 255, 0.1) 0%,
    rgba(255, 255, 255, 0.1) 75%,
    rgba(255, 255, 255, 0.5) 90%,6
    rgba(255, 255, 255, 0.0) 100%
  );
  background: linear-gradient(
    top,
    rgba(255, 255, 255, 0.1) 0%,
    rgba(255, 255, 255, 0.1) 75%,
    rgba(255, 255, 255, 0.5) 90%,
    rgba(255, 255, 255, 0.0) 100%
  );

  -webkit-transform: rotate(30deg);
     -moz-transform: rotate(30deg);
      -ms-transform: rotate(30deg);
       -o-transform: rotate(30deg);
          transform: rotate(30deg);
    
  -webkit-transition-property: left, top, opacity;
     -moz-transition-property: left, top, opacity;
      -ms-transition-property: left, top, opacity;
       -o-transition-property: left, top, opacity;
          transition-property: left, top, opacity;
  -webkit-transition-duration: 0.5s, 0.5s, 0.1s;
     -moz-transition-duration: 0.5s, 0.5s, 0.1s;
      -ms-transition-duration: 0.5s, 0.5s, 0.1s;
       -o-transition-duration: 0.5s, 0.5s, 0.1s;
          transition-duration: 0.5s, 0.5s, 0.1s;
  -webkit-transition-timing-function: ease;
     -moz-transition-timing-function: ease;
      -ms-transition-timing-function: ease;
       -o-transition-timing-function: ease;
          transition-timing-function: ease;
}

/* Trigger effect */

.icon.triggered .icon-effect {
  opacity: 1;
  top: -30%;
  left: -30%;
}

/* Active state */

.icon:active .icon-effect {
  opacity: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="icon" id="my_button_div">
    <span class="icon-effect"></span>
</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

The art of deciphering a returned object in jQuery

Recently, I've been working with this jQuery code snippet: $.ajax({ type: "POST", url: "/problems/vote.php", dataType: "json", data: dataString, success: function ...

Is it possible to use regex for matching both spaces and letters exclusively?

I recently created a regular expression that only allows letters. While I'm not very experienced with regex, I am struggling to figure out how to also include spaces in my expression. This is the HTML code I have: <input id="input" /> Below i ...

Modify image on webpage using PHP every second

My website has a feature that dynamically changes the picture source and displays a new image using a combination of PHP and JQuery. Here is the PHP5 script I am using: <?php header("Content-Type: image/jpeg"); $page = $_SERVER['PHP_SEL ...

Tips for effectively downsizing an HTML Canvas to achieve high-quality images

Introduction I am currently facing issues with blurry visuals in my canvas animation, especially on devices with high pixel densities like mobile and retina screens. In order to address this problem, I have researched canvas down-scaling techniques and f ...

Next.js encountering page not found error due to broken link URL

Currently, I am working on implementing a login system in next.js. In the login page, I have included a Link to the Register page using the Link href attribute. However, every time I click on that link, it displays a message saying "404 page not found." Al ...

Tips for utilizing jQuery to substitute strings within a content variable?

$content = "Please locate the student results where Date of birth **IS BETWEEN** 2012-02-18 00:00:00 AND 2013-02-18 00:00:00 AND name **IS NOT EQUAL TO** 'John' AND marks **IS BETWEEN** 40 AND 75 AND grade **EQUAL TO** 'A' AND AGE **I ...

The combination of Chromium, raw HTML pulled directly from a C# string, and Angular leads to issues when trying to implement routing

CefSharp: 1.25.0 (Chromium 25.0.1364.152-based) Angular: 1.3.0-beta16 UIRouter: 0.2.10 Currently, I am in the process of creating a C# application that will function as a standalone program using CefSharp Chromium, Angular, and UIRouter for the graphical ...

Troubleshooting the issue of not successfully retrieving and sending values from form checkboxes in jQuery to $_POST variable

I am facing an issue with checkboxes that have identical names and use square brackets to create an array. <label> <input type="checkbox" value="Tlocrt objekta" name="dokument[]" > Tlocrt objekta </input> </label> ...

Utilize jQuery to deactivate all click interactions except for the scrollbar

I am currently working on creating a page that is completely unclickable, both with right and left clicks, and I want to display a message when someone attempts to click. This may lead to some questions such as "Why would anyone want to do this? This s ...

What are some ways to employ ul and li elements to create columns that are consistently aligned, even when some columns may be empty?

In my HTML document, I have multiple sections with unordered lists of items. In a specific ul section, the list looks like this: <ul> <li>item 1</li> <li>item 2</li> <li>ck-item 2</li> <li ...

Obtaining the date for the previous month using JavaScript or jQuery

I need to retrieve a specific date in JavaScript. My goal is to obtain the current date based on a variable named frequency, which can have values of Daily, Weekly, or Monthly. if(frequency=="daily") { date='current_date -2 days'; } e ...

Grabbing the HTML value and storing it in a JavaScript variable

In my table, each row has multiple columns including one with a checkbox. When this checkbox is checked, a new column cell appears to the right of the table with an up/down spinner to adjust a value. The issue I'm facing is that I want to limit the s ...

Learn the best way to efficiently transfer multiple checkbox selections in a single object using AJAX

In my form, I have 4 checkboxes with unique IDs like filter_AFFILIATION_1, filter_AFFILIATION_2, and so on up to 4. My goal is to dynamically send the values of checked checkboxes to the server using an ajax call. Below is the snippet of my code: $(&a ...

What is the best way to eliminate YouTube branding from a video embedded in a website?

I am currently utilizing an <iframe width="550" height="314" src="https://www.youtube.com/embed/vidid?modestbranding=1&amp;rel=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe> This setup removes the "YouTube" logo from the ...

Discovering inner arrays within an outer array using the Plus function

Apologies if these questions seem basic in terms of JavaScript. Challenge 1. I am attempting to write code that can identify an inner array within an outer array, and the structure of the array looks something like this; var array = ['Bob', [ ...

What is the syntax for invoking a method within a component in the OctoberCMS framework?

I'm not sure if this is even possible or if it's just a crazy idea, but I'm attempting to call a PHP method from an AJAX request using OctoberCMS Ajax Framework (which I believe utilizes jQuery in the background). Unfortunately, it doesn&apo ...

Find all relevant employee information at once without the need for iteration

I have structured two JSON arrays for employee personal and company details. By inputting a value in the field, I compare both tables and present the corresponding employees' personal and company information in a unified table. <html> ...

employing jQuery for invoking webMethod results in receiving HTML... webMethod doesn't get invoked

Trying to invoke a method on an ASPX page from VisualStudio 2008 using javascript and jQuery, but only getting back the HTML of the page. Even changing the method name in the javascript does not result in an error message indicating that the method cannot ...

Extracting unique text values from nested div elements within list items using Selenium with Python

Within the HTML code provided, I am looking to extract the text of three variables (descr_confezione, aic_farmaco, and stato_confezione) for each of the four list items: <ul id="ul_lista_confezioni"> <li style="display: list-item;"> &l ...

How have search results traditionally been organized?

I have been dealing with this issue for quite some time now. I am currently working on a page in Django/Python that includes a search function. After searching, users are directed to the results page. However, I am facing an issue: What are the various p ...