parallax scrolling issue - div element jerking when scrolling in webkit browsers

前端 未结 6 991
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-12 19:44

I have created a parallax scroll, which seem to be working fine in firefox however in the chrome browser there\'s a slight jump on the body text when scrolling. click here scrol

6条回答
  •  情话喂你
    2021-02-12 20:09

    In a previous question I created a fairly good parallax scrolling implementation. Jquery Parallax Scrolling effect - Multi directional You might find it useful.

    Here's the JSFiddle http://jsfiddle.net/9R4hZ/40/ use the up/down arrows or scroll wheel.

    Using padding and margin for the positioning are probably why you're experiencing rendering issues. While my code uses scroll or keyboard input for the effect you can loop the relavent portion and check the $moving variable until you reach the desired element on screen.

    function parallaxScroll(scroll) {
        // current moving object
        var ml = $moving.position().left;
        var mt = $moving.position().top;
        var mw = $moving.width();
        var mh = $moving.height();
        // calc velocity
        var fromTop = false;
        var fromBottom = false;
        var fromLeft = false;
        var fromRight = false;
        var vLeft = 0;
        var vTop = 0;
        if($moving.hasClass('from-top')) {
            vTop = scroll;
            fromTop = true;
        } else if($moving.hasClass('from-bottom')) {
            vTop = -scroll;
            fromBottom = true;
        } else if($moving.hasClass('from-left')) {
            vLeft = scroll;
            fromLeft = true;
        } else if($moving.hasClass('from-right')) {
            vLeft = -scroll;
            fromRight = true;
        }
        // calc new position
        var newLeft = ml + vLeft;
        var newTop = mt + vTop;
        // check bounds
        var finished = false;
        if(fromTop && (newTop > t || newTop + mh < t)) {
            finished = true;
            newTop = (scroll > 0 ? t : t - mh);
        } else if(fromBottom && (newTop < t || newTop > h)) {
            finished = true;
            newTop = (scroll > 0 ? t : t + h);
        } else if(fromLeft && (newLeft > l || newLeft + mw < l)) {
            finished = true;
            newLeft = (scroll > 0 ? l : l - mw);
        } else if(fromRight && (newLeft < l || newLeft > w)) {
            finished = true;
            newLeft = (scroll > 0 ? l : l + w);
        }
        // set new position
        $moving.css('left', newLeft);
        $moving.css('top', newTop);
        // if finished change moving object
        if(finished) {
            // get the next moving
            if(scroll > 0) {
                $moving = $moving.next('.parallax');
                if($moving.length == 0)
                    $moving = $view.find('.parallax:last');
            } else {
                $moving = $moving.prev('.parallax');
                if($moving.length == 0)
                    $moving = $view.find('.parallax:first');
            }
        }
        // for debug
        $('#direction').text(scroll + " " + l + "/" + t + " " + ml + "/" + mt + " " + finished + " " + $moving.text());
    }
    

提交回复
热议问题