Javascript 在IE8中设置问题的动画
我正在尝试使用jQuery通过滚动背景显示iPhone应用程序的屏幕截图。我编写的代码在FF&Safari中工作得非常好,但是IE抛出了一个“无效参数”错误 /编辑:我想我应该添加IE在失败时实际做的事情。它跳转到第二个屏幕截图并抛出错误。其他浏览器都可以平滑地设置动画,但IE不会设置动画,它只是跳转到第二个屏幕截图,然后抛出错误 这是我的密码:Javascript 在IE8中设置问题的动画,javascript,jquery,internet-explorer,jquery-animate,Javascript,Jquery,Internet Explorer,Jquery Animate,我正在尝试使用jQuery通过滚动背景显示iPhone应用程序的屏幕截图。我编写的代码在FF&Safari中工作得非常好,但是IE抛出了一个“无效参数”错误 /编辑:我想我应该添加IE在失败时实际做的事情。它跳转到第二个屏幕截图并抛出错误。其他浏览器都可以平滑地设置动画,但IE不会设置动画,它只是跳转到第二个屏幕截图,然后抛出错误 这是我的密码: var scrollInterval = 5000; // scroll every 5 seconds // set the defaul
var scrollInterval = 5000; // scroll every 5 seconds
// set the default position
var current = 1284;
var screenScroll = function(){
var imageSize = 1284;
var screenshotWidth = 214;
current -= screenshotWidth;
if ( current < screenshotWidth) {
current = imageSize;
}
var bgPos = current + "px top";
$("#screenshotDiv").animate({backgroundPosition:bgPos}, 500);
}
$(document).ready(function() {
//Calls the scrolling function repeatedly
var init = setInterval(screenScroll, scrollInterval);
});
var scrollInterval=5000;//每5秒滚动一次
//设置默认位置
无功电流=1284;
var screensroll=函数(){
var-imageSize=1284;
var screenshotWidth=214;
当前-=屏幕截图宽度;
if(当前<屏幕截图宽度){
电流=图像大小;
}
var bgPos=电流+“px顶部”;
$(#screenshotDiv”).animate({backgroundPosition:bgPos},500);
}
$(文档).ready(函数(){
//反复调用滚动函数
var init=设置间隔(屏幕滚动,滚动间隔);
});
IE要求第二个参数为{duration:500},对吗?或者这只是惯例
事实上,环顾四周,IE8与top一起使用时背景位置被破坏:
也许你可以试试bottom?我无法仅使用jQuery在IE8上实现这一点。显然,IE上的jQuery背景位置动画有一个bug。令人惊讶的是,令人惊讶的是,IE在其他所有现代浏览器上都存在一些问题
不用担心,我找到了解决办法。在包含jQuery之后包含了这个脚本,它将解决这个问题。我试图找到该插件的官方链接,但放弃了寻找最新的链接,因此我只发布文件:
/**
* @author Alexander Farkas
* v. 1.21
*/
(function($) {
if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
var oldCurCSS = jQuery.curCSS;
jQuery.curCSS = function(elem, name, force){
if(name === 'background-position'){
name = 'backgroundPosition';
}
if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
return oldCurCSS.apply(this, arguments);
}
var style = elem.style;
if ( !force && style && style[ name ] ){
return style[ name ];
}
return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
};
}
var oldAnim = $.fn.animate;
$.fn.animate = function(prop){
if('background-position' in prop){
prop.backgroundPosition = prop['background-position'];
delete prop['background-position'];
}
if('backgroundPosition' in prop){
prop.backgroundPosition = '('+ prop.backgroundPosition;
}
return oldAnim.apply(this, arguments);
};
function toArray(strg){
strg = strg.replace(/left|top/g,'0px');
strg = strg.replace(/right|bottom/g,'100%');
strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
}
$.fx.step. backgroundPosition = function(fx) {
if (!fx.bgPosReady) {
var start = $.curCSS(fx.elem,'backgroundPosition');
if(!start){//FF2 no inline-style fallback
start = '0px 0px';
}
start = toArray(start);
fx.start = [start[0],start[2]];
var end = toArray(fx.options.curAnim.backgroundPosition);
fx.end = [end[0],end[2]];
fx.unit = [end[1],end[3]];
fx.bgPosReady = true;
}
//return;
var nowPosX = [];
nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
};
})(jQuery);
该代码现在在IE8中完美地工作。我没有费心在IE7中测试,因为我只有13%的流量来自IE,所以我不会费心支持旧版本。。。。太让人头痛了。亚历山大·法卡斯的脚本解决方案是可行的,但它首先需要一点修正(我需要这样才能使它生效)。行:
必须在
var end = toArray(fx.end);
我尝试过用0px代替top,结果是相同的,以防我继续尝试用bottom代替。它仍然抛出相同的错误。
var end = toArray(fx.end);