How do you animate FB.Canvas.scrollTo?

后端 未结 4 529
粉色の甜心
粉色の甜心 2020-12-13 10:50

I\'ve created an app that is a set size (around 2,000 px tall) and have a menu that calls FB.Canvas.scrollTo in order to help the user navigate the long page.

Is the

4条回答
  •  醉话见心
    2020-12-13 11:40

    Just had the same problem today - I came up with a little bit of javascript which makes use of jQuery's animate method which provides some easing - the scroll is still a touch jerky (I'm guessing that's because of the FB.Canvas.scrollTo proxy). Anyway, here's the snippet:

    function scrollToTop() {
        // We must call getPageInfo() async otherwise we will get stale data.
        FB.Canvas.getPageInfo(function (pageInfo) { 
    
            // The scroll position of your app's iFrame.
            var iFrameScrollY = pageInfo.scrollTop;
    
            // The y position of the div you want to scroll up to.
            var targetDivY = $('#targetDiv').position().top;
    
            // Only scroll if the user has scrolled the window beneath the target y position.
            if (iFrameScrollY > targetDivY) {
                var animOptions = {
    
                    // This function will be invoked each 'tick' of the animation.
                    step: function () {
                        // As we don't have control over the Facebook iFrame we have to ask the Facebook JS API to 
                        // perform the scroll for us.
                        FB.Canvas.scrollTo(0, this.y);
                    },
    
                    // How long you want the animation to last in ms.
                    duration: 200
                };
    
                // Here we are going to animate the 'y' property of the object from the 'iFrameScrollY' (the current 
                // scroll position) to the y position of your target div.
                $({ y: iFrameScrollY }).animate({ y: targetDivY }, animOptions);
            }
        });
    }
    

提交回复
热议问题