IAMTHESTIG
IAMTHESTIG

Reputation: 418

Getting the width of a long horizontal div and find the middle with javascript

I have a 9000px horizontal div container and I want to find it's middle part.

I have this code but its getting the whole width of the window not the container div.

    $(document).ready(function(){
     scrollTo(($(document).width() - $(window).width()) / 2, 0);
});

Here's a jsFiddle of the whole code

Upvotes: 0

Views: 121

Answers (1)

Manjunath Manoharan
Manjunath Manoharan

Reputation: 4617

It should not be $(document) instead if the id of the 9000px div is #long_div, see the code below.

$(document).ready(function(){
  scrollTo($("#long_div").width()/2, 0);
});

Upvotes: 2

Related Questions