Javascript iOS使用iScroll.js的动画锚滚动方法
我用它来帮助你在iOS上滚动动画。基本上,它使用硬件加速的CSS属性来移动内容,而不是传统的滚动 iScoll工作得很好,但我也在尝试实现一个平滑滚动锚定解决方案 它在桌面上运行良好,但问题是我无法确定如何检索正确的偏移量值以传递给iScoll实例。我觉得我非常接近一个解决方案:Javascript iOS使用iScroll.js的动画锚滚动方法,javascript,jquery,ios,iscroll,iscroll4,Javascript,Jquery,Ios,Iscroll,Iscroll4,我用它来帮助你在iOS上滚动动画。基本上,它使用硬件加速的CSS属性来移动内容,而不是传统的滚动 iScoll工作得很好,但我也在尝试实现一个平滑滚动锚定解决方案 它在桌面上运行良好,但问题是我无法确定如何检索正确的偏移量值以传递给iScoll实例。我觉得我非常接近一个解决方案: var ua = navigator.userAgent, isMobileWebkit = /WebKit/.test(ua) && /Mobile/.test(ua); if (isMob
var ua = navigator.userAgent,
isMobileWebkit = /WebKit/.test(ua) && /Mobile/.test(ua);
if (isMobileWebkit) {
iScrollInstance = new iScroll('wrapper');
}
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
|| location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
if (isMobileWebkit) {
iScrollInstance.refresh();
/* issue here with "target.position.top" = "undefined" */
iScrollInstance.scrollTo(0, target.position.top, 1000);
}else{
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
}
return false;
}
}
});
这里的完整演示不确定原因,但iScroll.js似乎不喜欢scrollTo函数中的jQuery。因此,提前设置变量似乎有帮助。我还必须计算出正确的偏移量,因为iScroll移动div的方式,而不是屏幕 如果有人需要,只要您正确设置iScroll,此jQuery将有助于在IOs设备上平滑滚动定位:
var ua = navigator.userAgent,
isMobileWebkit = /WebKit/.test(ua) && /Mobile/.test(ua);
if (isMobileWebkit) {
$('html').addClass('mobile');
}
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
|| location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
if (isMobileWebkit) {
scrollOffset = $('#scroller').position().top;
targetOffset = target.offset().top;
iScrollInstance.refresh();
iScrollInstance.scrollTo(0, -(targetOffset-scrollOffset), 1000);
}else{
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
}
return false;
}
}
});