我正在处理一个导航栏,它使用交叉点观察器来缩小,它相应地添加了一个类,但当转换开始时,它从0填充开始.

在这里,当添加.NAV滚动时,css属性也会添加到导航栏中.但就在它相加的时候,填充开始从0过渡.我能做什么?

const nav = document.querySelector("nav");
const sectionOne = document.querySelector(".intersection");

const sectionOneObserver = new IntersectionObserver(function(entries, sectionOneObserver) {
  entries.forEach(entry => {
    if (!entry.isIntersecting) {
      nav.classList.add("nav-scrolled")
    } else {
      nav.classList.remove("nav-scrolled")
    }
  });
});

sectionOneObserver.observe(sectionOne)
.nav-container {
  display: flex;
  justify-content: center;
}

nav {
  position: fixed;
  width: 100vw;
  padding-block: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: 1s ease;
}

nav>* {
  margin-inline: 5%;
}

nav>div {
  width: 300px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  overflow: hidden;
  padding-block: 10px;
}

.nav-scrolled {
  width: 80%;
  padding-block: 15px;
  font-size: 20%;
  border-radius: 20px;
  transform: translateY(15px);
}

.nav-scrolled>* {
  margin-inline: 15px;
}
.intersection{height:10vh}

.body{color:red; height:120vh}
<div class="nav-container">
  <nav>
    <a href="#">
      <h1>Heading</h1>
      <h2>Heading-2</h2>
    </a>
    
    <div>
      <a href="#">link1</a>
      <a href="#">link2</a>
      <a href="#">link3</a>
    </div>
  </nav>
</div>

<div class="intersection"></div>
<div class="body"><div>

推荐答案

这并不是说填充从0开始.之所以会发生这种情况,是因为在NAV元素的子元素上有margin-inline.当你滚动时,这个边距消失了,子元素们没有过渡标签,这就是为什么它看起来是 skip 的原因.我刚刚删除了这条css-line.似乎工作得很好:)

const nav = document.querySelector("nav");
const sectionOne = document.querySelector(".intersection");

const sectionOneObserver = new IntersectionObserver(function(entries, sectionOneObserver) {
  entries.forEach(entry => {
    if (!entry.isIntersecting) {
      nav.classList.add("nav-scrolled")
    } else {
      nav.classList.remove("nav-scrolled")
    }
  });
});

sectionOneObserver.observe(sectionOne)
.nav-container {
  display: flex;
  justify-content: center;
}

nav {
  position: fixed;
  width: 100vw;
  padding-block: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: 1s ease;
}

nav>* {
  margin-inline: 5%;
}

nav>div {
  width: 300px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  overflow: hidden;
  padding-block: 10px;
}

.nav-scrolled {
  width: 80%;
  padding-block: 15px;
  font-size: 20%;
  border-radius: 20px;
  transform: translateY(15px);
}

.intersection{height:10vh}

.body{color:red; height:120vh}
<div class="nav-container">
  <nav>
    <a href="#">
      <h1>Heading</h1>
      <h2>Heading-2</h2>
    </a>
    
    <div>
      <a href="#">link1</a>
      <a href="#">link2</a>
      <a href="#">link3</a>
    </div>
  </nav>
</div>

<div class="intersection"></div>
<div class="body"><div>

Html相关问答推荐

R中的动态Webscraping

自动字间距以适应行CSS

放大缩小标题在外面的图像

输入宽度在表格显示上不起作用FLEX溢出隐藏Firefox

如何使用HTML和css代码在vb.net上打印POS

在单独的容器之间堆叠上下文

如何将CSS滤镜仅应用于背景图像

渐变进度条位置

视频重新加载而不是在 Swift 中暂停 WKWebView

MUI 日期 Select 器要包含的日期

:after 伪元素没有出现,即使它有 content 属性

水平填充容器内的空间

这两个CSS中的网格居中对齐内部盒子有什么区别?

如何将图像移动到父容器的右侧?

当我希望它只横向滚动时,可滚动菜单也会上下移动

无法使用 HTML 中的 Bootstrap 自上而下居中

HTML 标记未在页面上展开

使用 htmloutput 在shiny 的应用程序中呈现文本

禁用的 Select 标签没有在 chrome 上的 css 中指定的 colored颜色

根据正则表达式 attr 从 read_html 过滤表格