Change div offset when window resizes. Jquery
I'm using Fancybox开发者_运维知识库 plugin. When it loads I insert an image (preview comments) next to the Close button and position it this way:
var p = $("#fancybox-close");
var offset = p.offset();
$("#preview_comments").offset({ top: offset.top, left: offset.left-30});
This way when the window is resized the image stays right as it had an absolute position. I've also tried:
function pcomments() {
var p = $("#fancybox-close");
var offset = p.offset();
$("#preview_comments").offset({ top: offset.top, left: offset.left-30});
};
pcomments();
$(window).resize(function() {
pcomments();
});
When I minimize the window, this image moves away to the right. If I continue resizing the window the image goes back to the left of the Fancybox close button but doesn't make the offset correctly (and it looks like it doesn't update on time).
Thanks'
Ok, I solved the problem.
I appended the div to the image (I could only do it through .ax-window, not fancybox-img) and then applied the offset. It was not necessary to apply the window resize. this is the final code:
$("#preview_comments").appendTo('.ax-window');
$("#preview_comments").css("position","relative");
function pcomments() {
var p = $("#fancybox-close");
var offset = p.offset();
$("#preview_comments").offset({ top: offset.top, left: offset.left-30});
};
pcomments();
精彩评论