0

i want to find the center of the view-port using jquery(as it is cross browser compatible) i found this post Using jQuery to center a DIV on the screen but as a learner i just don't understand how to get it in variables. i want the x and values of window center to be stored in "var x" and "var y" and also if the screen is scrolled then it should provide the correct center not the absolute one. i don't want to use css as i need to pass this x and y values to a function. thanks in advance

Community
  • 1
  • 1
arpymastro
  • 751
  • 3
  • 16
  • 34

1 Answers1

0

Actually the answer depends on the start point you want to measure from. If you want to measure from the viewport you should use:

$(window).on("resize", function() {
    window.x = $(window).width() / 2;
    window.y = $(window).height() / 2;
});

Othwerwise if you want to get offsets from the beginning of the page you should use something like $("#...")..offset().

Vadim
  • 1,125
  • 8
  • 18
  • No. But for what? Viewport's center coordinates aren't being modified while the page is being scrolled up/down/left/right. – Vadim May 05 '14 at 14:49
  • ohh okie didnt knew it.. well then let me rephrase the question.. i want the current x and y position of screen's center when even if the screen being scrolled down/up or left/right – arpymastro May 05 '14 at 14:54