Unable to scroll to the overflow part of the element above the page
P粉041881924
2023-08-30 21:41:10
<p>If I resize the page, the elements overflow above the viewport and I can't scroll up to see them. I found a question with a solution but it didn't work. I have two "core" invisible divs and one div element that contains all the content. </p>
<p>The "container" div exists because I'm trying to solve this problem. </p>
<p>
<pre class="brush:css;toolbar:false;">.container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 500;
}
.main-content {
display: flex;
width: 100%;
height: 100%;
overflow: auto;
margin: auto;
}
.window {
/*height: 16vw;
width: 27vw;*/
display: flex;
height: 550px;
width: 800px;
position: absolute;
top: 50%;
left: 50%;
border: solid blue 5px;
background-color: black;
margin: auto;
overflow: hidden;
}</pre>
<pre class="brush:html;toolbar:false;"><div class="container">
<div class="main-content">
<div class="window" id="window1" style="transform: translate(-10%, -90%);">
<div class="header" id="window-header">
<img src="https://picsum.photos/800/550">
<p class="title">navigation</p>
</div>
</div>
</div>
</div></pre>
</p>
For this problem, I think the only solution is to make a custom scrollbar (if you have to place the image above the viewport)
This is the code I made based on your code
EXPLANATION
Basically, I made a scrollbar using HTML and CSS and placed it on the far right side of the image
Hope HTML and CSS can be understood Now, let's look at the JavaScript part
First, we map the mouse coordinates
We create an event listener that calls a function or changes the stop drag variable
Then we create the startDrag() function
In this function, first we set stopdrag to false because the user is currently dragging the scroll bar Then we set up an interval loop code According to the coordinates, mouse.y is mostly trial and error. We check if it is within the limits and if so we relocate Then we change the top position of the scrollbar while dragging (calculation is trial and error) Then we change the bottom position of the window class div to reposition the window class div (because the image itself cannot be repositioned; if you don't want the whole window class div to move, you can create another container on top of it) to view it If dragging stops, we clear the interval
Thanks to @TylerH for resolving this issue. The problem is with the transform style in my HTML. After removing it, it scrolls correctly. From what I've seen, it seems to offset the scrolling and seems to move the entire page instead of just the elements. Thanks for everyone's help, but I've figured it out.