调整图像大小以适合屏幕
考虑到导航栏的可用空间,我创建了此代码来调整照片/图像的大小以适合屏幕。
该脚本在图像加载和导航单击时触发。
有没有人有关于改进这一点并确保浏览器兼容性的建议?
HTML
$(document).ready(function(){
$("#photo").load(function(){
resize();
});
$(".navigation img").click(function(){
var imgPath = $(this).attr("src");
$("#photo").attr({ src: imgPath });
resize();
return false;
});
});
JavaScript
resize = function() {
var borderVt=150; //value based on css style. bottom bar + padding of photoContain
var borderHz=40; //value based on css style photoContain padding
$("#photo").css("width", "auto").css("height", "auto"); // Remove existing CSS
$("#photo").removeAttr("width").removeAttr("height"); // Remove HTML attributes
var origSizeW = $("#photo").width();
var origSizeH = $("#photo").height();
var ratioVt=(origSizeW/origSizeH);
var ratioHz=(origSizeH/origSizeW);
var winW = $(window).width();
var winH = $(window).height();
var screenSizeW=Math.round(winW-borderHz);
var screenSizeH=Math.round(winH-borderVt);
if (origSizeW>=origSizeH){
var newHeight = Math.round(screenSizeW*ratioHz);
if (newHeight <= screenSizeH){
$("#photo").css("width", screenSizeW); // Set new width
$("#photo").css("height", newHeight);
}
else{
$("#photo").css("height", screenSizeH);
}
}
else{
$("#photo").css("height", screenSizeH); // Set new height
}
};
I created this code to resize photos/images to fit the screen, considering the space available for the nav bar.
The script fires on image load and on navigation click.
Does anyone have suggestions as to making this better and ensuring browser compatibility?
HTML
$(document).ready(function(){
$("#photo").load(function(){
resize();
});
$(".navigation img").click(function(){
var imgPath = $(this).attr("src");
$("#photo").attr({ src: imgPath });
resize();
return false;
});
});
Javascript
resize = function() {
var borderVt=150; //value based on css style. bottom bar + padding of photoContain
var borderHz=40; //value based on css style photoContain padding
$("#photo").css("width", "auto").css("height", "auto"); // Remove existing CSS
$("#photo").removeAttr("width").removeAttr("height"); // Remove HTML attributes
var origSizeW = $("#photo").width();
var origSizeH = $("#photo").height();
var ratioVt=(origSizeW/origSizeH);
var ratioHz=(origSizeH/origSizeW);
var winW = $(window).width();
var winH = $(window).height();
var screenSizeW=Math.round(winW-borderHz);
var screenSizeH=Math.round(winH-borderVt);
if (origSizeW>=origSizeH){
var newHeight = Math.round(screenSizeW*ratioHz);
if (newHeight <= screenSizeH){
$("#photo").css("width", screenSizeW); // Set new width
$("#photo").css("height", newHeight);
}
else{
$("#photo").css("height", screenSizeH);
}
}
else{
$("#photo").css("height", screenSizeH); // Set new height
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我知道这个问题已经很老了,但这里有一个解决方案(尽管我确信OP也能正常工作):
这个 jQuery 插件似乎完全满足您的需要:
http://code.google.com/p/jquery-imagefit-plugin/
如果在 100% 高度、100% 宽度元素上执行:
http://www.tutwow.com/htmlcss/quick-tip- css-100-height/
(演示:http://jsfiddle.net/nottrobin/9Pbdz/< /a>)
I know this question is well old, but here's a solution (although I'm sure the OP's works fine too):
This jQuery plugin seems to do exactly what you need:
http://code.google.com/p/jquery-imagefit-plugin/
if you perform it on a 100% height, 100% width element:
http://www.tutwow.com/htmlcss/quick-tip-css-100-height/
(demo: http://jsfiddle.net/nottrobin/9Pbdz/)
尝试使用 jQuery-Backgrounder 插件。您也许可以调整它来完成您需要的操作。这是一个例子:
Try using the jQuery-Backgrounder plugin. You might be able to tweak it to do what you need. Here is an example:
我写了一个插件!
用法:
I wrote a plugin!
Usage:
我是这样做的:
Here is how I do it:
对我来说看起来不错,但我建议将调整大小函数附加到 jQuery 的窗口调整大小事件处理程序。然后图像将随着页面拉伸和收缩。
Looks good to me, but I would suggest attaching your resize function to jQuery's Window Resize Event handler. Then the image will stretch and shrink with the page.