Page does not scroll up when dragging an element to top with fixed navigation bar

Viewed 930

I have elements on a page that needs to be draggable to the top or bottom of the page - if there is a long list of elements, the page should automatically scroll up or down while dragging the elements.

I find no issue with scrolling when dragging an element from the top to the bottom of the page, however, if the element that I want to drag is from the bottom to the top, I found that the fixed top navigation bar does not allow the page to scroll up.

Here is the JSFiddle

.App {
  font-family: sans-serif;
  text-align: center;
}

h1 {
  background-color: teal;
  width: 100%;
  position: fixed;
  z-index: 100;
  top: -25px;
  height: 50px;
}
<html>
  <body>
    <div class="App">
      <h1>This is a fixed navbar</h1>
      <p>item</p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
      <p draggable="true">
        Draggable paragraph. Page should scroll when you drag up or down
      </p>
    </div>
  </body>
</html>

I would like to see first if there is a way to fix this in HTML / CSS, if not, in JavaScript.

1 Answers

Below is sample code with fixed top, right and left navs and a centre container that can scroll. The important things to note are the heights give to each element. The reason it does not scroll in your sample code is because the top and the bottom of the draggable container are not on top of the view port which can trigger the scroll event.

<!DOCTYPE html>
<html>
    <head>
        <title>Hello World</title>
        <style>
            html, body {
                width: 100%;
                height: 100%;
                margin: 0;
                padding: 0;
                background-color: green;
            }
            
            .sticky {
              padding: 0 16px;
              background: #555;
              color: #f1f1f1;
              position: fixed;
              top: 0;
              width: 100%;
              height: 80px;
              
            }
            .parent-container {
                width: inherit;
                height: inherit;
                margin: 0;
                padding: 0;
                background-color: pink;
            }
            h1 {
                margin: 0;
                padding: 0;
            }
            .container-con{
            position: static;  
              overflow-y: scroll;
            height: 100%;
          }
          .container{
            position: relative; 
            top:80px;
            overflow-y: scroll;
            height: calc(100% - 82px);
            border: 1px solid red;
          }
          .container > div {
            display: inline-block;
            vertical-align: top;
          }
          .left-nav{
            position: fixed; 
            width: 100px;
          }
          .center-nav{
            margin-left: 100px;
            width: 250px;
            height: calc(100% - 10px);
            overflow-y: scroll;
            border: 1px solid blue;
            
          }
          .right-nav{
            width: 100px;
            position: fixed; 
            right: 0;

          }
        </style>
    </head>
    <body>
        <div class="parent-container">
            <div class="sticky">
              <h1>My Header</h1>
            </div>
            <div class="container-con">
            <div class="container" >
                <div class="left-nav">
                  Left nav
                </div>
                <div class="center-nav">                            
                  <div draggable="true">draggable start <br><br></div>
                  <div draggable="true">draggable 2 <br><br></div>
                  <div draggable="true">draggable 3 <br><br></div>
                  <div draggable="true">draggable 4 <br><br></div>


                  <div draggable="true">draggable 1 <br><br></div>
                  <div draggable="true">draggable 2 <br><br></div>
                  <div draggable="true">draggable 3 <br><br></div>
                  <div draggable="true">draggable 4 <br><br></div>
                  <div draggable="true">draggable 1 <br><br></div>
                  <div draggable="true">draggable 2 <br><br></div>
                  <div draggable="true">draggable 3 <br><br></div>
                  <div draggable="true">draggable 4 <br><br></div>

                  <div draggable="true">draggable 1 <br><br></div>
                  <div draggable="true">draggable 2 <br><br></div>
                  <div draggable="true">draggable 3 <br><br></div>
                  <div draggable="true">draggable 4 <br><br></div>

                  <div draggable="true">draggable 1 <br><br></div>
                  <div draggable="true">draggable 2 <br><br></div>
                  <div draggable="true">draggable 3 <br><br></div>
                  <div draggable="true">draggable 4 <br><br></div>

                  <div draggable="true">draggable 1 <br><br></div>
                  <div draggable="true">draggable 2 <br><br></div>
                  <div draggable="true">draggable 3 <br><br></div>
                  <div draggable="true">draggable 4 <br><br></div>

                  <div draggable="true">draggable 1 <br><br></div>
                  <div draggable="true">draggable 2 <br><br></div>
                  <div draggable="true">draggable 3 <br><br></div>
                  <div draggable="true">draggable 4 <br><br></div>

                  <div draggable="true">draggable 1 <br><br></div>
                  <div draggable="true">draggable 2 <br><br></div>
                  <div draggable="true">draggable 3 <br><br></div>
                  <div draggable="true">draggable 4 <br><br></div>

                  <div draggable="true">draggable 1 <br><br></div>
                  <div draggable="true">draggable 2 <br><br></div>
                  <div draggable="true">draggable 3 <br><br></div>
                  <div draggable="true">draggable end <br><br></div>
                </div>
                 <div class="right-nav">
                  right nav
                </div>
            </div>
          </div>
        </div>
        
        
    </body>
</html>

Related