在Chrome DevTools调试React/Ionic应用时,如何用控制台JS实现页面滚顶?
React/Ionic 标签页切换后无法滚动到顶部问题
我正在开发一款React/Ionic应用,某单页模块包含多个标签页。切换标签页时新内容正常显示,但页面会保留之前的滚动位置。即使在浏览器以Ionic模式调试应用,控制台输入常规JS代码也无法强制页面滚动到顶部。我曾在useEffect中尝试相关代码但无效,于是直接在控制台编写JS,虽能获取到DOM元素,但调用以下方法均无效:
document.querySelector('ion-app').scroll(0,0) document.querySelector('ion-app').scrollHeight = 0 document.querySelector('ion-page').scrollIntoView document.getElementsByClassName('account-settings-container')[0].scroll(0,0) document.getElementsByClassName('account-settings-container')[0].scrollTop(0,0);
更新:奇怪的是,使用精确DOM选择器时该功能生效:
document.querySelector("#root-app > ion-app > div:nth-child(4) > ion-router-outlet > div > ion-content > div > div").scrollIntoView();
内容的提问来源于stack exchange,提问作者Wes Creations
相关产品推荐
相关产品推荐

