+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Javascript Scollbar Movement

  1. #1
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    Javascript Scollbar Movement

    I started with an image that is over 60,000 px wide and sliced it into squares to decrease load time. I also decreased the size of the code so that the customer can zoom in and out to view the image. This image table is viewed in a fixed size div. The code below is the javascript i am using to zoom in and out.

    Code:
        var zoom_percent = 50;
        var rows = 8;
        var cols = 16;
    
    
        var zoom_percent = 50;
        var rows = 8;
        var cols = 16;
    
    function zoom_in(){
        percent = zoom_percent / 100;
        var w = document.images;
        var width = w[0].width;
        var height = w[0].height;
        var w_percent = width*percent;
        var h_percent = height*percent;
        // increase width and height on all images
        for ( var i in w ){
           w[i].width = w[i].width + w_percent;
           w[i].height = w[i].height + h_percent;
        } 
        // move scrollbars evenly with image
        var v_movement = (h_percent*rows)/2;
        document.getElementById('image_box').scrollTop = document.getElementById('image_box').scrollTop + v_movement;
        var h_movement = (w_percent*cols)/2;
        document.getElementById('image_box').scrollLeft = document.getElementById('image_box').scrollLeft + h_movement;
    }
    
    function zoom_out(){
        percent = zoom_percent / 100;
        var w = document.images;
        var width = w[0].width;
        var height = w[0].height;
        var w_percent = width*percent;
        var h_percent = height*percent;
        // Reduce width and height on all images
        if (w[0].height>height){
            for ( var i in w ){
               w[i].width = w[i].width - w_percent;
               w[i].height = w[i].height - h_percent;
        }
        // move scrollbars evenly with image
        var v_movement = (h_percent*rows)/2;
        document.getElementById('image_box').scrollTop = document.getElementById('image_box').scrollTop - v_movement;
        var h_movement = (w_percent*cols)/2;
        document.getElementById('image_box').scrollLeft = document.getElementById('image_box').scrollLeft - h_movement;
        } 
    
    }
    The code works great for zooming in on the image by increasing the image size by 50% but I am trying to get the scrollbars to align properly so that it zooms in to one location and does not jump all over.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Javascript Scollbar Movement

    Working one dimension (vertical) and one way (zoom out) at a time. See if it works first, and then adjust the other dimension/directions.

    Code:
    ................... Top of content
     
    This height is scrollTopOld
     
    ------------------- Top of container
     
     
            X    Center of the map--what you want to stay stationary
     
     
    ------------------- Bottom of container

    You want X to stay stationary.

    From TopOfContent to CenterOfMap (ie X ) is

    scrollTopOld + 0.5*H ,

    where H is the height of the container.

    When you zoom out, that distance will stretch to

    1.5*( scrollTopOld + 0.5*H)

    If the X is to remain centered, the distance will be

    scrollTopNew + 0.5*H

    So, setting them equal,

    1.5*( scrollTopOld + 0.5*H) = scrollTopNew + 0.5*H

    or,

    scrollTopNew = 1.5*scrollTopOld + 0.25*H

    Make sure you take scrollTopOld before you resize the images.

    >>>>only done on paper, it seems right to me.
    Nothing is always absolutely so.

  3. #3
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    Re: Javascript Scollbar Movement

    Ok i used the code below to zoom in but i have been unable to figure out how to get it to zoom out.

    document.getElementById('image_box').scrollTop = 1.5*scrollTopOld + 0.25*enc_h;
    document.getElementById('image_box').scrollLeft = 1.5*scrollLeftOld + 0.25*enc_w;

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Javascript Scollbar Movement

    I take it the concept worked for zoom out.

    Go through my steps, but this time the distance from the top of the content to the X shrinks.

    Also, zooming out you went from 100 to 150. So you grow by 50 percent, and you multiply by 1.5

    Zoom in, your sizes go from 150 to 100, so you shrink by 33.3 percent. And you multiply by 0.67
    Nothing is always absolutely so.

  5. #5
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    Re: Javascript Scollbar Movement

    I am trying to understand the mathematical equations you are using to solve this. The zoom percent is going to be adjusted for different images so i need to understand how to adjust this. I am going to dumb down your calculations to see if I am understanding them properly.


    Zoom in works
    Code:
    percent = .50 // zoom percentage
    
    if ScrolltopOld = 200
    
    Scrolltopnew = ((1+percent)*scrollTopOld) + ((percent/2)*enclosure_height);
    
    Scrolltopnew should = 425

    I have not been able to figure out the zoom out feature. The last post did not help me figure it out. If you could possibly format it like i did above it would be greatly appreciated. Thanks for all the help.
    Last edited by driveflexfuel; 10-09-2009 at 12:53 PM.

  6. #6
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Javascript Scollbar Movement

    For the zoom out feature, try to extend this.The zoom percentage takes the old scrolltop value (100, the value before the last zoom) and the current scroll top value. It divides the two of them to create the percentage, which is used to return to the scroll top value before the last zoom.
    Code:
    percent = ScrolltopReallyOld/ScrolltopOld;
    
    //if ScrolltopOld = 150
    
    Scrolltopnew = (percent*scrollTopOld);
    
    //Scrolltopnew should = 100
    Last edited by xav0989; 10-10-2009 at 12:20 PM.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  7. #7
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    Re: Javascript Scollbar Movement

    I have tried that but it does not work.

  8. #8
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Javascript Scollbar Movement

    Your zoom factor zooming out is +50 percent.
    Your zoom factor zooming in is -33 percent, not 50 percent. You should use that percent on the width/height of the images.

    (assuming you want zoom in + zoom out ==> original )


    Code:
    ................... Top of content
     
    This height is scrollTopOld
     
    ------------------- Top of container
     
     
            X    Center of the map--what you want to stay stationary
     
     
    ------------------- Bottom of container

    You want X to stay stationary.

    From TopOfContent to CenterOfMap (ie X ) is

    scrollTopOld + 0.5*H ,

    where H is the height of the container. Note that the 0.5 comes from the fact that we are talking about the center of the display area. It has nothing to do with zoom in/out factors.

    When you zoom in, that distance will shrink to

    .66667*( scrollTopOld + 0.5*H)

    If the X is to remain centered, the distance will be

    scrollTopNew + 0.5*H

    So, setting them equal,

    .66667*( scrollTopOld + 0.5*H) = scrollTopNew + 0.5*H

    or,

    scrollTopNew = .66667*scrollTopOld - 0.16667*H

    Make sure you take scrollTopOld before you resize the images.
    Nothing is always absolutely so.

  9. #9
    driveflexfuel is offline x10 Sophmore driveflexfuel is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    159

    Re: Javascript Scollbar Movement

    I entered your suggestion into my coding ant it does move properly. I created a test page you can see at the link below.

    www.demonwares.com/sdm

    The code in question is as follows.

    Code:
    function shrink(){
        percent = zoom_percent / 100;
        var w = document.images;
        var width = w[0].width;
        var height = w[0].height;
        var w_percent = width*percent;
        var h_percent = height*percent;
        var scrollTopOld = document.getElementById('image_box').scrollTop; // find current scrollbar possition 
        var scrollLeftOld = document.getElementById('image_box').scrollLeft; // find current scrollbar possition 
        // Reduce width and height on all images
        
        if (w[0].height>org_height){
            for ( var i in w ){
               w[i].width = w[i].width - w_percent;
               w[i].height = w[i].height - h_percent;
        }
        // move scrollbars evenly with image
        document.getElementById('image_box').scrollTop = .66667*scrollTopOld - 0.16667*enc_h;
        document.getElementById('image_box').scrollLeft = .66667*scrollLeftOld - 0.16667*enc_w;
        } 
    }

  10. #10
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Javascript Scollbar Movement

    The zoom out feature moves the view to the bottom right corner...
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Making a site JavaScript dependent - pros/cons?
    By Tarzan in forum Programming Help
    Replies: 8
    Last Post: 07-11-2008, 10:08 AM
  2. drop down menus with JavaScript disabled?
    By sifaka in forum Free Hosting
    Replies: 1
    Last Post: 05-15-2008, 10:46 AM
  3. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  4. A question about javascript files
    By rlodge in forum Programming Help
    Replies: 6
    Last Post: 12-19-2007, 11:26 AM
  5. XML and Javascript
    By cuteboytm in forum Graphics & Webdesign
    Replies: 1
    Last Post: 09-21-2007, 10:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers