【发布时间】:2011-06-25 23:57:00
【问题描述】:
我正在用 jQuery 做全屏背景更改系统。 当进入站点时,将全屏大小设为默认背景,当单击按钮时必须更改背景。在歌剧上一切正常!但是FireFox没有发生。我认为问题在于 attr 函数,请帮助发现问题。
你可以在http://www.hiphopdance.lt看到所有这些
$(document).ready(function(){
//default actions
var now_img="images/bg.jpg";
resize(1600,900,"#bgimg",now_img);
$(window).bind("resize", function() { resize(1600,900,"#bgimg"); });
//default actions end
//clicks
$('li#red').click(function(){
$("img#bgimg").attr({src:'http://www.hiphopdance.lt/images/redbg.jpg'});
resize(1024,683,"#bgimg");
$(window).bind("resize", function() { resize(1024,683,"#bgimg"); });
});
//end clicks
//resize function start
function resize(img_width,img_height,img_id)
{
var ratio = img_height / img_width;
// Get browser window size
var browserwidth = $(window).width();
var browserheight = $(window).height();
// Scale the image
if ((browserheight/browserwidth) > ratio){
$(img_id).height(browserheight);
$(img_id).width(browserheight / ratio);
} else {
$(img_id).width(browserwidth);
$(img_id).height(browserwidth * ratio);
}
// Center the image
$(img_id).css('left', (browserwidth - $(img_id).width())/2);
$(img_id).css('top', (browserheight - $(img_id).height())/2);
};
//resize function end
});
感谢您的回答,我已尝试从
更改$("img#bgimg").attr({src:'http://www.hiphopdance.lt/images/redbg.jpg'});
到
$("img#bgimg").attr("src","http://www.hiphopdance.lt/images/redbg.jpg");
仍然适用于 Opera,但不适用于 firefox / IE
【问题讨论】:
-
顺便说一句,你可以使用 $(function () { });而不是 $(document).ready(function(){ });