简体   繁体   English

带有iScroll.js的iOS动画锚滚动方法

[英]animated anchor scrolling method for iOS with iScroll.js

Im using iScoll.js to help with scrolling animations on iOS. 我使用iScoll.js帮助在iOS上滚动动画。 Basically it uses hardware accelerated CSS properties to move the content, not traditional scrolling. 基本上,它使用硬件加速的CSS属性来移动内容,而不是传统的滚动。

iScoll is working well but I'm also trying to implement a smooth scrolling anchor solution. iScoll运作良好,但我也在尝试实现平滑的滚动锚点解决方案。

It works fine on desktop but the problem is that I can't workout how to retrieve the correct offset value to pass to the iScoll Instance. 它在台式机上运行良好,但问题是我无法锻炼如何检索正确的偏移值以传递给iScoll实例。 I feel like im super close to a solution: 我觉得我非常接近解决方案:

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;
            }
        }
    });

full demo here http://jsfiddle.net/Wccev/3/ 完整的演示在这里http://jsfiddle.net/Wccev/3/

Not sure why but it seems iScroll.js didn't like jQuery in the scrollTo function. 不知道为什么,但是iScroll.js似乎不喜欢scrollTo函数中的jQuery。 So setting the variable ahead of time seemed to help. 因此,提前设置变量似乎有所帮助。 I also had to work out the correct offset because of the way iScroll moves a div not the screen. 我还必须计算出正确的偏移量,因为iScroll会移动div而不是屏幕。

If anyone needs it, this jQuery should help with smooth scrolling anchors on IOs devices provided you setup iScroll correctly: 如果有人需要,只要您正确设置了iScroll,此jQuery应该有助于在IO设备上平滑地滚动锚点:

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;
            }
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM