【问题标题】:Background-size cover jumping when background-position switches to fixed背景位置切换到固定时背景大小的封面跳跃
【发布时间】:2012-12-12 23:34:14
【问题描述】:

我正在处理一个视差/滚动时间轴项目,但我遇到了 CSS3 背景尺寸封面属性的问题。

div 具有以下属性:

background: url(../images/timeline/back-6.jpg) no-repeat top center black; 
background-size: cover;
padding-top: 90px;
height: 1855px;
position: relative;

使用 jQuery,我将背景附件切换为固定。当我这样做时,背景图像会“进入”(意味着超过屏幕边缘的图像部分现在可见)。这不是我们想要的结果。

在测试中,我可以将 div 切换为使用 background-size: 100% 覆盖,但在滚动时会导致不同的垂直跳跃问题。

当我将背景切换为固定时如何防止它跳入任何想法? (当我将背景设置为滚动时,它也会反过来发生)。

很遗憾,我无法链接到此代码的演示,因为该页面尚未准备好部署。

【问题讨论】:

    标签: css


    【解决方案1】:

    在将background-size 设置为covercontain 时,我遇到了同样的问题

    设置固定高度,例如通过@media 设置较小的屏幕可防止背景图像跳跃。经过我的测试,我得出的结论是,跳跃是由于将background-attachment 设置为fixed 后元素的方向所致

    将其设置为fixedsize 由视口的大小计算,而不是包含background-image 的元素。这就是跳跃的来源以及为什么为background-size 设置固定高度或宽度可以解决此问题。

    【讨论】:

    • 我有同样的问题,这对我不起作用。我将背景设置为覆盖(尽管我尝试使用固定值),如果我尝试将图像定位得更高一点,屏幕仍然会在滚动动作中向下跳。还有其他解决方案吗?
    【解决方案2】:

    在创建要与 scrollTo-Plugin 等一起使用的单页布局时,我遇到了同样的问题.... 页面布局分为两部分: 背景图像的左侧应该随着右侧的内容而改变/滚动。 所以我曾经制作了一种jquery插件来结合“背景位置:固定”和“背景尺寸:封面”。 您只需要通过 class/id 定义元素来对齐背景图像。

    不要抱怨代码。我对 javascript/jquery 比较陌生。但它的工作;) 就是这样:

    function fixedResize() {
        var targetEl = $('element where bg-images are in');
        var targetWidth = targetEl.width();
        var targetHeight = targetEl.height();
        var targetPosX = targetEl.offset().left;
        var targetPosY = targetEl.offset().top;
        var leftRatio = targetWidth / targetHeight;
        //console.log('TargetWidth', targetWidth, 'TargetHeight', targetHeight, 'Offset', targetPosX, targetPosY, 'leftRatio', leftRatio);
        targetEl.each(function(){
    
            var imgTarget = $(this);
            var url = $(this).css('background-image').replace('url(', '').replace(')', '').replace("'", '').replace('"', '');
            var bgImg = $('<img />'); // make background-image as image tag for getting width and height of the image
            imgTarget.css('background-attachment','fixed');
            bgImg.hide();
            bgImg.bind('load', function(){
                var imgHeight = $(this).height();
                var imgWidth = $(this).width();
                var imgRatio = imgWidth / imgHeight;
                $(this).remove(); // remove img Tags again
    
                // Calculate resize dimensions
                if (imgRatio > leftRatio) {
                    var currentWidth = imgRatio * targetHeight; // image width after resize
                    var currentHeight = (currentWidth/imgWidth)*imgHeight;
                    var setToLeft = ((currentWidth - targetWidth)/2);
                    var imgPosX = targetPosX - setToLeft;
                    var imgPosY = (currentHeight - targetPosY - currentHeight/2 - targetHeight/2)* -1;
                    var resizeImg = 'background-size: auto '+ targetHeight +'px;';                  
    
                    } else if (imgRatio < leftRatio){
                        var currentWidth = targetWidth;
                        var currentHeight = (currentWidth/imgWidth)*imgHeight;
                        var imgPosX = targetPosX;
                        var imgPosY = (currentHeight - targetPosY - currentHeight/2 - targetHeight/2)* -1;
                        var resizeImg = 'background-size: '+ targetWidth +'px auto;'; // resize background
                    }
                imgTarget.attr('style','background-attachment: fixed; background-position: '+ imgPosX +'px '+ imgPosY +'px;' + resizeImg);
                console.log('imgWidth', imgWidth, 'imgHeight', imgHeight, 'imgRatio', imgRatio, 'currentWidth', currentWidth, 'currentHeight', currentHeight, 'setToLeft', setToLeft);
                console.log('imgPos', imgPosX, imgPosY, 'setToLeft', setToLeft, targetPosX);
    
            });
            $(this).append(bgImg);
            bgImg.attr('src', url);
    
        });
    }
    fixedResize(); // initiate function
    
    $(window).resize(function() {
        fixedResize(); // initiate function for window resize (Fluid behavior)
    });
    

    jsfiddle.net/rowphant/eXb6e/14/

    【讨论】:

      猜你喜欢
      • 2014-05-27
      • 2015-02-07
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2013-05-12
      • 1970-01-01
      相关资源
      最近更新 更多