The correct approach is to use element.getBoundingClientRect()
:
var rect = element.getBoundingClientRect();
console.log(rect.top, rect.right, rect.bottom, rect.left);
Internet Explorer has supported this since as long as you are likely to care about and it was finally standardized in CSSOM Views. All other browsers adopted it a long time ago.
Some browsers also return height and width properties, though this is non-standard. If you're worried about older browser compatibility, check this answer's revisions for an optimised degrading implementation.
The values returned by element.getBoundingClientRect()
are relative to the viewport. If you need it relative to another element, simply subtract one rectangle from the other:
var bodyRect = document.body.getBoundingClientRect(),
elemRect = element.getBoundingClientRect(),
offset = elemRect.top - bodyRect.top;
alert('Element is ' + offset + ' vertical pixels from <body>');
Multiple backgrounds!
body {
background: #eb01a5;
background-image: url("IMAGE_URL"); /* fallback */
background-image: url("IMAGE_URL"), linear-gradient(#eb01a5, #d13531); /* W3C */
}
These 2 lines are the fallback for any browser that doesn't do gradients.
See notes for stacking images only IE < 9 below.
- Line 1 sets a flat background color.
- Line 2 sets the background image fallback.
The final line sets a background image and gradient for browsers that can handle them.
- Line 3 is for all relatively modern browsers.
Nearly all current browsers have support for multiple background images and css backgrounds. See http://caniuse.com/#feat=css-gradients for browser support. For a good post on why you don't need multiple browser prefixes, see http://codepen.io/thebabydino/full/pjxVWp/
Layer Stack
It should be noted that the first defined image will be topmost in the stack. In this case, the image is on TOP of the gradient.
For more information about background layering see http://www.w3.org/TR/css3-background/#layering.
Stacking images ONLY (no gradients in the declaration) For IE < 9
IE9 and up can stack images this same way. You could use this to create a gradient image for ie9, though personally, I wouldn't. However to be noted when using only images, ie < 9 will ignore the fallback statement and not show any image. This does not happen when a gradient is included. To use a single fallback image in this case I suggest using Paul Irish's wonderful Conditional HTML element along with your fallback code:
.lte9 #target{ background-image: url("IMAGE_URL"); }
Background position, sizing etc.
Other properties that would apply to a single image may also be comma separated. If only 1 value is supplied, that will be applied to all stacked images including the gradient. background-size: 40px;
will constrain both the image and the gradient to 40px height and width. However using background-size: 40px, cover;
will make the image 40px and the gradient will cover the element. To only apply a setting to one image, set the default for the other: background-position: 50%, 0 0;
or for browsers that support it use initial
: background-position: 50%, initial;
You may also use the background shorthand, however this removes the fallback color and image.
body{
background: url("IMAGE_URL") no-repeat left top, linear-gradient(#eb01a5, #d13531);
}
The same applies to background-position, background-repeat, etc.
Best Answer
Without going too deep in your plugin, you can just re-use your
css3animate
method using the current (computed) values for the props you want, and setting the duration to 0 (1 in you plugin since you use the!speed
to use the default..)so in the example using
will do the trick.
example at http://jsfiddle.net/T5LVX/1/
Of course, you should automate this for the current properties in use. If you use a timer when you start the animation and one when someone use the stop method, you can also simulate a pause/resume functionality ..
update
You can store the
cssObject
passed to the animation, to thedata
of the element, and on stop loop through them to get the current values.So in you
animation
method you could$obj.data('animationCss', cssObject);
and in thestop
methodexample: http://jsfiddle.net/T5LVX/6/