使用Firefox 18.0.1进行动画闪烁(由于RequestAnimationFrame?)

时间:2013-01-24 13:52:14

标签: javascript jquery firefox parallax

我使用这个http://www.netmagazine.com/tutorials/create-interactive-street-view-jquery教程为我们的一位客户创建了一个介绍:

http://f-bilandia.de/kunstmann/bronski/

它曾经在所有浏览器上都运行得非常好。当我更新到最新稳定版本的Firefox(FF 18.0.1)时,在更改图像时会出现大量闪烁。

在阅读最新版本的发行说明时,我看到ff有一个新的Javascript引擎,并通过新的HTML缩放算法提高了图像质量。也许是因为那个?其他可能的解决方案?

下面你可以看到我用过的代码:

$(document).ready(function(){
        var $doc = $(document);
        var $win = $(window);

        // dimensions - we want to cache them on window resize
        var windowHeight, windowWidth;
        var fullHeight, scrollHeight;
        var streetImgWidth = 1024, streetImgHeight = 640;
        calculateDimensions();

        var currentPosition = -1, targetPosition = 0;
        var $videoContainer = $('.street-view');
        var video = $('.street-view > img')[0];
        var $hotspotElements = $('[data-position]');


        // handling resize and scroll events

        function calculateDimensions() {
            windowWidth = $win.width();
            windowHeight = $win.height();
            fullHeight = $('#main').height();
            scrollHeight = fullHeight - windowHeight;
        }

        function handleResize() {
            calculateDimensions();
            resizeBackgroundImage();
            handleScroll();
        }

        function handleScroll() {
            targetPosition = $win.scrollTop() / scrollHeight;
        }

        // main render loop
        window.requestAnimFrame = (function(){
          return  window.requestAnimationFrame       ||
                  window.webkitRequestAnimationFrame ||
                  window.mozRequestAnimationFrame    ||
                  window.oRequestAnimationFrame      ||
                  window.msRequestAnimationFrame     ||
                  function(/* function */ callback, /* DOMElement */ element){
                    window.setTimeout(callback, 1000 / 60);
                  };
        })();


        function animloop(){
            if ( Math.floor(currentPosition*5000) != Math.floor(targetPosition*5000) ) {
                currentPosition += (targetPosition - currentPosition) / 5;
                render(currentPosition);
            }
          requestAnimFrame(animloop);
        }






        // rendering


        function render( position ) {
            // position the elements
            var minY = -windowHeight, maxY = windowHeight;
            $.each($hotspotElements,function(index,element){
                var $hotspot = $(element);
                var elemPosition = Number( $hotspot.attr('data-position') );
                var elemSpeed = Number( $hotspot.attr('data-speed') );
                var elemY = windowHeight/2 + elemSpeed * (elemPosition-position) * scrollHeight;
                if ( elemY < minY || elemY > maxY ) {
                    $hotspot.css({'visiblity':'none', top: '-1000px','webkitTransform':'none'});
                } else {
                    $hotspot.css({'visiblity':'visible', top: elemY, position: 'fixed'});
                }
            });


            renderVideo( position );
        }



        function resizeBackgroundImage(){
            // get image container size
            var scale = Math.max( windowHeight/streetImgHeight , windowWidth/streetImgWidth );
            var width = scale * streetImgWidth , height = scale * streetImgHeight;
            var left = (windowWidth-width)/2, top = (windowHeight-height)/2;
            $videoContainer
                      .width(width).height(height)
                      .css('position','fixed')
                      .css('left',left+'px')
                      .css('top',top+'px');
        }





        // video handling

        var imageSeqLoader = new ProgressiveImageSequence( "street/vid-{index}.jpg" , 387 , {
            indexSize: 4,
            initialStep: 16,
            onProgress: handleLoadProgress,
            onComplete: handleLoadComplete,
            stopAt: 1
        } );
                    // there seems to be a problem with ie 
                    // calling the callback several times
        var loadCounterForIE = 0; 
        imageSeqLoader.loadPosition(currentPosition,function(){
            loadCounterForIE++;
            if ( loadCounterForIE == 1 ) {
                renderVideo(currentPosition);
                imageSeqLoader.load();
                imageSeqLoader.load();
                imageSeqLoader.load();
                imageSeqLoader.load();
            }
        });


        var currentSrc, currentIndex;

        function renderVideo(position) {
            var index = Math.round( currentPosition * (imageSeqLoader.length-1) );
            var img = imageSeqLoader.getNearest( index );
            var nearestIndex = imageSeqLoader.nearestIndex;
            if ( nearestIndex < 0 ) nearestIndex = 0;
            var $img = $(img);
            var src;
            if ( !!img ) {
                src = img.src;
                if ( src != currentSrc ) {
                    video.src = src;
                    currentSrc = src;
                }
            }
        }



        $('body').append('<div id="loading-bar" style="">Loading...</div>');

        function handleLoadProgress() {
            var progress = imageSeqLoader.getLoadProgress() * 100;
            $('#loading-bar').css({width:progress+'%',opacity:1});
        }

        function handleLoadComplete() {
            $('#loading-bar').css({width:'100%',opacity:0,display: "none"});
            $("html, body").css("overflow", "auto");
            $("html, body").css("overflow-x", "hidden");
            $("nav").css("display", "block");
            $("#preloader").fadeOut("slow");
            $("#scroll-hint").css("display", "block");
        }




        $win.resize( handleResize );
        $win.scroll( handleScroll );

        handleResize();
        animloop();




    });

1 个答案:

答案 0 :(得分:1)

在“渲染(位置)”功能中,以下几行似乎应该重构。

            if ( elemY < minY || elemY > maxY ) {
                $hotspot.css({'visiblity':'none', top: '-1000px','webkitTransform':'none'});
            } else {
                $hotspot.css({'visiblity':'visible', top: elemY, position: 'fixed'});
            }
  • 因为一个可见性拼写错误,并且没有“无”值(它将被“隐藏”)。只需使用“display”和“none”和“”值。
  • “top”,“webkitTransform”和“position”键似乎没必要。如果元素不可见,则无需设置顶部,为什么元素始终不是固定位置?