jquery - 如何防止最大高度过渡将页面滚动到顶部?

标签 jquery html css scroll css-transitions

我有一个原始最大高度为 0 的 div,最初将其隐藏,通过点击 anchor ,它的最大高度为 800px 以显示它。基本的东西。但是,每当发生此转换时,它都会自动将页面滚动到顶部,而不是将页面保留在其所在的任何位置。

这是 html:

<main class="outer-container">
  <div class="wrapper">
    <div class="text-container">
      <div class="text">

        <a class="reveal-hide" href="#">Show Hidden Div</a>

        <div class="hide">

         <!-- hidden div -->

        </div>
     </div>   
    </div>
  </div>
</main>

和 CSS:

.outer-container {
  display: block;
  position: relative;
  -webkit-box-flex: 1;
  -ms-flex: 1 0 auto;
  flex: 1 0 auto;
  -webkit-flex: 1 0 auto;
}
.wrapper {
  margin-top: 100px;
  box-sizing: border-box;
  padding: 30px;
  position: relative;
}
.text-container {
  max-width: 600px;
  padding: 30px;
  margin: 0 auto;
}
.hide {
  max-height: 0;
  overflow-y: hidden;
  -webkit-transition: max-height 0.4s ease-in-out;
  -moz-transition: max-height 0.4s ease-in-out;
  -ms-transition: max-height 0.4s ease-in-out;
  -o-transition: max-height 0.4s ease-in-out;
  transition: max-height 0.4s ease-in-out;
}
.hide-is-showing {
  max-height: 1000px;
}

和 jQuery:

    $(document).ready(function() {

      $('.reveal-hide').on('click', function() {
        $(".hide").addClass("hide-is-showing");
      });

    });

那么为什么每次转换时它都会滚动到顶部?我该如何解决这个问题?

最佳答案

尝试改变:

 <a class="reveal-hide" href="#">Show Hidden Div</a>

至:

<a class="reveal-hide" href="javascript:void(0)">Show Hidden Div</a>

因为这样当您单击它时它不会重新加载页面。

关于jquery - 如何防止最大高度过渡将页面滚动到顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38362727/

相关文章:

jquery - 使用 jquery 调用 ajax 后如何等待图像加载?

javascript - 使用 Jquery 检查自动生成的跨度类标签

html - 寻找一个完美的下拉菜单 : stretching to full width, 等宽项,纯 CSS。请帮忙解决一个故障

Javascript 生成的 margin-top 弄乱了放置内容

javascript - 尝试根据 JQuery 属性包含更改 h1 标签

c# - 如何修改c#,ASP .NET中的请求头

javascript - 从简单的 html 页面调用实时 Web 服务...?

html - CSS float 顺序不符合预期。为什么?

html - 在 HTML <button> 标签中定位

css - 容器调整到窗口大小