Setting position of a new element with .offset gives me negative position
I'm trying to learn jQuery, and I'm trying to create a set of absolutely-positioned elements across the page (why is a long story), positioning them with .offset.
However, an element positioned with .offset({ top:20, left:20 }) comes out as
- <div class="rand" style="top: -618px; left: -336px; ">20 - 20</div>
which is not at all what I expected...
- for (var i=0; i< n; i++) {
- var off = {
- top: i * 20 // Math.floor(h * Math.random())
- ,left: i * 20 //Math.floor(w * Math.random())
- };
- divs.push($('<div />').
- appendTo($('body')).
- addClass('rand').
- offset(off).
- text(off.left + " - " + off.top).
- click(function(ev){
- var off = $(this).offset() ;
- alert("Pos: " + off.left + ", " + off.top) ;
- }
- ));
- }
can anyone explain what I'm doing wrong?