简体   繁体   中英

jQuery - Simulate background-size cover

I have two panels on a site with images inside which are always the full width and height of the screen.

Fiddle

.panel {
    float:left;
    width:50%;
    position:relative;
    background-size:cover
}
.panel img {
    position:absolute;
    top:0;
    left:0;
    width:100%;
    height:100%;
}

Basically I am trying to get the same result as background size (left panel) for the right panel which has the img tag inside.

I've tried a few different things but have had no luck in keeping a good image ratio when the page is scaled like background-size

Does anyone know any tricks or a good way to do this with jquery?

To solve the issue for IE - make the image width pixel-based. I changed the width to 800px in the fiddle and it straightened out.

Like this:

.panel {
    float:left;
    width:50%;
    position:relative;
    background-size:cover
}
.panel img {
    position:absolute;
    top:0;
    left:0;
    width:800px;
    height:100%;
}

Here's a fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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