How to prevent (bootstrap) fixed top navigation from zooming on mobile

后端 未结 2 1104
花落未央
花落未央 2021-02-09 09:01

When using the fixed top navigation of bootstrap 3, I noticed that when users use the native zoom on mobile, the top navigation also becomes very large. This leads to a pretty p

相关标签:
2条回答
  • 2021-02-09 09:13

    The problem that you're seeing is because of the interaction between the header on the content div. The header is overlapping the content panel so when you zoom the header gets bigger and overlaps more content. If you separate the nav from the content it will probably solve your problem. The baseline bootstrap nav should be setup that way where the nave is above the main content container.

    Remove the fixed position from the nav. Delete this:

    position:fixed;
    

    Remove the margin on the bottom of the nav:

    margin-bottom: 27px;
    

    Now your nav and content don't overlap and things zoom in a cleaner fashion. You also may want to add the img-responsive class to your logo which will help it scale correctly so it doesn't overlap the menu button on mobile.

    0 讨论(0)
  • 2021-02-09 09:17

    Keeping Bootstrap's navbar-top-fixed from zooming on mobiles

    The final result: using css transform to keep fixed top header at constant size after zoom

    On mobile devices only, is possible to calculate the zoom factor as the ratio of window.innerWidth and the screen width, as shown in [2]. When the user toggles zoom, the resize event is triggered; moreover, when a page is reloaded that was previously zoomed, the zoom factor is maintained. Thus, one use jQuery to dynamically update CSS transforms that keep the header in shape. Note that, with position: fixed, a transformation of the origin is also required. Applying class device-fixed-height to the bootstrap nav and device-fixed-width to brand logo and hamburger icon then produce very close to the desired result.

    • Bug: As the page is zoomed, the hamburger icons still moves a bit to the left. Any suggestions how to fix it are appreciated!
    • Tested on: Android 4.4

    Live example: http://www.exploretrade.info/other/example-fixed-nav-after-zoom.html

    Source code:

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">
        <link rel="icon" href="../../favicon.ico">
    
        <title>Bootstrap zoom-proof fixed top nav</title>
    
        <!-- Bootstrap core CSS -->
        <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
    
        <!-- Custom styles for this template -->
        <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
        <style>
        body {
         padding-top: 50px;
        }
        </style>
      </head>
      <body>
        <nav class="navbar navbar-inverse navbar-fixed-top device-fixed-height">
          <div class="container">
            <div class="navbar-header">
              <button type="button" class="navbar-toggle collapsed device-fixed-width" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                 <i class="fa fa-bars"></i>
              </button>
              <a class="navbar-brand device-fixed-width" href="#">Example</a>
            </div>
            <div id="navbar" class="collapse navbar-collapse device-fixed-width">
              <ul class="nav navbar-nav">
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#about">About</a></li>
                <li><a href="#contact">Contact</a></li>
              </ul>
            </div><!--/.nav-collapse -->
          </div>
        </nav>
        <div class="container">
          <div class="starter-template">
            <h1>Bootstrap starter template</h1>
            <p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
    
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In in metus eget nisi imperdiet dictum ac eu metus. Morbi consequat sodales porta. Nam convallis sed dolor in ullamcorper. Vestibulum ut tortor porttitor, venenatis nulla iaculis, sollicitudin metus. Mauris ut hendrerit purus, sed ultricies lacus. Proin imperdiet, lectus vel efficitur hendrerit, quam tortor efficitur sapien, vehicula viverra magna ipsum vitae lacus. Sed faucibus elit vel massa placerat, in porttitor est suscipit. Pellentesque consequat condimentum elit, at sagittis erat euismod nec. Fusce consequat purus quis turpis volutpat, vel luctus tortor consectetur. Sed in lectus vitae enim fringilla faucibus. Mauris vitae risus ut ex convallis luctus. Interdum et malesuada fames ac ante ipsum primis in faucibus. Etiam tempor ante augue, sed iaculis nisi porta quis.</p>
          </div>
    
        </div><!-- /.container -->
    
    
        <!-- Bootstrap core JavaScript
        ================================================== -->
        <!-- Placed at the end of the document so the pages load faster -->
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
        <script>
    
    // from https://signalvnoise.com/posts/2407-device-scale-user-interface-elements-in-ios-mobile-safari
    function getDeviceScale() {
        var deviceWidth, landscape = Math.abs(window.orientation) == 90;
        if (landscape) {
          // iPhone OS < 3.2 reports a screen height of 396px
          deviceWidth = Math.max(480, screen.height);
        } else {
          deviceWidth = screen.width;
        }
        return window.innerWidth / deviceWidth;
    }
    
    // mobile only - keep the position:fixed header at constant size when page is zoomed
    if (navigator.userAgent.match(/Mobi/)) {
        $(window).on('load scroll', function() {
            var ds = getDeviceScale();
            $('.device-fixed-height').css('transform','scale(1,' + ds + ')')
                .css('transform-origin', '0 0');
            $('.device-fixed-width').css('transform', 'scale(' + ds + ',1)')
                .css('transform-origin', '0 0');
            })
    }
    
      </script>
        </script>
      </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题