jQuery可以检测用户何时调整浏览器大小?

时间:2017-03-30 21:59:53

标签: javascript jquery twitter-bootstrap

我用jQuery编写了简单的代码,当用户滚动页面时隐藏了导航栏。我工作得很好,但现在我想再添加一件东西。

当用户视口宽度超过768px时,我希望它能像现在一样工作。 但是当用户视口小于768px时,我希望导航栏始终可见。

代码应该足够智能,它可以检测到浏览器已被用户调整大小并且它仍然可以正常工作。

总结一下:

  1. 用户使用chrome视口1024px启动我的页面 - 当他向下滚动页面时,导航栏隐藏,当他回到页面顶部时,导航栏重新出现。

  2. 然后用户调整chrome的大小并具有视口700px - 当他向下滚动页面时,导航栏应始终打开。

  3. 任何聪明的想法如何实现它?

    $(document).ready(function() {  
      $(".navbar").show();
      $(function() {
        $(window).scroll(function() {     
          if ($(this).scrollTop() > 300) {
            $(".navbar").fadeOut();
          } else {
            $(".navbar").fadeIn();
          }     
        });
      });
    });
    

2 个答案:

答案 0 :(得分:0)

编辑:实际上,你会希望下面的,前面的答案(window.width)只在你告诉它时会检查,只要调整大小。

var windowsize = $(window).width();  // if you need var outside of function for something

$(window).resize(function() {
    windowsize = $(window).width();
    if (windowsize > 700) {
    dowhatever();
  });
 } 
});

答案 1 :(得分:0)

可以使用$(window).resize来解决。这是完整的演示如何完成。希望这能回答你的问题。



$(document).ready(function() {
  $(function() {
    var winWidth = $(window).width();
    $(window).resize(function() {
      winWidth = $(window).width();
    });
    $(window).scroll(function() {
      if (winWidth <= 700) {
        $(".navbar").css({
          position: "fixed",
          width: "100%"
        });
        $("p").css("margin-top","60px");
      } else {
        if ($(this).scrollTop() > 300) {
          $(".navbar").fadeOut();
        } else {
          $(".navbar").fadeIn();
        }
      }
    });
  });
});
&#13;
<!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">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>

<body>
  <nav class="navbar navbar-default">
    <div class="container-fluid">
      <!-- Brand and toggle get grouped for better mobile display -->
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
        <a class="navbar-brand" href="#">Brand</a>
      </div>

      <!-- Collect the nav links, forms, and other content for toggling -->
      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav">
          <li class="active"><a href="#">Home <span class="sr-only">(current)</span></a></li>
          <li><a href="#">About Us</a></li>
          <li><a href="#">Contact</a></li>
        </ul>
      </div>
      <!-- /.navbar-collapse -->
    </div>
    <!-- /.container-fluid -->
  </nav>
  <div class="container-fluid">
    <p>
    The standard Lorem Ipsum passage, used since the 1500s "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
    ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi
    architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia
    dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid
    ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?" 1914 translation by H. Rackham "But I must explain to
    you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one
    rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires
    to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain
    some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
  </p>
  </div>
  <script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>

</html>
&#13;
&#13;
&#13;