Can we do fade in and fade out in iframes - javascript
Is it possible to make fade in and fade out transitions in iframes?
Fading in or out can be achieved by changing the opacity of an element over time, a very simple example:
var iframe = document.getElementById('iframe');
fadeOut(iframe, 1000);
function fadeOut(el, duration) {
/*
* #param el - The element to be faded out.
* #param duration - Animation duration in milliseconds.
*/
var step = 10 / duration,
opacity = 1;
function next() {
if (opacity <= 0) { return; }
el.style.opacity = ( opacity -= step );
setTimeout(next, 10);
}
next();
}
While jQuery is an incredible library your usage of it should be justified by more than just its ability to create fancy effects. A library should be adopted for its completeness and ease of use; not because it happens to offer just one thing you might want to use.
you can do it with jquery!
http://docs.jquery.com/Effects/fadeOut
http://docs.jquery.com/Effects/fadeIn
Or maybe, you can let CSS handle this for you. With a very little of javascript to trigger the effect.
CSS:
#iframe_selector {
/* initial values for iframe, we'll change them via javascript. */
opacity: 0;
/* Note: In out/2016 opacity is on 97.39% of browsers */
/* Just an extra property to show multiple transitions, not needed for fade effect. */
height: 0;
/* Here you can handle a couple of transitions as you wish */
transition: opacity 2s ease-in-out, height 3s ease-in-out;
/* Note: Add all prefixes */
}
Javascript
function toogleIframe(iframe) {
//Check if is show with opacity property,
if (iframe.style.opacity == 0) {
//and set to original values,
iframe.style.opacity = 1;
iframe.style.height = '500px';
} else {
//or hide it.
iframe.style.opacity = 0;
iframe.style.height = '0px';
}
}
//And now, just use it...
//Examples:
domReady(function() {
toogleIframe(document.getElementById('iframe_selector'));
});
var button = document.getElementById('my_button');
button.onclick = function() {
toogleIframe(document.getElementById('iframe_selector'));
};
//Just specify when your iframe needs to be show or not...
Just one thing, maybe you want load your iframe just when it is going to show, to do this just remove src from your iframe in HTML, and add in javascript with iframe.src. That was my case.
You can use the onload property and a css animation to make the iframe fade in once loaded
<iframe
src="..."
onload="this.style.opacity = '1';"
style="
opacity: 0;
transition-duration: 300ms;
transition-property: opacity;
transition-timing-function: ease-in-out;
"
></iframe>
Related
move image vertically from top to bottom by percentage of scroll
I'm trying to make a certain image move from top of the document to it's bottom depending on the scroll percentage, for example, when you load the site, the image will be on top of the page and as the user scrolls down it'll go down little by little depending on the overall document percentage, until at 100% it's at the bottom. I've went through lots of similar solutions on stackoverflow and other sites, but only two seemed close to being what I need. The first works but only on one resolution which is manually adjusted in the code: var imgscroll = document.getElementById('myimg'); window.addEventListener('scroll', function() { var scrollposition = window.scrollY; imgscroll.style.top = scrollposition * 0.1323 + 'vh'; } The second is from a stackoverflow answer - located here and copied below - I think the percentage part is what I need, but couldn't make it work (the image stopped moving): var container = document.getElementById('container'); var windowHeight = window.innerHeight; var windowWidth = window.innerWidth; var scrollArea = 1000 - windowHeight; var square1 = document.getElementsByClassName('square')[0]; var square2 = document.getElementsByClassName('square')[1]; // update position of square 1 and square 2 when scroll event fires. window.addEventListener('scroll', function() { var scrollTop = window.pageYOffset || window.scrollTop; var scrollPercent = scrollTop/scrollArea || 0; square1.style.left = scrollPercent*window.innerWidth + 'px'; square2.style.left = 800 - scrollPercent*window.innerWidth*0.6 + 'px'; }); I'd appreciate any help or tips on how to reach the answer.
Personally I find the approach where you control the position of the image by using animation-play-state: paused and assigning a CSS variable to the animation-delay one of the neatest bit of scripts I ever saw on the web. Here's the pen by Chris Coyier it's based on. And a quote from his website that describes the mechanism: A positive animation delay is where the animation waits a certain amount of time to begin. A negative animation delay starts the animation immediately, as if that amount of time has already gone by. In other words, start the animation at a state further into the animation cycle. When the window has loaded, we first calculate the available space below the image and the amount of page overflow. The first CSS variable --maximum defines the end point of the animation. This is recalculated when the user resizes the screen. Then when scrolling, the ratio of progress is set through another CSS variable --epoch that controls the timing of the keyframe animation. let aim = document.getElementById('image'), room, overflow; window.addEventListener('load', setEdge); window.addEventListener('resize', setEdge); window.addEventListener('scroll', function() { let ratio = (this.pageYOffset || this.scrollY)/overflow; aim.style.setProperty('--epoch', ratio); }); function setEdge() { room = window.innerHeight; overflow = document.body.scrollHeight-room; aim.style.setProperty('--maximum', room-aim.height + 'px'); } body { margin: 0; height: 700vh; } #image { position: fixed; animation-duration: 1s; animation-timing-function: linear; animation-play-state: paused; animation-iteration-count: 1; animation-fill-mode: both; animation-name: move; animation-delay: calc(var(--epoch) * -1s); } #-webkit-keyframes move { 0% { transform: translateY(0); } 100% { transform: translateY(var(--maximum)); } } #keyframes move { 0% { transform: translateY(0); } 100% { transform: translateY(var(--maximum)); } } <img id="image" src="https://via.placeholder.com/140x100" alt=""> For those that want to play around with it: https://jsfiddle.net/z2r40y8c/
Css transition manifests only inside timeout function
I've created new div using JavaScript and set its width and height. Immediately after that I need to resize it to 100% width with transition effect. But it manifests only when the styles editing is inside of Timeout function. Without that it just jump to new width. Css: #project-detail { #extend .project-detail-preview; transition: width 0.25s ease-out, top 0.25s ease-out, left 0.25s ease-out, height 0.25s ease-out; } Script: var detailContainer = document.createElement("div"); detailContainer.id = "project-detail"; detailContainer.innerHTML = previewContent.innerHTML; detailContainer.style.width = previewWidth; detailContainer.style.height = previewHeight; blocksContainer.appendChild(detailContainer); for (let project of source.projects) { if(project.id == projectID) { setTimeout(function () { detailContainer.style.width = "100%"; }, 1); } }
JS is single threaded if you change width to 20 and then to 100, the change to 20 is like if didn't happen. so you need to use a setTimeout() so it first changes it to 20, and "later" it changes to 100
I believe this is because you append the div to the DOM, and immediately (next line of code), you resize it to 100% width. The problem is that in the page's life cycle, the CSS doesn't have time to catch up and apply between these two lines of code. So, the transition duration is not yet applied, and you already resize the div, so it jumps immediately to 100%. On the other hand, when you set a Timeout, being asynchronous, the function inside the Timeout is executed at the end of the execution stack, that is, after applying the CSS rules to the newly created elements. You can even set a 0 delay or no delay at all, it will work all the same.
I tried to do things like this with JS, even read bunch of articles about requestAnimationFrame and understood, that things like that better to do with CSS classes. Try to toggle class on action: for (let project of source.projects) { if(project.id == projectID) { detailContainer.className += ' fullwidth-class'; } } And add same CSS class: .fullwidth-class { width: 100%!important; } #project-detail { animation-duration: 1s; }
How to remove a div with fade out effect in JavaScript?
I want to remove a div element on click event but i want to remove it with a fade out effect. I have got some JQuery solution but i need pure JavaScript or css solution. document.querySelector('.list').addEventListener("click", function(e){ if (e.target.localName === "span") { var removeTarget = e.target.parentNode.parentNode; removeTarget.parentNode.removeChild(removeTarget); }; }); This code is removing the div element with no effect. How can i add a fade out effect?
I've made this function a while ago for a personal project: function removeFadeOut( el, speed ) { var seconds = speed/1000; el.style.transition = "opacity "+seconds+"s ease"; el.style.opacity = 0; setTimeout(function() { el.parentNode.removeChild(el); }, speed); } removeFadeOut(document.getElementById('test'), 2000);
There are two ways you can achieve this: CSS3 animation or jQuery animation. CSS3 Animation In your CSS document, add: .list { opacity: 1; -webkit-transition: opacity 1000ms linear; transition: opacity 1000ms linear; } This will make any change of opacity to your item fade by 1000ms. Change line 4 of your JavaScript to: removeTarget.style.opacity = '0'; setTimeout(() => removeTarget.remove(), 1000); This will make your item change opacity to 0, thus making the transition from step 1 have an effect. Then it will remove the item with your code after 1000ms. Note: Make sure the time of the CSS3 transition and the setTimeout are the same. jQuery Animation Get jQuery Go to the jQuery Website and download it, or Add ` in your HTML document before any jQuery code. Change line 4 of your Javascript to: removeTarget.fadeOut(1000) This will Fade Out your item by 1000ms, you can change this time to whatever you want.
In 2020 you can forgo use of use setTimeout for the animationend event, removing the need to maintain the duration in two places: .fade-out { animation: fade 2s; -webkit-animation: fade 2s; -moz-animation: fade 2s; } /* Animate opacity */ #keyframes fade { from { opacity: 1 } to { opacity: 0 } } #-moz-keyframes fade { from { opacity: 1 } to { opacity: 0 } } #-webkit-keyframes fade { from { opacity: 1 } to { opacity: 0 } } const elementToFade = document.getElementById('my-element'); elementToFade.onanimationend = (e) => { if (e.target.classList.contains('fade-out')) { elementToFade.parentNode.removeChild(elementToFade); } }; // To fade away: elementToFade.classList.add('fade-out');
It's a good question, but to animate some element in html, this element has to exist while it is animating. So, you have some ways to do this, a good way is hide this element with CSS and after the animation you remove this element. While you hiding you can animate, you can see this example: <style> .hide{ opacity: 0; } .fade-out { transition:1s linear all; } </style> <span class="list fade-out"> This is a List, click me to hide </span> <script> document.querySelector('.list').addEventListener("click", function(e) { if (e.target.localName === "span") { //Add CSS hide and animate with fade out var currentCSS = this.className; this.className = currentCSS + ' hide'; var removeTarget = e.target.parentNode.parentNode; setTimeout(function(){ removeTarget.parentNode.removeChild(removeTarget); },1000); }; }); </script>
Add the following CSS class to the element using elem.className="my-animation"; on click: .my-animation { animation: fade 3s steps(90) forwards; -webkit-animation: fade 3s steps(90) forwards; -moz-animation: fade 3s steps(90) forwards; } #keyframes fade { from { opacity: 1; } to { opacity: 0.0; } } You may control the speed of the animation by modifying the steps(number) as well.
Just goto jQuery source code, take out the fade code which is in pure javascript, and use it, no need to reinvent the wheel, or a hint is reduce the height of div to 0 slowly using setTimeInterval() or a css solution would be to use transform and transition properties
Flashing text on value change [duplicate]
I'm brand new to jQuery and have some experience using Prototype. In Prototype, there is a method to "flash" an element — ie. briefly highlight it in another color and have it fade back to normal so that the user's eye is drawn to it. Is there such a method in jQuery? I see fadeIn, fadeOut, and animate, but I don't see anything like "flash". Perhaps one of these three can be used with appropriate inputs?
My way is .fadein, .fadeout .fadein, .fadeout ...... $("#someElement").fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100); function go1() { $("#demo1").fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100)} function go2() { $('#demo2').delay(100).fadeOut().fadeIn('slow') } #demo1, #demo2 { text-align: center; font-family: Helvetica; background: IndianRed; height: 50px; line-height: 50px; width: 150px; } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button onclick="go1()">Click Me</button> <div id='demo1'>My Element</div> <br> <button onclick="go2()">Click Me</button> (from comment) <div id='demo2'>My Element</div>
You can use the jQuery Color plugin. For example, to draw attention to all the divs on your page, you could use the following code: $("div").stop().css("background-color", "#FFFF9C") .animate({ backgroundColor: "#FFFFFF"}, 1500); Edit - New and improved The following uses the same technique as above, but it has the added benefits of: parameterized highlight color and duration retaining original background color, instead of assuming that it is white being an extension of jQuery, so you can use it on any object Extend the jQuery Object: var notLocked = true; $.fn.animateHighlight = function(highlightColor, duration) { var highlightBg = highlightColor || "#FFFF9C"; var animateMs = duration || 1500; var originalBg = this.css("backgroundColor"); if (notLocked) { notLocked = false; this.stop().css("background-color", highlightBg) .animate({backgroundColor: originalBg}, animateMs); setTimeout( function() { notLocked = true; }, animateMs); } }; Usage example: $("div").animateHighlight("#dd0000", 1000);
You can use css3 animations to flash an element .flash { -moz-animation: flash 1s ease-out; -moz-animation-iteration-count: 1; -webkit-animation: flash 1s ease-out; -webkit-animation-iteration-count: 1; -ms-animation: flash 1s ease-out; -ms-animation-iteration-count: 1; } #keyframes flash { 0% { background-color: transparent; } 50% { background-color: #fbf8b2; } 100% { background-color: transparent; } } #-webkit-keyframes flash { 0% { background-color: transparent; } 50% { background-color: #fbf8b2; } 100% { background-color: transparent; } } #-moz-keyframes flash { 0% { background-color: transparent; } 50% { background-color: #fbf8b2; } 100% { background-color: transparent; } } #-ms-keyframes flash { 0% { background-color: transparent; } 50% { background-color: #fbf8b2; } 100% { background-color: transparent; } } And you jQuery to add the class jQuery(selector).addClass("flash");
After 5 years... (And no additional plugin needed) This one "pulses" it to the color you want (e.g. white) by putting a div background color behind it, and then fading the object out and in again. HTML object (e.g. button): <div style="background: #fff;"> <input type="submit" class="element" value="Whatever" /> </div> jQuery (vanilla, no other plugins): $('.element').fadeTo(100, 0.3, function() { $(this).fadeTo(500, 1.0); }); element - class name first number in fadeTo() - milliseconds for the transition second number in fadeTo() - opacity of the object after fade/unfade You may check this out in the lower right corner of this webpage: https://single.majlovesreg.one/v1/ Edit (willsteel) no duplicated selector by using $(this) and tweaked values to acutally perform a flash (as the OP requested).
You could use the highlight effect in jQuery UI to achieve the same, I guess.
If you're using jQueryUI, there is pulsate function in UI/Effects $("div").click(function () { $(this).effect("pulsate", { times:3 }, 2000); }); http://docs.jquery.com/UI/Effects/Pulsate
$('#district').css({opacity: 0}); $('#district').animate({opacity: 1}, 700 );
Pure jQuery solution. (no jquery-ui/animate/color needed.) If all you want is that yellow "flash" effect without loading jquery color: var flash = function(elements) { var opacity = 100; var color = "255, 255, 20" // has to be in this format since we use rgba var interval = setInterval(function() { opacity -= 3; if (opacity <= 0) clearInterval(interval); $(elements).css({background: "rgba("+color+", "+opacity/100+")"}); }, 30) }; Above script simply does 1s yellow fadeout, perfect for letting the user know the element was was updated or something similar. Usage: flash($('#your-element'))
You could use this plugin (put it in a js file and use it via script-tag) http://plugins.jquery.com/project/color And then use something like this: jQuery.fn.flash = function( color, duration ) { var current = this.css( 'color' ); this.animate( { color: 'rgb(' + color + ')' }, duration / 2 ); this.animate( { color: current }, duration / 2 ); } This adds a 'flash' method to all jQuery objects: $( '#importantElement' ).flash( '255,0,0', 1000 );
You can extend Desheng Li's method further by allowing an iterations count to do multiple flashes like so: // Extend jquery with flashing for elements $.fn.flash = function(duration, iterations) { duration = duration || 1000; // Default to 1 second iterations = iterations || 1; // Default to 1 iteration var iterationDuration = Math.floor(duration / iterations); for (var i = 0; i < iterations; i++) { this.fadeOut(iterationDuration).fadeIn(iterationDuration); } return this; } Then you can call the method with a time and number of flashes: $("#someElementId").flash(1000, 4); // Flash 4 times over a period of 1 second
How about a really simple answer? $('selector').fadeTo('fast',0).fadeTo('fast',1).fadeTo('fast',0).fadeTo('fast',1) Blinks twice...that's all folks!
I can't believe this isn't on this question yet. All you gotta do: ("#someElement").show('highlight',{color: '#C8FB5E'},'fast'); This does exactly what you want it to do, is super easy, works for both show() and hide() methods.
This may be a more up-to-date answer, and is shorter, as things have been consolidated somewhat since this post. Requires jquery-ui-effect-highlight. $("div").click(function () { $(this).effect("highlight", {}, 3000); }); http://docs.jquery.com/UI/Effects/Highlight
function pulse() { $('.blink').fadeIn(300).fadeOut(500); } setInterval(pulse, 1000);
I was looking for a solution to this problem but without relying on jQuery UI. This is what I came up with and it works for me (no plugins, just Javascript and jQuery); -- Heres the working fiddle -- http://jsfiddle.net/CriddleCraddle/yYcaY/2/ Set the current CSS parameter in your CSS file as normal css, and create a new class that just handles the parameter to change i.e. background-color, and set it to '!important' to override the default behavior. like this... .button_flash { background-color: #8DABFF !important; }//This is the color to change to. Then just use the function below and pass in the DOM element as a string, an integer for the number of times you would want the flash to occur, the class you want to change to, and an integer for delay. Note: If you pass in an even number for the 'times' variable, you will end up with the class you started with, and if you pass an odd number you will end up with the toggled class. Both are useful for different things. I use the 'i' to change the delay time, or they would all fire at the same time and the effect would be lost. function flashIt(element, times, klass, delay){ for (var i=0; i < times; i++){ setTimeout(function(){ $(element).toggleClass(klass); }, delay + (300 * i)); }; }; //Then run the following code with either another delay to delay the original start, or // without another delay. I have provided both options below. //without a start delay just call flashIt('.info_status button', 10, 'button_flash', 500) //with a start delay just call setTimeout(function(){ flashIt('.info_status button', 10, 'button_flash', 500) }, 4700); // Just change the 4700 above to your liking for the start delay. In this case, //I need about five seconds before the flash started.
Would a pulse effect(offline) JQuery plugin be appropriate for what you are looking for ? You can add a duration for limiting the pulse effect in time. As mentioned by J-P in the comments, there is now his updated pulse plugin. See his GitHub repo. And here is a demo.
Found this many moons later but if anyone cares, it seems like this is a nice way to get something to flash permanently: $( "#someDiv" ).hide(); setInterval(function(){ $( "#someDiv" ).fadeIn(1000).fadeOut(1000); },0)
The following codes work for me. Define two fade-in and fade-out functions and put them in each other's callback. var fIn = function() { $(this).fadeIn(300, fOut); }; var fOut = function() { $(this).fadeOut(300, fIn); }; $('#element').fadeOut(300, fIn); The following controls the times of flashes: var count = 3; var fIn = function() { $(this).fadeIn(300, fOut); }; var fOut = function() { if (--count > 0) $(this).fadeOut(300, fIn); }; $('#element').fadeOut(300, fIn);
If including a library is overkill here is a solution that is guaranteed to work. $('div').click(function() { $(this).css('background-color','#FFFFCC'); setTimeout(function() { $(this).fadeOut('slow').fadeIn('slow'); } , 1000); setTimeout(function() { $(this).css('background-color','#FFFFFF'); } , 1000); }); Setup event trigger Set the background color of block element Inside setTimeout use fadeOut and fadeIn to create a little animation effect. Inside second setTimeout reset default background color Tested in a few browsers and it works nicely.
Like fadein / fadeout you could use animate css / delay $(this).stop(true, true).animate({opacity: 0.1}, 100).delay(100).animate({opacity: 1}, 100).animate({opacity: 0.1}, 100).delay(100).animate({opacity: 1}, 100); Simple and flexible
$("#someElement").fadeTo(3000, 0.3 ).fadeTo(3000, 1).fadeTo(3000, 0.3 ).fadeTo(3000, 1); 3000 is 3 seconds From opacity 1 it is faded to 0.3, then to 1 and so on. You can stack more of these. Only jQuery is needed. :)
There is a workaround for the animate background bug. This gist includes an example of a simple highlight method and its use. /* BEGIN jquery color */ (function(jQuery){jQuery.each(['backgroundColor','borderBottomColor','borderLeftColor','borderRightColor','borderTopColor','color','outlineColor'],function(i,attr){jQuery.fx.step[attr]=function(fx){if(!fx.colorInit){fx.start=getColor(fx.elem,attr);fx.end=getRGB(fx.end);fx.colorInit=true;} fx.elem.style[attr]="rgb("+[Math.max(Math.min(parseInt((fx.pos*(fx.end[0]-fx.start[0]))+fx.start[0]),255),0),Math.max(Math.min(parseInt((fx.pos*(fx.end[1]-fx.start[1]))+fx.start[1]),255),0),Math.max(Math.min(parseInt((fx.pos*(fx.end[2]-fx.start[2]))+fx.start[2]),255),0)].join(",")+")";}});function getRGB(color){var result;if(color&&color.constructor==Array&&color.length==3) return color;if(result=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color)) return[parseInt(result[1]),parseInt(result[2]),parseInt(result[3])];if(result=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color)) return[parseFloat(result[1])*2.55,parseFloat(result[2])*2.55,parseFloat(result[3])*2.55];if(result=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color)) return[parseInt(result[1],16),parseInt(result[2],16),parseInt(result[3],16)];if(result=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color)) return[parseInt(result[1]+result[1],16),parseInt(result[2]+result[2],16),parseInt(result[3]+result[3],16)];if(result=/rgba\(0, 0, 0, 0\)/.exec(color)) return colors['transparent'];return colors[jQuery.trim(color).toLowerCase()];} function getColor(elem,attr){var color;do{color=jQuery.curCSS(elem,attr);if(color!=''&&color!='transparent'||jQuery.nodeName(elem,"body")) break;attr="backgroundColor";}while(elem=elem.parentNode);return getRGB(color);};var colors={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[169,169,169],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[255,0,0],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0],transparent:[255,255,255]};})(jQuery); /* END jquery color */ /* BEGIN highlight */ jQuery(function() { $.fn.highlight = function(options) { options = (options) ? options : {start_color:"#ff0",end_color:"#fff",delay:1500}; $(this).each(function() { $(this).stop().css({"background-color":options.start_color}).animate({"background-color":options.end_color},options.delay); }); } }); /* END highlight */ /* BEGIN highlight example */ $(".some-elements").highlight(); /* END highlight example */ https://gist.github.com/1068231
Unfortunately the top answer requires JQuery UI. http://api.jquery.com/animate/ Here is a vanilla JQuery solution http://jsfiddle.net/EfKBg/ JS var flash = "<div class='flash'></div>"; $(".hello").prepend(flash); $('.flash').show().fadeOut('slow'); CSS .flash { background-color: yellow; display: none; position: absolute; width: 100%; height: 100%; } HTML <div class="hello">Hello World!</div>
Here's a slightly improved version of colbeerhey's solution. I added a return statement so that, in true jQuery form, we chain events after calling the animation. I've also added the arguments to clear the queue and jump to the end of an animation. // Adds a highlight effect $.fn.animateHighlight = function(highlightColor, duration) { var highlightBg = highlightColor || "#FFFF9C"; var animateMs = duration || 1500; this.stop(true,true); var originalBg = this.css("backgroundColor"); return this.css("background-color", highlightBg).animate({backgroundColor: originalBg}, animateMs); };
This one will pulsate an element's background color until a mouseover event is triggered $.fn.pulseNotify = function(color, duration) { var This = $(this); console.log(This); var pulseColor = color || "#337"; var pulseTime = duration || 3000; var origBg = This.css("background-color"); var stop = false; This.bind('mouseover.flashPulse', function() { stop = true; This.stop(); This.unbind('mouseover.flashPulse'); This.css('background-color', origBg); }) function loop() { console.log(This); if( !stop ) { This.animate({backgroundColor: pulseColor}, pulseTime/3, function(){ This.animate({backgroundColor: origBg}, (pulseTime/3)*2, 'easeInCirc', loop); }); } } loop(); return This; }
Put this together from all of the above - an easy solution for flashing an element and return to the original bgcolour... $.fn.flash = function (highlightColor, duration, iterations) { var highlightBg = highlightColor || "#FFFF9C"; var animateMs = duration || 1500; var originalBg = this.css('backgroundColor'); var flashString = 'this'; for (var i = 0; i < iterations; i++) { flashString = flashString + '.animate({ backgroundColor: highlightBg }, animateMs).animate({ backgroundColor: originalBg }, animateMs)'; } eval(flashString); } Use like this: $('<some element>').flash('#ffffc0', 1000, 3); Hope this helps!
Here's a solution that uses a mix of jQuery and CSS3 animations. http://jsfiddle.net/padfv0u9/2/ Essentially you start by changing the color to your "flash" color, and then use a CSS3 animation to let the color fade out. You need to change the transition duration in order for the initial "flash" to be faster than the fade. $(element).removeClass("transition-duration-medium"); $(element).addClass("transition-duration-instant"); $(element).addClass("ko-flash"); setTimeout(function () { $(element).removeClass("transition-duration-instant"); $(element).addClass("transition-duration-medium"); $(element).removeClass("ko-flash"); }, 500); Where the CSS classes are as follows. .ko-flash { background-color: yellow; } .transition-duration-instant { -webkit-transition-duration: 0s; -moz-transition-duration: 0s; -o-transition-duration: 0s; transition-duration: 0s; } .transition-duration-medium { -webkit-transition-duration: 1s; -moz-transition-duration: 1s; -o-transition-duration: 1s; transition-duration: 1s; }
just give elem.fadeOut(10).fadeIn(10);
This is generic enough that you can write whatever code you like to animate. You can even decrease the delay from 300ms to 33ms and fade colors, etc. // Flash linked to hash. var hash = location.hash.substr(1); if (hash) { hash = $("#" + hash); var color = hash.css("color"), count = 1; function hashFade () { if (++count < 7) setTimeout(hashFade, 300); hash.css("color", count % 2 ? color : "red"); } hashFade(); }
you can use jquery Pulsate plugin to force to focus the attention on any html element with control over speed and repeatation and color. JQuery.pulsate() * with Demos sample initializer: $(".pulse4").pulsate({speed:2500}) $(".CommandBox button:visible").pulsate({ color: "#f00", speed: 200, reach: 85, repeat: 15 })
Creating a vertical scrolling transition effect in Jquery
Wanted to get insight and help advancing a plugin I am beginning to build! Looking to build the same effect that AKQA.com has, were on page load certain elements transition into place (using translateY of course). However if the elements are in view within the browser window. As you scroll down, other elements have the same effect transitioning up into place and appearing from opacity 0 to 1. What I am trying to accomplish is getting select elements to transition from opacity 0 to 1 effect translating upwards via scrollonly however when the element is not in-view. If however the elements are already in view (due to page loading right where the elements are) the effect will happen automatically until you scroll down to reveal more elements. Currently in my JS code I am grabbing the data selector on the elements and applying to each of the elements a transition-delay and a CSS class which suppose to be the class that creates the effect. I have three variables docHeight, offSetter and scrolling that are suppose to help me create the logic behind the scrolling effect but I simply can not wrap my head around creating the effect. Here is a live demo in my fiddle http://jsfiddle.net/coder101/hYS48/1/ The Hi link is simply for testing to toggle the in-view CSS class I have Thank you for the help! Javascript var loop = function ScrollTransition( ) { var core = function() { var i = 100, dataTheme = $('[data-show*="on-scroll"]').not('in-view'), docHeight = $( document ).height(), offSetter = parseInt(dataTheme.offset().top, 10), scrolling = dataTheme.scrollTop(); // console.log(h); dataTheme.each(function() { _this = $( this ), _this.css("transition-delay", i + "ms", i += 100); }); }, initializer = function() { if ( el.hasClass('js') && el.hasClass('no-touch') && el.hasClass('csstransitions') ) { core(); } }; return { init:initializer() } }; loop(); // For testing var divElements = $('article'); var doc = $( '#hit' ); doc.on("click", function() { if( el.hasClass('js') && el.hasClass('no-touch') && el.hasClass('csstransitions') ) { divElements.toggleClass('in-view'); } }); CSS .base { width: 300px; height:300px; background:blue; float:left; } article { margin-right:45px; margin-bottom: 40px; } /* starting phase */ .js.no-touch.csstransitions [data-show="on-scroll"] { opacity:0; -webkit-transform:translate(0,90px); -ms-transform:translate(0,90px); transform:translate(0,90px); -webkit-transition:opacity .6s .1s, -webkit-transform .6s ease; transition:opacity .6s .1s, transform .6s ease } /* finishing phase */ .js.no-touch.csstransitions .in-view { opacity:1; -webkit-transform:translate(0,0); -ms-transform:translate(0,0); transform:translate(0,0) }