dongqiancui9194 2012-07-10 14:40
浏览 155
已采纳

javascript将图像调整为全屏并保持纵横比

I have a mobile slideshow that I want to have a fullscreen option for. I have created a pseudo-fullscreen CSS that works fine but it stretches the image to fill. I have tried some techniques like wrapping it in a div and setting the CSS to auto for height and width but it still stretches.

The CSS just sets all to 0 top bottom left and right. I know its a "hack" but this works great for my HTML5 video it even keeps the ratio. not so hot for images though. I have tried the CSS3 object-fit but that has little to no browser support afaik.

Is there a way to get the image to go fullscreen and keep the aspect ratio? maybe using a letterbox like look to fill the other space(either vertically or horizontally). I need this to be dynamic so it will work on multiple mobile devices with different screen sizes. I have seen this work on google images on my mobile devices but obviously they are more clever than I am.

I am using jQuery Mobile and PHP on the back end. I was thinking a JS solution would be best but PHP would be welcome too.

  • 写回答

1条回答 默认 最新

  • dongyong3223 2012-07-10 16:08
    关注

    I believe you're looking for the background-size property. You can set it to either contain or cover as of CSS3.

    contain, which specifies that the background image should be scaled to be as large as possible while ensuring both its dimensions are less than or equal to the corresponding dimensions of the background positioning area.

    cover, which specifies that the background image should be scaled to be as small as possible while ensuring both its dimensions are greater than or equal to the corresponding dimensions of the background positioning area.

    MDN Docs: https://developer.mozilla.org/en/CSS/background-size

    background-size : cover would be a full-screen image and background-size : contain would be the letterbox effect.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)