作为一个快速的解释,我创建了重新调整使用此功能伟大的工程,填补了背景的图像:使用Javascript - 图像变化的大小调整,以全面的背景
function resize_bg(element_id){
$("#"+element_id).css("left","0");
var doc_width = $(window).width();
var doc_height = $(window).height();
var image_width = $("#"+element_id).width();
var image_height = $("#"+element_id).height();
var image_ratio = image_width/image_height;
var new_width = doc_width;
var new_height = Math.round(new_width/image_ratio);
if(new_height<doc_height){
new_height = doc_height;
new_width = Math.round(new_height*image_ratio);
var width_offset = Math.round((new_width-doc_width)/2);
$("#"+element_id).css("left","-"+width_offset+"px");
}
$("#"+element_id).width(new_width);
$("#"+element_id).height(new_height);
return true;
}
所以对于全背景图像没有问题。当我使用Javascript更改图像源时,问题就出现了。换句话说,我将1个图像设置为背景,但将某些元素悬停在页面上时,图像会更改,但不会更改右侧的调整大小。因此,加载的第一个图像被调整大小并正确定位,但是当我使用.attr('src',newimg)切换图像时,即使再次调用该函数,图像也没有正确调整大小。
下面是我用它来改变形象,并调整它的代码:
$('#menu_work li a').hover(function(){
$('#content').hide();
var img_src = $(this).next('img').attr('src');
$('#full_screen_img').attr('src', img_src);
resize_bg();
$('#full_screen_img').show();
},function(){
$('#full_screen_img').hide();
$('#content').show();
});
感谢您的帮助。
你可以在代码中调用'resize_bg'函数吗?加载和元素悬停。 – satoshi 2012-02-18 17:45:25
@micha - 是的,请查看问题中添加的代码。 – denislexic 2012-02-18 17:50:21
您是否确认过'img_src'的值是您期望的值? – satoshi 2012-02-18 17:53:31