简体   繁体   English

修复div内的位置背景图像

[英]Fixed position background image inside div

I want to make background of a div fixed so switching div will gave an effect of switching images like on phone screen. 我想让div的背景固定,所以切换div会产生像手机屏幕上那样切换图像的效果。 It is like worsen version of this: https://www.android.com/versions/nougat-7-0/ 这就像恶化版本: https//www.android.com/versions/nougat-7-0/

Fiddle link . 小提琴链接

I am trying in this way: 我正在尝试这种方式:

 .main { min-height: 1000px; } .col1 { width: 29.9%; min-height: 800px; display: inline-block; float: left; } .col2 { width: 70%; min-height: 800px; display: inline-block; } .row1 .col1 { background: rgba(238, 238, 34, 0.3) url("https://icons.iconarchive.com/icons/graphicloads/100-flat-2/256/24-hours-phone-icon.png") !important; background-position: left !important; background-repeat: no-repeat !important; background-size: contain !important; background-attachment: fixed !important; } .row2 .col1 { background: rgba(238, 238, 34, 0.3) url("https://icons.iconarchive.com/icons/graphicloads/100-flat-2/256/abs-icon.png") !important; background-position: left !important; background-repeat: no-repeat !important; background-size: contain !important; background-attachment: fixed !important; } .row3 .col1 { background: rgba(238, 238, 34, 0.3) url("https://icons.iconarchive.com/icons/graphicloads/100-flat-2/256/arrow-down-icon.png") !important; background-position: left !important; background-repeat: no-repeat !important; background-size: contain !important; background-attachment: fixed !important; } 
 <div class="main"> <div class="row1"> <div class="col1"> Col1 </div> <div class="col2"> Col2 </div> </div> <div class="row2"> <div class="col1"> Col1 </div> <div class="col2"> Col2 </div> </div> <div class="row3"> <div class="col1"> Col1 </div> <div class="col2"> Col2 </div> </div> </div> 

But there is no luck since picture is always bigger than div and not inside div as i need and even more problem is that when browser size is changing a background position is changing too. 但是没有运气,因为图片总是比div大,而不是我需要的div内部更多的问题是当浏览器大小改变时,背景位置也在变化。

I know because of background attachment fixed, so it is looking a hole viewport and not a div only, but is there any workaround to achieve this? 我知道因为背景附件固定,所以它看起来是一个洞视口而不仅仅是div,但有没有解决方法来实现这个目的?

So goal is to have background image nicely on center BUT fixed position inside this mother div, so when scrolling an switching effect will be given. 所以目标是在这个母div内的中心BUT固定位置上很好地具有背景图像,因此在滚动时将给出切换效果。 When changing browser viewport picture is always the same size at the center of mother div. 更改浏览器视口图片时,母亲div的中心始终大小相同。


I know about scroll magic, but i just want something more "natural" trough CSS or might minimum JS code. 我知道滚动魔法,但我只想要一些更“自然”的CSS或者最简单的JS代码。

Unfortunately this is simply an artifact of how fixed positioning works in CSS and there is no way around it in pure CSS - you have to use Javascript. 不幸的是,这只是一个固定定位如何在CSS中工作的工件,并且在纯CSS中无法解决它 - 你必须使用Javascript。

The reason this happens is due to the combination of background-attachment: fixed and background-size: cover. 发生这种情况的原因是由于背景附件:固定和背景尺寸:封面的组合。 When you specify background-attachment: fixed it essentially causes the background-image to behave as if it were a position: fixed image, meaning that it's taken out of the page flow and positioning context and becomes relative to the viewport rather than the element it's the background image of. 当你指定background-attachment:fixed时,它本质上会导致background-image的行为就像它是一个位置:fixed image,这意味着它从页面流和定位上下文中取出并变得相对于视口而不是它的元素的背景图像。

To get around this you basically need to use background-attachment: scroll and bind an event listener to the scroll event in JS that manually updates the background-position relative to how far the window has been scrolled in order to simulate fixed positioning but still calculate background-size: cover relative to the container element rather than the viewport. 为了解决这个问题,你基本上需要使用background-attachment:滚动并将事件监听器绑定到JS中的滚动事件,该事件手动更新窗口滚动距离的背景位置,以便模拟固定定位但仍然计算background-size:相对于容器元素而不是视口的覆盖。

Try this 试试这个

background-size: 29.9% 100% !important;

Fiddle 小提琴

After some trying I came out with this one, BUT it's a JS solution, still will be very happy if anyone crack it with pure CSS. 经过一番尝试,我推出了这个,但它是一个JS解决方案,如果有人用纯CSS破解它仍然会很高兴。

Fiddle link https://jsfiddle.net/w4hz4cqf/31/ 小提琴链接https://jsfiddle.net/w4hz4cqf/31/

Changed CSS code a bit: 改变了CSS代码:

    .main
    {
      min-height: 1000px;
      width: 500px;
      margin: 0 auto;

    }

    .col1
    {
      width: 150px;
      min-height: 800px;
      display: inline-block;
      float: left;
    }

    .col2
    {
      width: 70%;
      min-height: 800px;
      display: inline-block;
    }

    .row1 .col1
    {
      background: rgba(238,238,34,0.3) url("https://icons.iconarchive.com/icons/graphicloads/100-flat-2/256/24-hours-phone-icon.png") !important;
        //background-position: left !important;
        background-repeat: no-repeat !important;
        background-size: 100px 100px !important;
        background-attachment: fixed !important;
    }

    .row2 .col1
    {
        background: rgba(238,238,34,0.3) url("https://icons.iconarchive.com/icons/graphicloads/100-flat-2/256/abs-icon.png") !important;
        background-position: left !important;
        background-repeat: no-repeat !important;
        background-size: 100px 100px !important;
        background-attachment: fixed !important;
    }

    .row3 .col1
    {
        background: rgba(238,238,34,0.3) url("https://icons.iconarchive.com/icons/graphicloads/100-flat-2/256/arrow-down-icon.png") !important;
        background-position: left !important;
        background-repeat: no-repeat !important;
        background-size: 100px 100px !important;
        background-attachment: fixed !important;
    }

Added JS Code: 添加JS代码:

    jQuery(document).ready(function(){
      backgroundImageSize = 100;

      elem = jQuery(".row1 .col1, .row2 .col1, .row3 .col1"); 
      for(ind = 0; ind < 3; ind++) {
        elem[ind].getBoundingClientRect();

          elem[ind].style.setProperty('background-position', (elem[ind].getBoundingClientRect().left + jQuery(".col1").width() / 2 - (backgroundImageSize/2))+'px center', 'important');
      }

      var width = $(window).width();
      $(window).on('resize', function(){
         if($(this).width() != width){
            width = $(this).width();
            elem = jQuery(".row1 .col1, .row2 .col1, .row3 .col1"); 

            for(ind = 0; ind < 3; ind++) {
              elem[ind].getBoundingClientRect();
              elem[ind].style.setProperty('background-position', (elem[ind].getBoundingClientRect().left + jQuery(".col1").width() / 2 - (backgroundImageSize/2))+'px center', 'important');
            }
         }
      });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM