We love choice

Fx{r} is trying to start the Fx{r} Community! Please join our group on Adobe Groups following this link: http://fxr.groups.adobe.com.
Fx{r} is now on Twitter too. Follow us @ twitter.com/fx_r!
«
»

How to, Javascript, RIA

How To Scroll To Embeded SWF Object

Andrei Ionescu | 29.10.08 | 1 Comment

Google Buzz

These days I was struggling with this problem: I needed to scroll the page (using javascript) to the embeded swf object. After some search and tests I used the following function (found here) to find the Y position of the object.

function findPosY(obj) {
    var curtop = 0;
    if(obj.offsetParent)
        while(1) {
            curtop += obj.offsetTop;
            if(!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

The next step was to use it with according to my need. So I taken the following steps:

  1. get the object using getElementById (my object has an id set)
  2. put the obtained object into the findPosY funtion to get its Y position
  3. use scrollTo function to scroll the page to the desired Y position

But… big problem occurred. Firefox browser is returning a totally different Y position (afterward I found that it happens to all Gecko based browsers). The difference is approximately the height of the swf object. By trial and error I found that I have to substract the swf height and add 15 pixels.

Using embeds doesn’t give you the same problem. Instead of document.getElementById() function use document.embeds[] array and you won’t have this problem. You can test all these following this link.

The scrolling method is here:

function scrollToMovie(obj) {
    var cy = 0;
    // get the y position
    cy = findPosY(obj);
    // Gecko engine work arround
    // 
    // Gecko returns another value (when using the
    // object tag instead of embed tag) which is 
    // somehow equal with:
    //     y position + object height - 15
    // (this was found by trial and error)
    if (obj.tagName == "OBJECT") {
        if (navigator.userAgent.indexOf("Gecko")!=-1) {
            cy = cy - obj.height + 15;
        }
    }
    // scroll to the movie
    window.scrollTo(0, cy - 10);
}

Please notice that we check if OBJECT is used and if the browser is Gecko. This is it. Archive with the HTML file can be downloaded at the end. If you’ll open the HTML file from your desktop a notification window will appear regarding flash security issues.

The test page is here: http://www.flexer.info/wp-content/uploads/2008/10/test_object_embed.html.

Share and Enjoy:
  • Twitter
  • Google Buzz
  • LinkedIn
  • Google Bookmarks
  • del.icio.us
  • Digg
  • Sphinn
  • blogmarks
  • Reddit
  • StumbleUpon
  • Facebook
  • DZone
  • FriendFeed
  • Yahoo! Buzz
  • Yahoo! Bookmarks
  • Slashdot
  • MySpace
  • Add to favorites
test_object_embed




Tags: , , ,

This post was written by Andrei Ionescu

Views: 7298

related

1 Comment

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

:

:


«
»