简体   繁体   English

如何滚动到 iframe 的顶部

[英]how to scroll to top of the iframe

I'm using a different website(already active) into my webpage, but that website page is too big in height.我在我的网页中使用了不同的网站(已经处于活动状态),但该网站页面的高度太大。 So after restricting its height to 100vh (or some height).因此,在将其高度限制为 100vh(或某个高度)之后。 Scrolling down through the iframe is okay, but I need a floating ' scroll to top button ' for iframe which will bring the iframe scroll to the top (NOT the parent window scroll). Scrolling down through the iframe is okay, but I need a floating ' scroll to top button ' for iframe which will bring the iframe scroll to the top (NOT the parent window scroll).

I have used this code in .ts file of Angular , and it worked for the parent window scroll, but NOT for <iframe> PS - I'm using Angular 7我在Angular.ts文件中使用了此代码,它适用于父 window 滚动,但不适用于<iframe> PS - 我使用的是 Angular 7

scrollToTop(){
    window.scroll(0,0);
  }

.html code <button (click)="scrollToTop()">TOP</button> Thank you in advance. .html代码<button (click)="scrollToTop()">TOP</button>提前谢谢您。

Update : I have tried the following suggested way document.getElementById("myIframe").scroll(0,0);更新:我尝试了以下建议的方式document.getElementById("myIframe").scroll(0,0); I'm able to print the innerHtml, that means getElementById is working fine, the issue is with .scroll(0,0) Had tried .scrollTo(0,0) & .scrollBy(0,0) too but results the same我能够打印 innerHtml,这意味着 getElementById 工作正常,问题出在.scroll(0,0)也尝试过.scrollTo(0,0) & .scrollBy(0,0)但结果相同

Just do the same but replace the window with the iframe element:只需执行相同操作,但将 window 替换为 iframe 元素:

document.getElementById(<iFrameId>).scroll(0,0); 

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

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