doubu1853 2015-10-13 23:08
浏览 48

通过CSS与服务器端PHP在浏览器中扩展图像

I'm building a Wordpress theme that will use imported posts from another site. These posts have images that vary in size and aspect ratio. There are predefined areas on the new theme where these image must fit, and fill the area, with centering and without letterboxing. Without breaking the semantics of the img tag by using background images, I've put together a few techniques I've seen elsewhere to scale images and fit/center them to a panel, like this:

CSS:

.fillwidth { 
 width: 100%; 
 height: auto; 
 position: absolute;
 top: 50%;              
 transform: translateY(-50%);       
}
.fillheight { 
 height: 100%; 
 width: auto; 
 position: absolute;
 left: 50%;
 transform: translateX(-50%);
}

div {
 border: 1px solid black;
 overflow: hidden;
 position: relative;
}

JS:

<script src="http://localhost/prefixfree.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">    </script>
<script>
(function($){
 $('div').each(function() {
 var fillClass = ($(this).height() > $(this).width()) 
 ? 'fillheight'
 : 'fillwidth';
 $(this).find('img').addClass(fillClass);
 });
})(window.jQuery);
</script>

HTML:

<div>
 <img src="test.jpg" />
</div>

So basically the JS determines the aspect ratio, the image is fit to the box and a transform centers it.

Now my question is, on a high traffic Wordpress site, would you recommend allowing the user's browser to do this resizing on the front end, or should the theme create multiple versions of uploaded images to fit the various panels on the site in advance? With modern browsers what is the impact of asking the browser to do the scaling and resizing of images?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?