2014-06-10 159 views
1

有许多问题关于阻止用户启动(即鼠标,触摸,键盘)通过JavaScript滚动。在这种情况下,我在CMS中创建了一个组件,该组件需要阻止另一个CMS组件滚动到视图中。我无法访问其他CMS组件的代码(这也是严重混淆)。防止JavaScript启动滚动

我曾尝试禁用迫使下面的代码滚动的典型方式,但其他CMS仍然滚动到视图中,第二个或两个以下代码后运行:

Element.prototype.scrollIntoView = function(alignWithTop){ 
    console.log("******** scrollIntoView: " + this.getAttribute("id")); 
    }; 

    Element.prototype.scrollIntoViewIfNeeded = function(alignWithTop){ 
    console.log("******** scrollIntoViewIfNeeded: " + this.getAttribute("id")); 
    }; 

    Element.prototype.scrollByLines = function(){ 
    console.log("******** scrollByLines: " + this.getAttribute("id")); 
    }; 

    Element.prototype.scrollByPages = function(){ 
    console.log("******** scrollByPages: " + this.getAttribute("id")); 
    }; 

    Element.prototype.focus = function(){ 
    console.log("******** focus: " + this.getAttribute("id")); 
    }; 

    window.scroll = function(xpos,ypos){ 
    console.log("******** scroll: " + xpos+","+ypos); 
    } 

    window.scrollTo = function(xpos,ypos){ 
    console.log("******** scrollTo: " + xpos+","+ypos); 
    } 

    window.scrollBy = function(xpos,ypos){ 
    console.log("******** scrollBy: " + xpos+","+ypos); 
    } 

    jQuery.fn.scrollTop = function(){ 
    console.log("!!!!!!!! scrollTop"); 
    }; 

    jQuery.fn.animate = function(){ 
    console.log("!!!!!!!! animate"); 
    }; 

    jQuery.fn.click = function(){ 
    console.log("!!!!!!!! click"); 
    }; 

我可以设置window.onscroll给力在一段时间内致电window.scrollTo(0,0),但这看起来并不令人满意,因为它看起来很刺眼,并且阻止了用户启动的滚动,我不想阻止它。

是否有其他方式的JavaScript(或jQuery)可以强制上述代码中缺少的滚动?

+0

“滚动自己进入视图“ - 它是一个像淡入或类似的动画效果? – Vandesh

+0

尝试返回false时,你认为它不应该滚动。 –

+0

通过“自动滚动到视图中”我的意思是我的组件应该位于页面的顶部,下面提供了CMS提供的组件,名义上滚动页面的底部。但是当它加载时,CMS提供的组件会滚动到视图中。 –

回答

1

因此,这应该是很明显,但CMS被调用函数是HTMLElementObject.focus(),所以下面是我用来停止页面加载和安装过程中不需要滚动代码:

// Stop the page from jumping around for a little while 
var oldFocus = Element.prototype.focus; 
Element.prototype.focus = function(){}; 
setTimeout(
    function(){ 
    Element.prototype.focus = oldFocus; 
    },5000);