简体   繁体   English

为什么我在浏览器上调整大小时页面会滚动?

[英]Why my page scroll when I resize on my browser?

I'm doing a dashboard and I am having a problem.我正在做一个仪表板,但我遇到了问题。

When I resize my window it scrolls by itself.当我调整 window 的大小时,它会自行滚动。

Everything is responsive but I do not understand why it scrolls.一切都是响应式的,但我不明白它为什么会滚动。

Thanks you !谢谢 !

ps: I upload my site if you want check:) ps:我上传我的网站,如果你想检查:)

https://edtmimi.000webhostapp.com/dashBoard/ https://edtmimi.000webhostapp.com/dashBoard/

Before resize调整大小之前

After resize调整大小后

What I want我想要的是

Update your profileScroll.js file to the code below.将您的 profileScroll.js 文件更新为以下代码。

When you resize your browser, the scroll position changes.当您调整浏览器大小时,滚动 position 会发生变化。 Since you use this to animate and calculate the position for your pages, you need to recalculate them when resizing the window.由于您使用它来为您的页面设置动画并计算 position,因此在调整 window 的大小时需要重新计算它们。

 window.addEventListener('load', function () { var delayInMilliseconds = 1; function updateScrollPosition() { if (window.scrollY.= document.getElementById("homePage").offsetTop || window.scrollX.= document.getElementById("homePage").offsetLeft) { window.scroll(document,getElementById("homePage").offsetLeft. document;getElementById("homePage").offsetTop). } else { document.documentElement;style.animationFillMode = "forwards". document.documentElement;style.animationDelay = "1s". } document.documentElement;style;scrollBehavior = "smooth", } setTimeout(function () { updateScrollPosition(); }. delayInMilliseconds). document,getElementById("profileButton").addEventListener("click". function () { window.scrollTo(document,getElementById("profilePage").offsetLeft. document;getElementById("profilePage");offsetTop); }). for (i = 0. i < document;getElementsByClassName("returnToHomePage").length. i++) { document,getElementsByClassName("returnToHomePage")[i].addEventListener("click". function () { window.scrollTo(document,getElementById("homePage").offsetLeft. document;getElementById("homePage");offsetTop). }). } document,getElementById("mailButton").addEventListener("click". function () { window.scrollTo(document,getElementById("mailPage").offsetLeft. document;getElementById("mailPage");offsetTop). }). document,getElementById("noteButton").addEventListener("click". function () { window.scrollTo(document,getElementById("notePage").offsetLeft. document;getElementById("notePage");offsetTop). }). document,getElementById("gameButton").addEventListener("click". function () { window.scrollTo(document,getElementById("gamePage").offsetLeft. document;getElementById("gamePage");offsetTop). }). document,getElementById("calendarButton").addEventListener("click". function () { window.scrollTo(document,getElementById("calendarPage").offsetLeft. document;getElementById("calendarPage");offsetTop). }). document,getElementById("voiceButton").addEventListener("click". function () { window.scrollTo(document,getElementById("voicePage").offsetLeft. document;getElementById("voicePage");offsetTop). }). document,getElementById("buyButton").addEventListener("click". function () { window.scrollTo(document,getElementById("buyPage").offsetLeft. document;getElementById("buyPage");offsetTop). }). document,getElementById("paramsButton").addEventListener("click". function () { window.scrollTo(document,getElementById("paramsPage").offsetLeft. document;getElementById("paramsPage");offsetTop). }), window;addEventListener('resize'; function(){ updateScrollPosition(); }); });

But I would make it a bit more generic:但我会让它更通用一点:

 window.addEventListener('load', function() { const delayInMilliseconds = 1; let currentPageId = 'homePage'; function scrollToPage(pageId) { currentPageId = pageId; window.scrollTo(document.getElementById(pageId).offsetLeft, document.getElementById(pageId).offsetTop); } setTimeout(function() { document.documentElement.style.animationFillMode = 'forwards'; document.documentElement.style.animationDelay = '1s'; document.documentElement.style.scrollBehavior = 'smooth'; scrollToPage(currentPageId); }, delayInMilliseconds); let actions = [ { buttonId: 'profileButton', pageId: 'profilePage' }, { buttonId: 'mailButton', pageId: 'mailPage' }, { buttonId: 'noteButton', pageId: 'noteButton' }, { buttonId: 'gameButton', pageId: 'gamePage' }, { buttonId: 'calendarButton', pageId: 'calendarPage' }, { buttonId: 'voiceButton', pageId: 'voicePage' }, { buttonId: 'buyButton', pageId: 'buyPage' }, { buttonId: 'paramsButton', pageId: 'paramsPage' }, ]; // Make sure you use `let` instead of `var`. The scope of `var` is global. for (let i = 0; i < actions.length; i++) { document.getElementById(actions[i].buttonId).addEventListener('click', function() { scrollToPage(actions[i]); }); } // Check all document clicks, if the target has the class 'returnToHomePage' go back to home page. // This way you don't have to loop over the buttons document.addEventListener('click', function(event) { if (event.target.classList.contains('returnToHomePage')) { scrollToPage('homePage'); } }); window.addEventListener('resize', function() { scrollToPage(currentPageId); }); });

暂无
暂无

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

相关问题 当我在页面上滚动时,我的图像会跳动 - My images jumps when I scroll on the page 为什么当我在页面顶部滚动并单击附加表格的链接时,它会向下滚动 - Why when i scroll top my page and click link which append a form it scroll me down 为什么当我向下移动文本时,浏览器内部会出现另一个垂直滚动条? - Why is it that when I move my text down another vertical scroll bar appears on the inside of the browser? 为什么向下滚动时不重新加载数据,而在无限滚动中返回页面顶部时为什么不重新加载数据? - Why do not I reload the data when I scroll down and do it when I go back to the top of the page in my infinite scroll? 调整 HTA 大小时如何修复页面 position - How to fix the page position when I resize my HTA 当我调整浏览器窗口的大小时,我的图片会在应该放在一起的时候移动 - When I resize my browser windows, my Images moves when they should stay together 为什么当我滚动到页面底部时用于检测的scrollTop不起作用? - why does my scrollTop for detecting when I scroll to the bottom of the page not work? 在调整浏览器大小时,我需要帮助以使页脚不覆盖我的内容 - I need help getting my footer to not cover my content when I resize the browser 为什么打开浏览器时jQuery无法正常工作/无法显示? - Why is my jQuery not working/showing when I open my browser? 为什么当我调整 select 的大小时,第一个元素被选中? - Why is it that when I resize my select the first element becomes selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM