使用纯Javascript在WKWebView中滚动动画
我使用下面的代码,它应该以给定的偏移量和给定的动画持续时间滚动webview。但它不起作用。我对Javascript不是很有经验使用纯Javascript在WKWebView中滚动动画,javascript,ios,objective-c,ios9,wkwebview,Javascript,Ios,Objective C,Ios9,Wkwebview,我使用下面的代码,它应该以给定的偏移量和给定的动画持续时间滚动webview。但它不起作用。我对Javascript不是很有经验 - (NSString *)jsCodeScrollTo:(NSInteger)offset withAnimationDuration:(NSInteger)animationDuration { return [NSString stringWithFormat:@" scrollTo(document.documentElement, %ld, %ld)
- (NSString *)jsCodeScrollTo:(NSInteger)offset withAnimationDuration:(NSInteger)animationDuration
{
return [NSString stringWithFormat:@" scrollTo(document.documentElement, %ld, %ld); function scrollTo(element, to, duration) { if (duration < 0) return; var difference = to - element.scrollTop; var perTick = difference / duration * 10; setTimeout(function() { element.scrollTop = element.scrollTop + perTick; if (element.scrollTop === to) return; scrollTo(element, to, duration - 10); }, 10); }", offset, animationDuration];
}
-(NSString*)jscode滚动到:(NSInteger)带animationDuration的偏移量:(NSInteger)animationDuration
{
return[NSString stringWithFormat:@“scrollTo(document.documentElement,%ld,%ld);函数scrollTo(element,to,duration){if(duration<0)return;var difference=to-element.scrollTop;var perTick=difference/duration*10;setTimeout(函数(){element.scrollTop=element.scrollTop+perTick;if(element.scrollTop==to)返回;滚动到(元素,到,持续时间-10);},10);}”,偏移量,动画持续时间];
}
它输出:
scrollTo(document.documentElement, 2841, 2000);
function scrollTo(element, to, duration) { if (duration < 0) return;
var difference = to - element.scrollTop;
var perTick = difference / duration * 10;
setTimeout(function() { element.scrollTop = element.scrollTop + perTick;
if (element.scrollTop === to) return;
scrollTo(element, to, duration - 10); }, 10); }
scrollTo(document.documentElement,28412000);
函数scrollTo(元素,to,持续时间){if(持续时间<0)返回;
var差异=到-element.scrollTop;
var perTick=差异/持续时间*10;
setTimeout(function(){element.scrollTop=element.scrollTop+perTick;
如果(element.scrollTop==to)返回;
滚动到(元素,到,持续时间-10);},10);}
我使用了此线程中发布的代码:
JS代码有问题吗?或者在WebKit中运行时出现问题(兼容性问题)?解决方案是,首先插入JS函数定义,然后调用它
- (void)scrollTo:(NSInteger)offset withAnimationDuration:(NSInteger)animationDuration {
[self insertJavascriptAnimationFunction];
[webView evaluateJavaScript:[NSString stringWithFormat:@"scrollTo(document.body, %ld, %ld);", offset, animationDuration]
completionHandler:nil];
}
- (void)insertJavascriptAnimationFunction
{
[webView evaluateJavaScript:@"function scrollTo(element, to, duration) { var start = element.scrollTop, change = to - start, currentTime = 0, increment = 20; var animateScroll = function(){ currentTime += increment; var val = Math.easeInOutQuad(currentTime, start, change, duration); element.scrollTop = val; if(currentTime < duration) { setTimeout(animateScroll, increment); } }; animateScroll(); } Math.easeInOutQuad = function (t, b, c, d) { t /= d/2; if (t < 1) return c/2*t*t + b; t--; return -c/2 * (t*(t-2) - 1) + b; };"
completionHandler:nil];
}
-(无效)滚动至:(NSInteger)带动画持续时间的偏移量:(NSInteger)动画持续时间{
[自插入JavaScriptAnimationFunction];
[webView evaluateJavaScript:[NSString stringWithFormat:@“scrollTo(document.body,%ld,%ld);”,偏移量,动画持续时间]
completionHandler:nil];
}
-(void)insertJavascriptAnimationFunction
{
[webView evaluateJavaScript:@“函数scrollTo(element,to,duration){var start=element.scrollTop,change=to-start,currentTime=0,increment=20;var animateScroll=function(){currentTime+=increment;var val=Math.easeInOutQuad(currentTime,start,change,duration);element.scrollTop=val;if(currentTime