【问题标题】:Animate nav to move with another nav动画导航与另一个导航一起移动
【发布时间】:2020-11-09 22:03:45
【问题描述】:

我有一个 PHP 脚本,用于定义网站的菜单和导航栏。我有一个顶部菜单,当您向下滚动页面时,它会固定在页面顶部。我想在左侧“粘”到顶部菜单的第二个菜单。我这样做的方式是,当顶部菜单到达页面顶部时效果很好,它会像我想要的那样被左侧菜单固定。问题是如果顶部菜单没有到达页面顶部(就像它只是一点点滚动),左侧菜单不会粘在顶部菜单上。

顶部菜单是具有“Inicio”、“Instrucciones”等的菜单。左侧菜单是具有“Nueva cata”、“Nueva cerveza”等的菜单。

这是开始的页面(0 滚动)。

这是顶部菜单到达页面顶部后的页面(>200 滚动)。

这是顶部菜单到达页面顶部之前的页面(0

正如您在第三页中看到的那样,左侧菜单随着滚动而下降,而不是随着顶部菜单上升。

这是 HTML:

<nav class="navbar navbar-expand-lg navbar-light bg-light" id="menu_arriba">
    <div class="navbar-brand" style="width: 200px;"></div>
    <a href="user.php" class="navbar-brand">
        <p>Inicio</p>
    </a>
    <a href="instrucciones.php" class="navbar-brand">
        <p>Instrucciones</p>
    </a>
    <a href="contacto.php" class="navbar-brand">
        <p>Contacto</p>
    </a>
    <a href="faq.php" class="navbar-brand">
        <p>FaQ</p>
    </a>
    <a href="ajax/logout.php" class="navbar-brand">
        <p><i class="fas fa-sign-out-alt"></i> Salir</p>
    </a>
</nav>

<nav id="menu_izq" class="sidenav">
    <div></div>
    <a href="nueva_cata.php">
        <p>Nueva Cata</p>
    </a>
    <a href="nueva_cerveza.php">
        <p>Nueva Cerveza</p>
    </a>
    <a href="cata.php">
        <p>Mis catas</p>
    </a>
    <a href="mis_cervezas.php">
        <p>Mis cervezas</p>
    </a>
    <a href="mis_amigos.php">
        <p>Mis amigos</p>
    </a>
</nav>

这是 CSS:

#menu_izq {
  height: 100%;
  width: 200px;
  position: fixed;
  left: 0;
  top: 252px;
  z-index: 3;
  background-color: #503522;
  overflow-x: hidden;
  padding-top: 20px;
}
#menu_arriba{
    background-color: #503522;
    width: 100%;
    z-index: 1;
}

这是我的 JQuery:

$(window).scroll(function(){
    var elementPosition = $('#menu_arriba').offset();

    if($(window).scrollTop() >= elementPosition.top){
        $('#menu_arriba').css('position','fixed').css('top','0');
        $("#menu_izq").css('position','fixed').css('top', '35');
    } else {
        $('#menu_arriba').css('position','initial');
    }

    if(elementPosition.top <= 200){
        $('#menu_arriba').css('position','initial').css('top', '200');
        $("#menu_izq").css('top', '250');
    }
});

我知道问题出在 JQuery 方法中,但我没有写出我想要的东西。非常感谢。

----- 编辑

我做我的网站的方式是使用 menus.php,我在其中编写这样的顶部和左侧菜单

<?php function izquierda() { ?>

<nav id="menu_izq" class="sidenav">
    <div></div>
    <a href="nueva_cata.php"><p>Nueva Cata</p></a>
    <a href="nueva_cerveza.php"><p>Nueva Cerveza</p></a>
    <a href="cata.php"><p>Mis catas</p></a>
    <a href="mis_cervezas.php"><p>Mis cervezas</p></a>
    <a href="mis_amigos.php"><p>Mis amigos</p></a>
</nav>

<?php } ?>
<?php function arriba() { ?>

<nav class="navbar navbar-expand-lg navbar-light bg-light" 
id="menu_arriba">
    <div class="navbar-brand" style="width: 200px;"></div>
    <a href="user.php" class="navbar-brand"><p>Inicio</p></a>
    <a href="instrucciones.php" class="navbar-brand"><p>Instrucciones</p>            
</a>
<a href="contacto.php" class="navbar-brand"><p>Contacto</p></a>
<a href="faq.php" class="navbar-brand"><p>FaQ</p></a>
<div class="navbar-brand" style="width: 450px;"></div>
<a href="ajax/logout.php" class="navbar-brand"><p><i class="fas fa-sign- 
out-alt"></i> Salir</p></a>
</nav>

所以在网站的实际页面中,我只写:

<?php echo banner(); ?>
<?php echo arriba(); ?>
<?php echo izquierda(); ?>
<div class="main">
// HERE GOES THE CONTENT OF THE PAGE
</div>

我编辑这个是为了让你知道menu_izq的原因是position: fixed是因为这样,它在左侧菜单的右侧显示了页面的内容,而在position: sticky||relative中,内容是显示在菜单的末尾。我需要在不改变(我认为)menu_izq(左侧菜单)的position: fixed 的情况下找到解决方案。

【问题讨论】:

  • 如果您要上传 HTML 网页的代码 sn-p,我将能够为您提供更好的帮助。对于这样的意图,我怀疑是否需要 JQuery 甚至 JS。谢谢!

标签: javascript html jquery css-position


【解决方案1】:

看起来您正在使用 Bootstrap。目前,您的左侧导航最初设置为position: fixed,我建议您最初使用position: relative 来定位您的左侧导航,以便定位您的nav 元素可以相对于背景图像的高度。使用 Bootstrap,此解决方案将左侧导航和内容包装在 flex 容器中,以便可以轻松地相对于该容器定位内容,因为稍后导航将获得position: fixed

基本上在脚本上,只需检测滚动条顶部的 Y 位置是否已经超过背景图像元素的height。如果是,则将fixed 位置分配给导航元素,并根据需要相对于包裹左侧导航和内容的容器调整内容的位置。检查涉及.navs-are-fixed 的CSS 属性,了解导航如何分配fixed 位置。

$(window).scroll(function() {

  // innerHeight is used to include any padding
  var bgImgHeight = $('.some-bg-img').innerHeight();

  // if the the scroll reaches the end of the background image, this is when you start to assign 'fixed' to your nav elements
  if ($(window).scrollTop() >= bgImgHeight) {
    $('body').addClass("navs-are-fixed");
  } else {
    $('body').removeClass("navs-are-fixed");
  }
});
#menu_izq {
  height: 100%;
  width: 200px;
  position: relative;
  left: 0;
  top: 0;
  z-index: 3;
  background-color: #503522;
  overflow-x: hidden;
  padding-top: 20px;
}

#menu_arriba {
  background-color: #503522;
  width: 100%;
  z-index: 1;
  position: relative;
  top: 0;
}

body {
  background-color: #ffc75a !important;
  height: 300vh; /* sample arbitrary value to force body scrolling */
}

.some-bg-img {
  background: url(https://via.placeholder.com/1920x200.png?text=Sample%20Background%20Image);
  height: 200px;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: bottom;
}

.navs-are-fixed #menu_arriba {
  position: fixed;
}

.navs-are-fixed #menu_izq {
  position: fixed;
  top: 72px; /* the height of your top nav */
}

.navs-are-fixed .some-sample-content {
  position: absolute;
  top: 72px; /* the height of your top nav */
  left: 200px; /* the width of your left nav */
}
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

<body>
  <div class="some-bg-img"></div>
  <nav class="navbar navbar-expand-lg navbar-light bg-light" id="menu_arriba">
    <div class="navbar-brand"></div>
    <a href="user.php" class="navbar-brand">
      <p>Inicio</p>
    </a>
    <a href="instrucciones.php" class="navbar-brand">
      <p>Instrucciones</p>
    </a>
    <a href="contacto.php" class="navbar-brand">
      <p>Contacto</p>
    </a>
    <a href="faq.php" class="navbar-brand">
      <p>FaQ</p>
    </a>
    <a href="ajax/logout.php" class="navbar-brand">
      <p><i class="fas fa-sign-out-alt"></i> Salir</p>
    </a>
  </nav>

  <div class="d-flex h-100 w-100 position-absolute">
    <nav id="menu_izq" class="sidenav">
      <div></div>
      <a href="nueva_cata.php">
        <p>Nueva Cata</p>
      </a>
      <a href="nueva_cerveza.php">
        <p>Nueva Cerveza</p>
      </a>
      <a href="cata.php">
        <p>Mis catas</p>
      </a>
      <a href="mis_cervezas.php">
        <p>Mis cervezas</p>
      </a>
      <a href="mis_amigos.php">
        <p>Mis amigos</p>
      </a>
    </nav>

    <div class="some-sample-content">
      <p>Lorem Ipsum</p>
      <p>Lorem Ipsum</p>
      <p>Lorem Ipsum</p>
      <p>Lorem Ipsum</p>
      <p>Lorem Ipsum</p>
      <p>Lorem Ipsum</p>
      <p>Lorem Ipsum</p>
      <p>Lorem Ipsum</p>
      <p>Lorem Ipsum</p>
      <p>Lorem Ipsum</p>
      <p>Lorem Ipsum</p>
      <p>Lorem Ipsum</p>
      <p>Lorem Ipsum</p>
    </div>
  </div>
</body>

如果您确实必须将fixed 定位在左侧导航中,那么您将不得不根据横幅和顶部导航的height 计算它的top 值,这样如果滚动条 Y 位置的 top 超过横幅的 height,左侧导航的 top 值将等于顶部导航的高度 - 所以你将它向下推,这样它们就不会重叠。如果滚动条 Y 位置的top 不超过横幅的高度,则左侧导航的顶部值将等于横幅的height 与顶部导航减去顶部的差值滚动条的 Y 位置。

$(window).scroll(function() {

  // innerHeight is used to include any padding
  var bgImgHeight = $('.some-bg-img').innerHeight();
  var topNavHeight = $('#menu_arriba').innerHeight();
  var leftNavInitialCssTop = bgImgHeight + topNavHeight;

  // if the the scroll reaches the end of the background image, this is when you start to assign 'fixed' to the top nav
  if ($(window).scrollTop() >= bgImgHeight) {
    $('body').addClass("navs-are-fixed");
    $("#menu_izq").css("top", topNavHeight);
  } else {
    $('body').removeClass("navs-are-fixed");
    $("#menu_izq").css("top", leftNavInitialCssTop - $(window).scrollTop())
  }

});
#menu_izq {
  height: 100%;
  width: 200px;
  position: fixed;
  left: 0;
  top: 252px;
  z-index: 3;
  background-color: #503522;
  overflow-x: hidden;
  padding-top: 20px;
}

#menu_arriba {
  background-color: #503522;
  width: 100%;
  z-index: 1;
  top: 0;
}

body {
  background-color: #ffc75a !important;
  height: 400vh; /* sample arbitrary value to force body scrolling */
}

.some-bg-img {
  background: url(https://via.placeholder.com/1920x200.png?text=Sample%20Background%20Image);
  height: 179px;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: bottom;
}

.navs-are-fixed #menu_arriba {
  position: fixed;
}
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

<div class="some-bg-img"></div>
<nav class="navbar navbar-expand-lg navbar-light bg-light" id="menu_arriba">
  <div class="navbar-brand"></div>
  <a href="user.php" class="navbar-brand">
    <p>Inicio</p>
  </a>
  <a href="instrucciones.php" class="navbar-brand">
    <p>Instrucciones</p>
  </a>
  <a href="contacto.php" class="navbar-brand">
    <p>Contacto</p>
  </a>
  <a href="faq.php" class="navbar-brand">
    <p>FaQ</p>
  </a>
  <a href="ajax/logout.php" class="navbar-brand">
    <p><i class="fas fa-sign-out-alt"></i> Salir</p>
  </a>
</nav>

<nav id="menu_izq" class="sidenav">
  <div></div>
  <a href="nueva_cata.php">
    <p>Nueva Cata</p>
  </a>
  <a href="nueva_cerveza.php">
    <p>Nueva Cerveza</p>
  </a>
  <a href="cata.php">
    <p>Mis catas</p>
  </a>
  <a href="mis_cervezas.php">
    <p>Mis cervezas</p>
  </a>
  <a href="mis_amigos.php">
    <p>Mis amigos</p>
  </a>
</nav>

【讨论】:

  • 我编辑了我的帖子。您的解决方案是正确的,但我制作网站的方式不允许我更改 menu_izqposition: fixed
  • 我添加了第二个解决方案。如果您确实必须保持左侧导航的 fixed 定位,则必须根据顶部导航、横幅和 window.scrollY 属性的高度计算其定位
【解决方案2】:

您应该能够只使用 CSS 而无需使用 position: sticky 属性的 JavaScript。

将两个元素设为position: sticky,顶部导航应具有top: 0 属性,侧导航应具有top: x 属性,其中x 是顶部导航的高度。

应该够了,你应该可以删除js代码了。

在此处了解更多关于sticky 职位https://developer.mozilla.org/en-US/docs/Web/CSS/position

【讨论】:

  • 问题是左边的菜单是position: fixed,因为在这个脚本中我只是写了要在所有不同视图中回显的菜单。所以当我把左边的菜单放到position: sticky 时,页面的所有内容都到了菜单的底部,而不是我想要的右边。
【解决方案3】:

为什么要这样做?您可以将顶部菜单和侧边菜单以及文本容器一起移动到一个容器中以相互关联,以百分比设置文本框容器固定高度并将其overflow-y 设置为auto。使整个容器像顶部菜单一样具有动画效果。这将解决您的问题。

【讨论】:

    【解决方案4】:

    侧面和顶部滑动&lt;nav&gt; 元素无法与页面内容正确交互。尤其是横向&lt;nav&gt;,根据许多变化的规则进行垂直滑动——坐下、跟随、滑动、分开。一次太多了。

    解决方法 1 - 平滑 - 顶部滑动,侧面固定 (已修正锚点)

    恐怕这不是您梦想的解决方案。由于避免了挤压点,因此工作顺畅且轻松。

    $(window).scroll(function(){
    var elementPosition = $('#menu_arriba').offset();
          
    if($(window).scrollTop() >= elementPosition.top){
    $('#menu_arriba').css('position','fixed').css('top','0');
    } else {
    $('#menu_arriba').css('position','relative');    /* fixed */
    }
    if(elementPosition.top <= 200){
    $('#menu_arriba').css('position','initial').css('top', '200');
    }});
    

    *{ box-sizing: border-box}
    body{
    margin: 0;  
    text-align: center;
    z-index: -1;
    }
    #pilar{     /* added fix */
    background: #503522;
    width: 200px;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: -1
    }
    #menu_izq {
    background: 0;  /* fixed */
    width: 200px;    /* padding-top: 200px; removed */
    position: fixed;
    top: 200px; /* fixed */
    left: 0;
    bottom: 0;
    z-index: 0; /* fixed */
    }
    #menu_arriba{
    background: #503522;
    width: 100%;
    position: relative; /* added fix */
    z-index: 1; /* added fix */
    }
    p{ margin: 100px 100px 100px 250px; z-index: -1 }
    a{ display: inline-block; width: 150px; border: 1px solid white; text- align: center; padding: 10px 0; margin: 20px}
    img{ border-bottom: 10px solid #503522; margin: 0 0 -3px; width: 100%; height: 200px; z-index: 1; z-index: 9;}  
    

    <img src="img/blue.jpg" alt="blue">
    <div id="pilar"></div>  <!-- added fix -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light" id="menu_arriba">
    <div class="navbar-brand" ></div>   <!-- needed or redundancy? -->
    <a href="user.php" class="navbar-brand">Inicio</a>
    <a href="instrucciones.php" class="navbar-brand">Instrucciones</a>
    <a href="contacto.php" class="navbar-brand">Contacto</a>
    <a href="faq.php" class="navbar-brand">FaQ</a>
    <a href="ajax/logout.php" class="navbar-brand"><i class="fas fa-sign-out-alt"></i>Salir</a>
    </nav>
    <nav class="sidenav" id="menu_izq">
    <a href="nueva_cata.php">Nueva Cata</a>
    <a href="nueva_cerveza.php">Nueva Cerveza</a>
    <a href="cata.php">Mis catas</a>
    <a href="mis_cervezas.php">Mis cervezas</a>
    <a href="mis_amigos.php">Mis amigos</a>
    </nav><p>... </p><p> ...</p>
    

    解决方法 1.1 - 流畅且视野开阔
    只有改变

    #menu_arriba{
    background: 0;
    width: 100%;
    position: relative; /* added fix */
    z-index: 0  /* added fix */
    }
    #hole{
    background: #503522;
    height: 100%;
    margin-left: 200px;
    width: auto;
    }
    
    <div id="pilar"></div>  <!-- added fix -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light" id="menu_arriba">
    <div id="hole">
        <div class="navbar-brand" ></div>
        <a href="user.php" class="navbar-brand">Inicio</a>
        <a href="instrucciones.php" class="navbar-brand">Instrucciones</a>
        <a href="contacto.php" class="navbar-brand">Contacto</a>
        <a href="faq.php" class="navbar-brand">FaQ</a>
        <a href="ajax/logout.php" class="navbar-brand"><i class="fas fa-sign-out-alt"></i> Salir</a>
    </div></nav>
    

    解决方法 2 - 脏 - 隐藏或掩盖副作用
    如果你仍然坚持 - 不是那么顺利,稍微有弹性的菜单。你的梦想也没有实现,但没有可见的漏洞。给你:

    $(window).scroll(function(){
    var elementPosition = $('#menu_arriba').offset();
    
    if($(window).scrollTop() >= elementPosition.top){
    $('#menu_arriba').css('position','fixed').css('top','0');
    $('#menu_izq').css('position','fixed').css('top','0');
    } else {
    $('#menu_arriba').css('position','initial');
    $('#menu_izq').css('position','initial');
    }
    if(elementPosition.top <= 200){
    $('#menu_arriba').css('position','initial').css('top', '200');
    $('#menu_izq').css('position','initial').css('top', '200');
    }});
    

    *{ box-sizing: border-box}
    body{
    margin: 0;  
    text-align: center;
    }
    #menu_izq {
    background: 0;
    height: auto;
    width: 200px;
    padding-top: 100px;
    }
    #menu_arriba{
    background: #503522;
    width: 100%;
    z-index: 1
    }
    #pilar{
    background: #503522;
    width: 200px;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: -1
    }
    p { margin: 100px 300px; position: relative; bottom: 400px}
    a{ display: inline-block; width: 150px; border: 1px solid white; text-align: center; padding: 10px 0; margin: 20px}
    img{ border-bottom: 10px solid #503522; margin: 0 0 -3px; width: 100%; height: 200px;}  
    

    <img src="img/blue.jpg" alt="blue">
    <div id="pilar"></div>
    
    <nav class="navbar navbar-expand-lg navbar-light bg-light" id="menu_arriba">
    <div class="navbar-brand" ></div>   <!-- what for it is? needed? -->
    <a href="user.php" class="navbar-brand">Inicio</a>
    <a href="instrucciones.php" class="navbar-brand">Instrucciones</a>
    <a href="contacto.php" class="navbar-brand">Contacto</a>
    <a href="faq.php" class="navbar-brand">FaQ</a>
    <a href="ajax/logout.php" class="navbar-brand"><i class="fas fa-sign-out-alt"></i>Salir</a>
    </nav>
    <nav class="sidenav" id="menu_izq">
    <a href="nueva_cata.php">Nueva Cata</a>
    <a href="nueva_cerveza.php">Nueva Cerveza</a>
    <a href="cata.php">Mis catas</a>
    <a href="mis_cervezas.php">Mis cervezas</a>
    <a href="mis_amigos.php">Mis amigos</a>
    </nav><p>... </p><p> ...</p>
    

    您的代码中还有其他一些小缺点。
    &lt;a href=""&gt;&lt;p&gt; .... &lt;/p&gt;&lt;/a&gt; 可能不算犯罪,但也不是好事。 &lt;p&gt; 有自己的特点,不是加强筋或支柱——我用 css 固定的。

    上面有我使用的整个代码 - 作为提示和解释。根据您的需要和品味调整元素大小。希望你喜欢它
    干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-24
      • 2019-03-26
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多