SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    center page with JS

    Hey,

    I'm having a problem with my website in that its main content area is sometimes too big for really small resolutions. Is it possible to have an onload even to center the browsers scrollbar so that the content area is visible without initially scrolling?

    Thanks

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Here is a cross-browser way to center the horizontal scrollbar.

    Code javascript:
    function Viewport() {
        this.windowX = (
            document.documentElement && document.documentElement.clientWidth) ||
            window.innerWidth ||
            self.innerWidth ||
            document.body.clientWidth;
        this.windowY = (
            document.documentElement && document.documentElement.clientHeight) ||
            window.innerHeight ||
            self.innerHeight ||
            document.body.clientHeight;
        this.scrollX = (document.documentElement && document.documentElement.scrollLeft) ||
            window.pageXOffset ||
            self.pageXOffset ||
            document.body.scrollLeft;
        this.scrollY = (document.documentElement && document.documentElement.scrollTop) ||
            window.pageYOffset ||
            self.pageYOffset ||
            document.body.scrollTop;
        this.pageX = (document.documentElement && document.documentElement.scrollWidth) ?
            document.documentElement.scrollWidth :
            (document.body.scrollWidth > document.body.offsetWidth) ?
                document.body.scrollWidth : document.body.offsetWidth;
        this.pageY = (document.documentElement && document.documentElement.scrollHeight) ?
            document.documentElement.scrollHeight :
            (document.body.scrollHeight > document.body.offsetHeight) ?
                document.body.scrollHeight : document.body.offsetHeight;
    }
    var viewport = new Viewport();
    window.scrollTo(viewport.pageX/2 - viewport.windowX/2, 0);

    *mumblebloodynonstandardbrowsersmuttergrumble*
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •