我有一个固定位置的箭头,以指示用户折叠下面有更多内容.

Screenshot of a website

这就是CSS:

.arrow {
    position: fixed;
    right: 0;
    top: 100%;
    transform: translateY(-100%);
    margin: -10px 10px 0 0;
    z-index: 100;
    opacity: 0.4;
}
.arrow svg {
    width: 80px;
    height: 80px;
}

我希望箭头在到达页脚时隐藏,并在向上滚动时可见.

有什么技巧我可以用来表演的吗?我试着使用IntersectionWatch,但只找到了it doesn't work with position: fixed个.我在想也许可以改变它的css,但我不确定我如何才能达到同样的效果.

我试了position: sticky个.但我不确定你能不能做这种事.

我也在考虑使用Scroll Drive Animation,但我从来没有使用过它们,不知道他们的API是否允许这样的东西.

有没有一种方法可以单独使用CSS来实现这种行为,或者我需要使用JavaScript和滚动事件?我更喜欢使用CSS解决方案或任何不涉及在滚动事件上执行JavaScript的现代解决方案.

EDIT这里是问题的再现.

body {
    margin: 0;
}
.arrow {
    position: fixed;
    right: 0;
    top: 100%;
    transform: translateY(-100%);
    margin: -10px 10px 0 0;
    z-index: 100;
    opacity: 0.4;
}
.arrow svg {
    width: 80px;
    height: 80px;
}
.force-scrollbar {
   height: 1000px;
}
footer {
   background: black;
   height: 200px;
}
<div class="force-scrollbar">

</div>

<footer>
<div class="arrow">
<svg width="361.514" height="361.514" viewBox="0 0 95.651 95.651" xmlns="http://www.w3.org/2000/svg"><g transform="translate(-58.796 -13.995)"><circle style="fill:#e6e6e6;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:4.79999;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1" cx="106.621" cy="61.821" r="47.825"></circle><path style="fill:#ff5300;fill-rule:evenodd;stroke-width:3.70417;stroke-linecap:square" d="M94.271 32.634a3.252 3.252 0 0 0-3.26 3.26v31.111h-6.33a2.612 2.01 0 0 0-2.194 3.101l21.58 25.686a2.99 2.3 0 0 0 5.02 0l21.685-25.81a2.507 1.929 0 0 0-2.105-2.977h-6.525V35.894a3.252 3.252 0 0 0-3.26-3.26z"></path></g></svg>
</div>
</footer>

推荐答案

使用滚动驱动动画,可以使用以下操作:

footer {
  animation: hide linear;
  animation-timeline: view(block);
  animation-range-start: entry 0%;
  --h: initial;
}

@keyframes hide {
  0.1%,
  100% {
    --h: none;
  }
}

当页脚进入滚动元素的可见部分时(我们看到页脚),您将变量更新为none,该变量用于定义页脚的显示.

body {
  margin: 0;
}

.arrow {
  position: fixed;
  display: var(--h);
  right: 0;
  bottom: 0;
  margin: -10px 10px 0 0;
  z-index: 100;
  opacity: 0.4;
}

.arrow svg {
  width: 80px;
  height: 80px;
}

.force-scrollbar {
  height: 1000px;
}

footer {
  background: black;
  height: 200px;
  animation: hide linear;
  animation-timeline: view(block);
  animation-range-start: entry 0%;
  --h: initial;
}

@keyframes hide {
  0.1%,
  100% {
    --h: none;
  }
}
<div class="force-scrollbar">

</div>

<footer>
  <div class="arrow">
    <svg width="361.514" height="361.514" viewBox="0 0 95.651 95.651" xmlns="http://www.w3.org/2000/svg"><g transform="translate(-58.796 -13.995)"><circle style="fill:#e6e6e6;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:4.79999;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1" cx="106.621" cy="61.821" r="47.825"></circle><path style="fill:#ff5300;fill-rule:evenodd;stroke-width:3.70417;stroke-linecap:square" d="M94.271 32.634a3.252 3.252 0 0 0-3.26 3.26v31.111h-6.33a2.612 2.01 0 0 0-2.194 3.101l21.58 25.686a2.99 2.3 0 0 0 5.02 0l21.685-25.81a2.507 1.929 0 0 0-2.105-2.977h-6.525V35.894a3.252 3.252 0 0 0-3.26-3.26z"></path></g></svg>
  </div>
</footer>

Css相关问答推荐

如何使重复线性渐变适合字体大小?

material 设计--Bootstrap输入域问题

本机CSS嵌套混乱

如果 css 仅位于 razor 页面中的一页上,我是否仍应在 wwwroot 中写入或仅将其放在页面上?

如何为图像堆叠效果下方的图层实现与磨砂玻璃类似的视觉效果

Bootstrap 和 Sass:将 SCSS 文件转换为 CSS 时出错.错误:$theme-colors-rgb 中未定义变量 @each $color、$value

当值小于 1 时理解 flex-shrink

如何使用 Tailwind CSS 稳定 React 中的浮动标签?

如何在 VS Code 中获得 JavaFX CSS 属性的自动完成功能?

如何更改导航栏标题中链接的文本 colored颜色 和导航丸中的链接(在shiny 的应用程序中)?

Tailwind CSS 隐藏和可见

CSS 字符串变量的正确null值是多少?

我无法覆盖 react ui 库中的组件样式

为什么 flexbox 中的 在应用居中时不会调整大小?

使用 Flex Box Tailwind CSS 的元素不相等

删除 IE10 Select 元素箭头

溢出的文本可以居中吗?

Sass 无效的 CSS 错误:预期的表达式

在渲染网页之前等待字体加载

在 CSS 中使用多个 @font-face 规则