Need Help Adding An Additional Callback Function - javascript

Using Unveil (https://luis-almeida.github.io/unveil/) a lightweight js lazy load plugin.
The site features an example of a callback. I am using it just fine, but would like to add an additional animation to the opacity.
I am not a coder (front end designer) so all that I've tried is basically just a hack and slash attempt at adding another 'this.style' line.
The current script is as follows:
$("img").unveil(200, function() {
$(this).on("load", function() {
this.style.opacity = 1;
});
});
I am hoping someone can show me how to add another call to load another CSS effect to the above script yet keep the opacity as well. I would like to add the following CSS animation alongside the opacity that's currently being used.
img {
animation: 1s ease-out 0s 1 slideIn;
}
#keyframes slideIn {
0% {
transform: translateY(-10%);
}
100% {
transform: translateY(0);
}
}

To add a new CSS effect, you don't need to add a new callback. You can add all CSS effects right after the other. All the effects that you give will run one by one in a sequential manner.
You could do something like this:
$("img").unveil(200, function() {
$(this).on("load", function() {
this.style.opacity = 1;
this.style.animation = "1s ease-out 0s 1 slideIn"
});
});

Related

jQuery .fadeOut method on and element that is being appended after DOM load

(I am 9 weeks into a boot camp, so I apologize for the potentially rudimentary nature of this...)
I am appending an element to the DOM (a button) within a conditional:
$('.buttonsAndInputs').append(`<button id="clearHistoryButton">Clear All</button>`);
When this button is clicked, it runs through a series of functions to empty an array and clear some other content off the DOM. I would like to use the .fadeOut method of jQuery to remove THE BUTTON.
I have this in a subsequent function:
$('#clearHistoryButton').remove();
I would like to:
$('#clearHistoryButton').fadeOut(1000);
...so that it disappears in a fancy fashion.
It's not working - it simply waits one second and then - POOF - is gone.
This is my first question. This community has been ESSENTIAL in my growth in this realm and, as always, I appreciate all of you so very much.
Did you try transition: opacity 1s in your CSS ?
Advantage:
Hardware accelerated (GPU), i.e. it doesn't bother your main processor (CPU) with this task, whereas jQuery's fadeOut() function is software based and does take CPU resources for that effect.
Steps:
Add transition: opacity 1s to your CSS rules of the desired button element
here: ( #clearHistoryButton )
Add a CSS rule with button.fadeMeOut with opacity: 0
Add a simple jQuery function to add the class ".fadeMeOut" at click
Then remove button with setTimeout(function(){$('#clearHistoryButton').remove()},1000)
Run code snippet
$(function() { // Shorthand for $( document ).ready()
$("#clearHistoryButton").on( "click", function() {
// first: fade out the button with CSS
$(this).addClass("fadeMeOut");
// then: after fadeOut effect is complete, remove button from your DOM
setTimeout(function(){
$('#clearHistoryButton').remove();
},1000);
});
});
button {
opacity: 1;
-webkit-transition: opacity 1s;
-moz-transition: opacity 1s;
transition: opacity 1s;
}
button.fadeMeOut {
opacity: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="clearHistoryButton">Press to hide me</button>

My css animation and JQuery are not syncing up

I'm building a simple blinking div, it blinks every 3s:
.timer { opacity: 1; animation: blink 3s alternate infinite; }
#keyframes blink { to {opacity: .25;} }
and it's text content is updated every 3s as well, counting down from 50 to 0:
var number = 50;
window.setInterval(function countdown(){ $('.timer').text(number--); }, 3000);
However for some reason my animation and JQ slowly become out of sync, you'll notice it after a few animations have run through. I'm wondering how I could go about assuring that they keep in time with each other.
JS Fiddle:
https://jsfiddle.net/rourkemclaren/61ry4s1j/3/
Thanks in advance.
The issue is related to the CSS alternate flag. If you remove it the effect becomes more streamlined.
Note that you can also listen to the CSS animation events using JavaScript instead of using the setInterval:
$('.timer').on('animationiteration', function() {
$(this).text(--number);
});

Remove an element whilst enter animation is still running

I have an animation which runs on any new item to a grid. Lets say this animation takes 5 seconds to run. Currently, if I try removing that element within the 5 seconds (so whilst the enter animation is still running), the item remains in the list until the enter animation finishes.
Looking at the docs, it says that this is by design:
You'll notice that when you try to remove an item
ReactCSSTransitionGroup keeps it in the DOM. If you're using an
unminified build of React with add-ons you'll see a warning that React
was expecting an animation or transition to occur. That's because
ReactCSSTransitionGroup keeps your DOM elements on the page until the
animation completes.
It ways that you need to add the following (updated to the relevant class names obviously) and it should work for the case described above:
.example-leave {
opacity: 1;
transition: opacity .5s ease-in;
}
.example-leave.example-leave-active {
opacity: 0.01;
}
I'm not finding this to be the case, even though I have the described leave classes, I'm finding that it is still waiting for the original enter animation to finish, is this behavior correct, how do I fix this?
Here is a video showing the quirk in question - https://www.youtube.com/watch?v=3oKerWlLZIE
If it makes a difference here is my classes:
.request-summary-item-holder-enter {
background-color: #F8F5EC;
transition: background-color 5s ease-in;
}
.request-summary-item-holder-enter.request-summary-item-holder-enter-active {
background-color: transparent;
}
.request-summary-item-holder-leave {
opacity: 1;
transition: opacity 0.05s ease-in;
}
.request-summary-item-holder-leave.request-summary-item-holder-leave-active {
opacity: 0.01;
}
Update:
Source code references:
Setting the state - https://github.com/avanderhoorn/Glimpse.Client.Prototype/blob/master/src/request/components/request-summary-view.jsx#L33
Usage of transition group and setting keys - https://github.com/avanderhoorn/Glimpse.Client.Prototype/blob/master/src/request/components/request-summary-list-view.jsx

javascript: pulse text using fade in and fade out?

Hi i am trying to get a piece of text to fade in and out automatically without a click or hover method.
i'm not sure i can use an on page load function either because the text i want to fade in and out only appears after a user clicks to open another div on the page.? (I may be wrong?)
I am using this script but the text is static and not doing anything. Can anyone advise me where i'm going wrong? Thanks
<script>
function cycle() {
$('#morebutton-pics').fadeOut(1000, function() {
$('#morebutton-pics').fadeIn(1000);
cycle();
});
}
$(document).click(function(){
cycle();
});
</script>
This sounds like it would be better as a CSS animation:
#morebutton-pics {
animation: pulse 1s alternate infinite;
-webkit-animation: pulse 1s alternate infinite;
}
#keyframes pulse {
from {opacity:1}
to {opacity:0}
}
#-webkit-keyframes pulse {
from {opacity:1}
to {opacity:0}
}
This will work in Firefox, Chrome and IE10. Since this is more eye-candy than a required feature, that seems acceptable to me.
Notwithstanding that CSS is probably the better answer, your code doesn't work because it repeatedly calls cycle() without ever waiting for the second animation to finish.
function cycle() {
var $el = $('#morebutton-pics');
$el.fadeOut(1000, function() {
$el.fadeIn(1000, cycle); // loop in the second callback
});
}
replace
$(document).click(function(){
cycle();
});
with
$(document).ready(function() {cycle(); });
if the div is hidden it does not matters

How does Google achieve the fading effect on the home page?

If you go to google.com, you notice the menu on top slowly appears once you have mouse over the page. I was wondering what does Google use to control the fading effect?
[edit] since I don't use jQuery, I don't want to include it just to use this feature
There are two ways.
Javascript
Works in most browsers.
Gradually change the CSS opacity attribute of an element using Javascript. That's easiest with a good framework like jQuery, but is quite simple to do yourself.
function fadeIn() {
var element = document.getElementById("someID");
var newOpacity = element.style.opacity + 0.05;
element.style.opacity = newOpacity;
if (newOpacity < 1) {
window.setTimeout(fadeIn, 50);
}
}
Pure CSS
Only supported in Webkit at the moment.
#someID {
opacity:0;
-webkit-transition: opacity 1s linear;
}
#someID:hover {
opacity:1;
}
For an example have a look at the Surfin' Safari blog.
You could use jQuery and add an onmousemove callback on the tag that fades a hidden div with id "mymenu" in, something like:
$("html").one("mousemove", function() {
$("#mymenu").fadeIn("slow")
});
Warning: this was typed here, so I dunno if it compiles ootb.
I've never looked at it, but it's only logical to assume that there's a timer that gets started at load time for the page, and that adjusts either the alpha for the specified element or the opacity of another element that overlays it, in that element's CSS. Every timer tick, the numbers get turned up/down a little and the text becomes a bit more legible. When full visibility is reached, the timer is turned off.
JQuery is a finished, ready to use implementation of this in a cross-platform compatible package. You just add it, stir it up and it's done.
If you choose not to take the advice of the other answers, you'll have to research and implement the strategy from my top paragraph on your own. Good luck!
This is actually a rather complex thing to do because of the cross browser differences. The basics are something like the following:
Get the current opactity of the element as float.
Determine the ending opacity as float.
Determine your rate speed - i dont know what this should be in raw terms - somthing like .01/ms maybe?
Use a setInterval to fire a function that increases the opacity by your rate where: setInterval(function(){myElement.style.opacity = parseFloat(myElement.style.opacity)+0.01;}, 1); Somewhere in ther though you need to check if youve reached the endpoint of your animation and shutdown your interval.
I would think that they set the initial opacity of the elements other than the search box to zero. When the mouseover event is fired, the elements' opacity is gradually increased to 1.
Edit: In code it would look something like this:
var hiddenStuff = document.getElementByClassName("hiddenStuff");
var interval=document.setInterval(function() {
for (var i=0; i<hiddenStuff.length;i++){
hiddenStuff[i].style.opacity+=0.1
}
if (hiddenStuff[1].style.opacity===1){
window.clearInterval(interval);
}
}, 100);
You may need to tweak the parameters to get a smooth animation.
#Georg, that example works on Firefox 3.5 too. :-)
Demo: PURE CSS http://jsfiddle.net/6QS2a/1/
</div>
css:
.item {
height:150px;
width:150px;
border-radius:100%;
background:skyblue;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
opacity:0.2;
}
.item:hover {
opacity: 1;
}

Categories