1

Is there a way/jQuery method to measure view-port height separately?

One vh when Address-bar is showing & another vh when Address-bar is hidden?

var vhAdBarPresent = $(window).height();
var vhAdBarHidden = $(window).height();

I googled this issue and stumbled various articles where they manually compensate for address-bar by add or subtracting 56px from regular 100vh. Is this the only option we got? 56px only works for chrome other browsers might have different address-bar heights...

All I want to do is make a draggable bottom nav which stays at bottom by default and covers the whole viewport once dragged up using a handle. But footer just refuses to stay at the bottom.

You can see the problem I am facing on my Github Demo. Open in android chrome (It works as intended on desktop browsers but in mobile browsers which hide the address-bar on scroll, it breaks.)

In this demo I am trying to align the footer at the bottom of the screen. But when the footer is dragged/ the page is scrolled it is not positioned at the bottom.

Here's the code for you reference...

$(function() {
  var dragFlag = 0;

  var navPos = Math.round(0.93 * $(window).height());
  var dragUL = Math.round(0.2 * navPos);
  var dragLL = Math.round(0.8 * navPos);

  console.log(dragFlag);

  var $draggable = $('.draggable').draggabilly({
    axis: 'y',
    handle: '.handle'
  });

  var draggie = $('.draggable').data('draggabilly');

  $draggable.on('dragEnd', function(event, pointer) {
    var dragPosY = Math.round(draggie.position.y);

    console.log("DPY: " + dragPosY + "; NP: " + navPos + "; dUL: " + dragUL + "; dLL: " + dragLL);

    if (dragFlag === 0) {
      if (dragPosY > dragLL) {
        $draggable.draggabilly('setPosition', 0, navPos);
        dragFlag = 0;
        console.log(dragPosY + "DPY > DLL" + dragLL + "; DF:" + dragFlag);
      }
      if (dragPosY < dragLL) {
        $draggable.draggabilly('setPosition', 0, 0);
        dragFlag = 1;
        console.log(dragPosY + "DPY < DLL" + dragLL + "; DF:" + dragFlag);
      }
    } else if (dragFlag === 1) {
      if (dragPosY > dragUL) {
        $draggable.draggabilly('setPosition', 0, navPos);
        dragFlag = 0;
        console.log(dragPosY + "DPY > DLL" + dragLL + "; DF:" + dragFlag);
      }
      if (dragPosY < dragUL) {
        $draggable.draggabilly('setPosition', 0, 0);
        dragFlag = 1;
        console.log(dragPosY + "DPY < DLL" + dragLL + "; DF:" + dragFlag);
      }
    }

  });
});
* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0
}

#content {
  background: #222;
  color: #ddd;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  height: 300vh;
  width: 100%;
  padding-bottom: 60px;
}

#footer {
  background: #f00;
  color: #ddd;
  height: 130vh;
  display: flex;
  width: 100%;
  flex-direction: column;
  position: fixed;
  left: 0;
  bottom: calc(-130vh + 7vh);
}

.handle {
  background: #0ff;
  color: #222;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  height: 7vh;
  width: 100%;
  box-shadow: inset #cf0 0 3px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>BotNav</title>

    <script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>
    <script src="https://unpkg.com/draggabilly@2/dist/draggabilly.pkgd.min.js"></script>


    <style>
        *{box-sizing: border-box;}
        html,body{margin: 0; padding:0}
        #content{
            background:#222; color:#ddd;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: space-between;
            height: 300vh; width: 100%;
            padding-bottom: 60px;
        }
        #footer{
            background: #f00;color: #ddd;
            height: 130vh;
            display: flex; width: 100%; flex-direction: column;
            position: fixed;
            left: 0;
            bottom: calc(-130vh + 7vh);

        }
        .handle{
            background: #0ff;
            color:#222;
            display: flex; flex-direction: row;
            align-items: center; justify-content: space-between;
            height: 7vh;
            width: 100%;
            box-shadow: inset #cf0 0 3px;
        }
    </style>
    <script>
        $(function(){
            var dragFlag = 0;

            var navPos = Math.round(0.93*$(window).height());
            var dragUL = Math.round( 0.2*navPos );
            var dragLL = Math.round( 0.8*navPos );

            console.log(dragFlag);

            var $draggable = $('.draggable').draggabilly({
                axis: 'y', handle:'.handle'
            });

            var draggie = $('.draggable').data('draggabilly');

            $draggable.on( 'dragEnd', function( event, pointer ) {
                var dragPosY = Math.round(draggie.position.y);

                console.log("DPY: "+dragPosY+"; NP: "+navPos+"; dUL: "+dragUL+"; dLL: "+dragLL);
                
                if(dragFlag === 0)
                {
                    if(dragPosY>dragLL){
                        $draggable.draggabilly('setPosition',0,navPos);
                        dragFlag = 0;
                        console.log(dragPosY+"DPY > DLL"+dragLL+"; DF:"+dragFlag);
                    }
                    if(dragPosY<dragLL){
                        $draggable.draggabilly('setPosition',0,0);
                        dragFlag = 1;
                        console.log(dragPosY+"DPY < DLL"+dragLL+"; DF:"+dragFlag);
                    }
                }
                else if(dragFlag === 1)
                {
                    if(dragPosY>dragUL){
                        $draggable.draggabilly('setPosition',0,navPos);
                        dragFlag = 0;
                        console.log(dragPosY+"DPY > DLL"+dragLL+"; DF:"+dragFlag);
                    }
                    if(dragPosY<dragUL){
                        $draggable.draggabilly('setPosition',0,0);
                        dragFlag = 1;
                        console.log(dragPosY+"DPY < DLL"+dragLL+"; DF:"+dragFlag);
                    }
                }

            });
        });
    </script>

</head>
<body>
    <footer id="footer" class="draggable">
        <div class="handle">
            <span>Handle1</span>
            <span>Handle2</span>
            <span>Handle3</span>
        </div>
        <div class="footContent">Foot Content</div>
    </footer>
    <div id="content">
        <div>Content Top</div>
        <div>Content Bottom</div>
    </div>
</body>
</html>

If we could somehow extract current viewport heights life would be much easier for us... :(

YourPalNurav
  • 1,204
  • 9
  • 40

1 Answers1

1

You can take advantage of the innerHeight variable in JavaScript. This updates the browsers interface whenever a visibility change is made.

let vh = window.innerHeight * 0.01;

document.documentElement.style.setProperty('--actualVh', `${vh}px`);
section {
  height: calc(var(--actualVh, 1vh) * 100);
}

.item {
  align-items: center;
  display: flex;
  height: calc(100% / 5);
  justify-content: center;
  color:white;
}

.item:nth-child(odd) {
  background-color: pink;
}

.item:nth-child(even) {
  background-color: lightblue;
}
<section>
  <div class="item">One</div>
  <div class="item">Two</div>
  <div class="item">Three</div>
  <div class="item">Four</div>
  <div class="item">Five</div>
</section>

Source

MattHamer5
  • 1,431
  • 11
  • 21