How do you get left position of parent element
I've tried using position.left it says invalid object i've tried css.('left') i don't really know what to do. I want to get the position of the parent element so I can ani开发者_运维知识库mate the child element left position Im creating a scrolling effect.
<div id="MyDiv">
<div>Element 1</div>
<div>Element 2</div></div><div id="Prev">Prev</div><div id="Next">Next</div>
If you're trying to get the left offset of 'MyDiv' you can do something like:
$('#MyDiv').offset().left;
this is using the offset()
method: http://api.jquery.com/offset/
$('#MyDiv').offset() return object position, here is the example
var pos = $('#MyDiv').offset();
console.debug(pos.left); //Try this or
console.debug(pos.top);
There's nice 4 lines pure Javascript (no external libraries) function for this:
http://www.quirksmode.org/js/findpos.html
Or you can just try x.offsetLeft
It really depends on the context of the elements position. Check the docs to find which context you want to use it in.
I created a simple example to illustrate the referencing of the offset. Here is the code that I think would solve your problem.
$('#MyDiv').offset().left; // Return the pixel number of the elements left position.
Here is a simple example that will help you out. http://www.jsfiddle.net/cNQjT/
You can use the float property of css
eg:- $('#MyDiv').css('float','left');
精彩评论