javascript - Fixed position background image inside div -
i want make background of div fixed switching div gave effect of switching images on phone screen. worsen version of this: https://www.android.com/versions/nougat-7-0/
fiddle link.
i trying in 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 no luck since picture bigger div , not inside div need , more problem when browser size changing background position changing too.
i know because of background attachment fixed, looking hole viewport , not div only, there workaround achieve this?
so goal have background image nicely on center fixed position inside mother div, when scrolling switching effect given. when changing browser viewport picture same size @ center of mother div.
i know scroll magic, want more "natural" trough css or might minimum js code.
unfortunately artifact of how fixed positioning works in css , there no way around in pure css - have use javascript.
the reason happens due combination of background-attachment: fixed , background-size: cover. when specify background-attachment: fixed causes background-image behave if position: fixed image, meaning it's taken out of page flow , positioning context , becomes relative viewport rather element it's background image of.
to around need use background-attachment: scroll , bind event listener scroll event in js manually updates background-position relative how far window has been scrolled in order simulate fixed positioning still calculate background-size: cover relative container element rather viewport.
Comments
Post a Comment