动画与Jquery的结果不一致
我有一个用jQuery设计的索引页动画,它的行为并不一致(所有部分都应该以逻辑方式居中)。可在以下网址查看。如果你把它刷新几次,你会发现它在谷歌浏览器中并不总是以同一个位置结束,但在所有其他浏览器中都能正常工作。要在chrome中查看此问题,页面需要刷新几次。任何帮助都将不胜感激,下面列出了.js文件的代码动画与Jquery的结果不一致,jquery,animation,Jquery,Animation,我有一个用jQuery设计的索引页动画,它的行为并不一致(所有部分都应该以逻辑方式居中)。可在以下网址查看。如果你把它刷新几次,你会发现它在谷歌浏览器中并不总是以同一个位置结束,但在所有其他浏览器中都能正常工作。要在chrome中查看此问题,页面需要刷新几次。任何帮助都将不胜感激,下面列出了.js文件的代码 // JavaScript Document $(document).ready(function() { $(".intro-no-js").css("visibility","hid
// JavaScript Document
$(document).ready(function() {
$(".intro-no-js").css("visibility","hidden");
$(".intro-javascript").css("visibility","visible");
//defines how max should be animated
jQuery.fn.maxanimate = function () {
this.css("position","absolute");
var t = (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop();
var l = (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft();
$(this).animate({
top: t,
left: l-79.5
}, 1000);
return this;
}
//defines how xim should be animated
jQuery.fn.ximanimate = function () {
this.css("position","absolute");
var t = (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop();
var r = (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft();
$(this).animate({
top: t,
left: r+78.5
}, 1000);
return this;
}
//defines how top arrows should be animated
jQuery.fn.arrowsanimate = function () {
this.css("position","absolute");
var t = (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop();
var l = (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft();
$(this).animate({
top: t-25,
left: l
}, 1000);
return this;
}
//defines how bottom section should be animated
jQuery.fn.animatebottom = function () {
this.css("position","absolute");
var b = (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop();
var l = (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft();
$(this).animate({
bottom: b-27,
left: l
}, 1000);
return this;
}
//max starting co-ordinates
var maxl = $(window).width() - 157;
var maxt = ($(window).height() - $("#intro-max").outerHeight()) / 2;
//xim starting co-ordinates
var ximr = $(window).width() - 159;
var ximt = ($(window).height() - $("#intro-xim").outerHeight()) / 2;
//arrows starting co-ordinates
var al = (($(window).width() - $("#intro-xim").outerWidth()) / 2) + $(window).scrollLeft()+ 35;
var at = 0;
//bottom of logo starting co-ordinates
var bl = (($(window).width() - $("#intro-xim").outerWidth()) / 2) + $(window).scrollLeft()-90;
var bt = 0;
//set initial co-ordinates for all divs
$("#intro-arrows").css({position: "absolute",top: at,left: al});
$("#intro-max").css({position: "absolute",top: maxt,right: maxl});
$("#intro-xim").css({position: "absolute",top: ximt,left: ximr});
$(".intro-bottom").css({position: "absolute", bottom: bt, left: bl});
//lets bring it all to life!
$("#intro-max").maxanimate();
$("#intro-xim").ximanimate();
$("#intro-arrows").arrowsanimate();
$(".intro-bottom").animatebottom();
//refresh window if resized
$(window).bind('resize',function(){
window.location.href = window.location.href;
});
$("#sales").click(function() {
window.location.href = "main.html?page=1&sel=1";
});
$("#design").click(function() {
window.location.href = "main.html?page=2&sel=1";
return false;//Just in case if it is a link prevent default behavior
});
});
问题在于,虽然这些元素在最初运行javascript时确实存在于DOM中,但它们实际上尚未在浏览器中呈现,因此还没有实际的宽度或高度。由于您使用了
outerWidth
和outerHeight
这两个选项,在页面加载时错误地返回了0,因此放置处于关闭状态
解决这个问题的一个简单方法是,不要在
$(文档)上调用初始动画。准备好了
,而是在$(窗口)上。加载
,在渲染元素和下载图像后触发。我使用的是FF11,动画口吃得很厉害,但在重新加载30页之后,只会有一次定位错误。铬16似乎在50%的时间里都会出现问题。你已经注意到,有人能解决这些问题吗?