i'm working on a project where i need a semi-transparent div to slideover the entire page at a certain point. i have a version working currently, but it's not as smooth as i'd like it to be.
http://jsfiddle.net/27e310e8/
jQuery from above example:
var windowWidth = $(window).innerWidth();
var windowHeight = $(window).innerHeight();
function blackOut() {
$('#fail-screen').css({
"width": windowWidth + "px",
"height": windowHeight + "px"
});
$('#fail-screen').delay(1000).animate({
top: '0px'
}, 1000, 'linear');
}
$(document).ready(function () {
blackOut(windowWidth, windowHeight);
});
as you can see i'm getting the innerWidth and height to set the "fail-screen" div, as setting it to 100% wasn't working well. i'm using jQuery to animate the top position of the "fail-screen" div.
again, i'm just looking to refactor this code and improve overall presentation and performance. i'm open to using animation/physic libraries if anyone knows of any that would be good to use here.
appreciate any suggestions.
As #Jason has mentioned, I strongly recommend using CSS transforms instead of fiddling with the offsets. That is being not only are CSS transforms offloaded to the GPU (intelligently determined by the browser as of when needed, but you can also force it), but it allows for subpixel rendering. Paul Irish published a rather good write-up on this topic back in 2012.
Also, your code is slightly problematic in the sense that it fails to handle viewport resize events. In fact, a more straightforward solution would simply be using position: fixed, and then playing around with CSS transform to bring the element into view after a delay.
See updated fiddle here: http://jsfiddle.net/teddyrised/27e310e8/2/
For the JS, we simply use the .css() method. The delay, animation duration and even the timing function can be easily done via CSS.
The new JS is rather straightforward: we set the transform of #fail-screen so that we move it back to its original vertical position. jQuery automagically prefixes the transform property ;)
$(document).ready(function () {
$('#fail-screen').css({
'transform': 'translateY(0)'
});
});
For the CSS, we initially set a vertical translation (translateY) of -100%, which means we push the element upwards by its own height. Using fixed positioning and declaring all four offsets as 0, we can force the element to fill the viewport without any advanced hack of listening to window resize events via JS. Remember that you will have to add vendor prefixes to the transform property to maximize cross-browser compatibility.
CSS can also handle transition delay, duration and even the timing function, i.e. transition: [property] [duration] [timing-function] [delay]; Since in your jQuery code you have set both duration and delay to be 500ms, it should be transition: all 0.5s linear 0.5s. However, the linear timing function doesn't look good — perhaps using ease-in-out would be better, or even a custom cubic-bezier curve, perhaps?
Also, I recommend moving the opacity to the background-color value, simply because if you set an opacity on the element itself, all child nodes will be rendered at 0.6 opacity, too... it might be something that you do not want to achieve.
#fail-screen{
top: 0;
left: 0;
bottom: 0;
right: 0;
background-color: rgba(0,0,0,.6); /* Moved opacity to background-color */
position: fixed; /* Use fixed positioning */
z-index: 303;
/* Use CSS transform to move the element up by its own height */
-webkit-transform: translateY(-100%);
-ms-transform: translateY(-100%);
-o-transform: translateY(-100%);
transform: translateY(-100%);
/* CSS transition */
transition: all .5s ease-in-out .5s;
}
Related
This is what I have:
.s2 {
top: 150px;
left: 20px;
position: absolute;
transition: left 300ms linear;
}
I change the left position dynamically on scroll with JavaScript. At the moment the performance is bad on mobile and even in a desktop browser.
How can I improve this? Is there a better approach for this?
Consider throttling the scroll using requestAnimationFrame
use properties such as translate if you can instead of left or top
Ad translateZ(0) or translate3d(0,0,0) to trigger GPU on mobile (not always guaranteed)
Also since you are animating during scroll, you do not need to use the transition property, unless you have breakpoints/thresholds where you set the property once scroll amount exceeds a certain value.
Alright I have these divs that I have been animating in/out previously by doing hide() or show(), however that does not look clean and Id like to have them grow out from the center, i.e. grow from width of nothing to their current width.
I am new to jquery animations and don't know how to do this properly. I have tried setting the initial width to 0 and doing:
function panelIn(labDiv) {
var neww = "700px";
$(labDiv).animate({
width: neww
}, 2000);
}
But that grows the div to the right. How can I achieve this? Are there any good ;libraries for animating in divs, i.e. introducing them on the page?
Depending on what you're doing, you might want to just use CSS and .toggleClass().
CSS
#labDiv{
height:700px;
width:700px;
transform:scale(0);
transform-origin:center center;
transition: transform 2000ms ease;
}
#labDiv.show{
transform:scale(1);
}
jQuery
function panelIn(labDiv){
labDiv.toggleClass('show');
}
For a number of projects now I have had elements on the page which I want to translate out of the screen area (have them fly out of the document). In proper code this should be possible just by adding a class to the relevant element after which the css would handle the rest. The problem lies in the fact that if for example
.block.hide{
-webkit-transform:translateY(-10000px);
}
is used the element will first of all fly out of the screen unnecessarily far and with an unnecessarily high speed. And purely from an aesthetic point of view there's a lot left to be desired (Theoretically speaking for example a screen with a height of 10000px could be introduced one day in the future).
(Update) The problem why percentages can't be used is that 100% is relative to the element itself, rather than to the parent element/screen size. And containing the element in a full-sized parent would be possible, but would create a mess with click events. And after a few answers, allow me to point out that I am talking about translations, not about position:absolute css3 transitions (which are all fine, but once you get enough of them they stop being fun).
What aesthetically pleasing solutions to allow an element to translate out of a screen in a fixed amount of time can you guys think of?
Example code can be found in this jsfiddle demonstrating the basic concept.
http://jsfiddle.net/ATcpw/
(see my own answer below for a bit more information)
If you wrap the .block div with a container:
<div class="container">
<div class="block"></div>
</div>
<button>Click</button>
you could expand and then, translate the container itself after the click event
document.querySelector("button").addEventListener("click", function () {
document.querySelector(".container").classList.add("hide");
});
with this style
.block {
position:absolute;
bottom:10px;
right:10px;
left:10px;
height:100px;
background:gray;
}
.container {
-webkit-transition: -webkit-transform ease-in-out 1s;
-webkit-transform-origin: top;
-webkit-transition-delay: 0.1s; /* Needed to calculate the vertical area to shift with translateY */
}
.container.hide {
position:absolute;
top:0;
left:0;
bottom:0;
right:0;
/* background:#f00; /* Uncomment to see the affected area */
-webkit-transform: translateY(-110%);
}
In this way, it is possible to apply a correct translationY percentage ( a little more than 100%, just to have it out of the way ) and mantaining the button clickable.
You could see a working example here : http://jsfiddle.net/MG7bK/
P.S: I noticed that the transition delay is needed only for the transitionY property, otherwise the animation would fail, probably because it tries to start before having an actual value for the height. It could be omitted if you use the horizontal disappearing, with translateX.
What I did is use the vh (view height) unit. It's always relative to the screen size, not the element itself:
/* moves the element one screen height down */
translateY(calc(100vh))
So if you know the position of the element in the screen (say top:320px), you can move it exactly off the screen:
/* moves the element down exactly off the bottom of the screen */
translateY(calc(100vh - 320px))
I know this is not exactly what you were asking but...
Would you consider using CSS animations with Greensock's Animation Platform? It is terribly fast (it claims it's 20 times faster than jQuery), you can see the speed test here: http://www.greensock.com/js/speed.html
It would make your code nicer I believe, and instead of trying to hack CSS animations you could focus on more important stuff.
I have created a JSFiddle here: http://jsfiddle.net/ATcpw/4/
Both CSS and possible JS look simpler:
document.querySelector("button").addEventListener("click",function(){
var toAnimate = document.querySelector(".block");
TweenMax.to(toAnimate, 2, {y: -window.innerHeight});
});
CSS:
.block{
position:absolute;
bottom:10px;
right:10px;
left:10px;
height:100px;
background-image: url(http://lorempixel.com/800/100);
}
I recently built an app which used precisely this technique for sliding 'panels' (or pages) and tabs of the application in and out of view. A basic implementation of the tabs mechanism can be seen here.
Basically (pesudo-code to illustrate the concept, minus prefixes etc):
.page {
transform: translateY(100%);
}
.page.active {
transform: translateY(0%);
}
The problem I had was that Android Webkit in particular wouldn't calculate percentage values correctly. In the end I had to use script to grab the viewport width and specify the value in pixels, then write the rules using a library for dynamic stylesheet parsing.
But eventually, and in spite of only these minor platform-specific problems, this worked perfectly for me.
Use calc method (http://jsfiddle.net/ATcpw/2/):
.block{
position:absolute;
top: -webkit-calc(100% - 110px);
right:10px;
left:10px;
height:100px;
background:gray;
-webkit-transition: all 2s;
/*this adds GPU acceleration*/
transform: translate3d(0,0,0);
-webkit-transform: translate3d(0,0,0);
}
.block.hide{
top: -100px;
}
Since you are using -webkit prefix I used it as well.
calc is supported by majority of browsers: http://caniuse.com/#search=calc
One very simple, but not aesthetically pleasing solution is to define the class dynamically:
var stylesheet = document.styleSheets[0];
var ruleBlockHide;
and
//onresize
if(ruleBlockHide) stylesheet.deleteRule(ruleBlockHide);
ruleBlockHide = stylesheet.insertRule('.block.hide{ -webkit-transform:translateY(-'+window.innerHeight+'px); }',stylesheet.cssRules.length);
see: http://jsfiddle.net/PDU7T/
The reason a reference to the rule needs to be kept is that after each screen resize the rule has to be deleted and re-added.
Although this solution gets the job done, there has to be some DOM/CSS combination which would allow this to be done without javascript (something along the lines of a 100%x100% element containing such a block, but I haven't been able to figure out any transform based way).
get the document width. then use a java script trigger to trigger the css3 translation.
function translate(){
var width = document.body.Width;
document.getElementById('whateverdiv').style='translateX(' + width + 'px)';
}
This is simple
add the following to your div
.myDiv {
-webkit-transition-property: left;
-webkit-transition-duration: 0.5s;
-webkit-transition-timing-function: ease-in-out;
-webkit-transition-delay: initial
}
then change the "left" property of it either by adding an additional class or by jQuery
This will animate it along the x-axis
Note: you can change the -webkit-transition-property to any property you want and this will animate it
Hey guys I'm in the process of making this website here: http://craigmarkdrums.com (bare in mind that it's still very early days, so no judging, yeh hehe) and take a look at the menu. In firefox it works fine, but in chrome and safari you can see a flickering in the right hand corner. i think what is happenning is it's the box changing size. they are all li's in a ul. here is my jquery:
$(function()
{
$('#nav li').hover(function()
{
$(this).children().stop(true, true).fadeIn();
$(this).stop().animate({
width: '90px'
}, 300, 'swing');
$(this).siblings().stop().animate({
width: '42px'
}, 300, 'swing');
}, function()
{
$(this).children().stop(true, true).fadeOut();
$(this).stop().animate({
width: '50px'
}, 200);
$(this).siblings().stop().animate({
width: '50px'
}, 200);
});
});
any ideas what i'm doing wrong, or how i could improve this code?
cheers
Matt
You're intuition is correct. To accomplish this effect using floats, you'd need to handle the animation yourself and resize all LIs in a single step, making sure the sum of their widths matched the containing element. Or try using absolute positioning and handling the offsets yourself.
.. Or you could cheat and put the whole thing inside a container div whose background was the photo your using. That way any bleed through would be of the photo, not the white background.
I'd suggest moving over to a flexible box layout for this (CSS3). You can just increase the size of the box you want and the others will shrink away by themselves.
This page has a lot of examples of the flex-box layout (Take a look at the second example)
Add this css to your menu panels:
-moz-box-flex: 1;
-webkit-box-flex: 1;
box-flex: 1;
And then set the width of the hovered element. For added fluidity, try adding some slight transition delays like this (I've separated the values for easy reading and understanding):
-moz-transition-property: width;
-moz-transition-duration: 0.2s;
-moz-transition-easing: ease-in-out;
-webkit-transition-property: width;
-webkit-transition-duration: 0.2s;
-webkit-transition-easing: ease-in-out;
transition-property: width;
transition-duration: 0.2s;
transition-easing: ease-in-out;
I am currently writing a jQuery plugin to create / manage CSS transitions, and I found this strange behavior with transition-duration.
Apparently, while a transition is running, any changes to the duration property are ignored unless the properties being transitioned receive a different value. The duration itself does not cause the transition to change.
Following is some code which shows an example of this, and below are some links to jsFiddle to give you a better idea of the transition behavior I am trying to achieve.
/* starting transition */
.t1 {
-webkit-transition-duration: 5s;
-webkit-transition-property: width;
width: 500px;
}
/* during the above, this will do nothing */
.t2 {
-webkit-transition-duration: 200ms;
-webkit-transition-property: width;
width: 500px;
}
/* but this will override the transition as expected */
.t3 {
-webkit-transition-duration: 200ms;
-webkit-transition-property: width;
width: 501px; /* 1 pixel added */
}
jsFiddle 1 - CSS duration problem: http://jsfiddle.net/danro/Kd58j/
jsFiddle 2 - Desired effect w/ jQuery: http://jsfiddle.net/danro/xPwc4/
Any ideas on how to force the transition to accept the updated duration?
UPDATE
It looks like this behavior is defined in the spec, but I am still open to a workaround if anyone has one.
(From www.w3.org/TR/css3-transitions/#starting)
Once the transition of a property has started, it must continue running based on the original timing function, duration, and delay, even if the ‘transition-timing-function’, ‘transition-duration’, or ‘transition-delay’ property changes before the transition is complete.
I've run into the same issue when I needed to overwrite transition-duration but leaving the transition-property intact. The only simple workaround I've found so far is to actually change transition-property a little bit, i.e. instead of opacity: 0 make it opacity: 0.0001.
just tested your first link with Chrome and Safari and it works fine, just like the jQuery example :)