JavaScript window.scrollTo Alternative - Stack Overflow

I'm writing a specific javascript plugin for a specific website.In one of my methods i want to use

I'm writing a specific javascript plugin for a specific website.

In one of my methods i want to use window.scrollTo but unfortunately the site owner has overridden the native functionality of this function (also window.scroll).

So i'm thinking about 2 possible solutions:

  1. Write my own myScrollTo implementation
  2. Somehow detect if the native function is overridden and call the native function.

If you have any ideas on this, i will be very glad to hear them :)

Thanks.

I'm writing a specific javascript plugin for a specific website.

In one of my methods i want to use window.scrollTo but unfortunately the site owner has overridden the native functionality of this function (also window.scroll).

So i'm thinking about 2 possible solutions:

  1. Write my own myScrollTo implementation
  2. Somehow detect if the native function is overridden and call the native function.

If you have any ideas on this, i will be very glad to hear them :)

Thanks.

Share Improve this question asked Feb 21, 2016 at 17:03 dimshikdimshik 1,28115 silver badges18 bronze badges 2
  • 1 there's also window.scroll which is basically identical. Is it overridden as well? – Prinzhorn Commented Feb 21, 2016 at 17:05
  • @Prinzhorn Overriden as well :( – dimshik Commented Feb 21, 2016 at 17:07
Add a ment  | 

3 Answers 3

Reset to default 3

Well you can create an iframe and use its window instance to get the native implementation of scrollTo. The following code should work

var iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
window.altScrollTo = iframe.contentWindow.scrollTo;

Now you should be able to use it as below

aScrollTo.call(window, 0, 600)

Well it's not the perfect solutions but is suites my current needs.

First i will check if the native function is overridden with this helpful piece of code :

function isFuncNative(f) {
       return !!f && (typeof f).toLowerCase() == 'function' 
       && (f === Function.prototype 
       || /^\s*function\s*(\b[a-z$_][a-z0-9$_]*\b)*\s*\((|([a-z$_][a-z0-9$_]*)(\s*,[a-z$_][a-z0-9$_]*)*)\)\s*{\s*\[native code\]\s*}\s*$/i.test(String(f)));
}

Source: https://stackoverflow./a/7536972/3009194

Then i will try the alternatives : window.scroll as the is no difference between window.scroll() and window.scrollTo()

And finally if this one is also overridden, i guess i will use document.body.scrollTop

Yes i know, there is a possibility that the body is not the scrolling Element. Unfortunately the document.scrollingElement is still a draft and not supported in most browsers.

So the final code will look something like this:

   function myScroll(left, top) {
       if (isFuncNative(window.scrollTo)) {
           window.scrollTo(left, top);
       } else if (isFuncNative(window.scroll)) {
           window.scroll(left, top);
       } else {
           document.body.scrollLeft = left;
           document.body.scrollTop = top;
       }
   }

   myScroll(0,150);

You can use the scrollIntoView method.

const element = document.getElementById("content");
element.scrollIntoView();

https://developer.mozilla/en-US/docs/Web/API/Element/scrollIntoView

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744889042a4599292.html

相关推荐

  • JavaScript window.scrollTo Alternative - Stack Overflow

    I'm writing a specific javascript plugin for a specific website.In one of my methods i want to use

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信