javascript - 在水平滚动 flexbox div 的边缘实现滚动指示器

标签 javascript html css flexbox

在我的网站上,我在导航栏中使用了一个可水平滚动的 flexbox。我使用这篇博文的帮助来实现它,所以或多或少,我们有相同的代码:https://iamsteve.me/blog/entry/using-flexbox-for-horizontal-scrolling-navigation

HTML:

<header class="scroll">
  <nav>
    <a class="item" href="http://example.com/blog">Blog</a>
    <a class="item" href="http://example.com/portfolio">Portfolio</a>
    <a class="item" href="http://example.com/downloads">Downloads</a>
    <a class="item" href="http://example.com/about">About</a>
    <a class="item" href="http://example.com/contact">Contact</a>
  </nav>
</header>

CSS:

.scroll {
  display: flex;
  flex-wrap: nowrap;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  -ms-overflow-style: -ms-autohiding-scrollbar; }
.item {
  flex: 0 0 auto; }

不过,该博文没有包括如何向边缘的导航栏添加指示器,以指示此栏可滚动并且右侧有更多内容。

我查找了一些解决方案,但没有一个与我在此处使用的 flexbox 技术非常吻合。这怎么能正确,整齐地完成呢?它需要花车吗?提前致谢!

最佳答案

您可以使用以下技术 我创建了这个 plunk 来展示它是如何工作的。 https://embed.plnkr.co/XzDEedEsgS1hDDmVuccZ/

.scroll {
  position: relative;
}

.scroll nav {
  display: flex;
  flex-wrap: nowrap;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  -ms-overflow-style: -ms-autohiding-scrollbar;
  position: relative;
}

.scroll:after { /* This is our indicator give it your custom style */
  content: ">>";
  position: absolute;
  width: 20px;
  right: 0;
  top: 0;
  color: #fff;
  padding: 0 10px;
  background: #333;
}

.item {
  flex: 1 1 auto;
  padding-right: 10px;
  background: #fff;
}

.item:last-child {
  position: relative;
  z-index: 9; /* higher z-index to get it above the indicator */
}

关于javascript - 在水平滚动 flexbox div 的边缘实现滚动指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47245687/

相关文章:

javascript - ExtJS 4.2 同名表单中的两个文本字段

javascript - Angular-modal-service 不显示模态

javascript - 两个变量声明之间的区别

html - 有没有办法让两个 <h> 标签彼此齐平(没有自动换行符)?

html - 将文本基线对齐到 div 的底部

html - ASP.NET MVC 3 - 隔离 HTML.Raw 输出

javascript - 将对象传递给 html 字符串

javascript - 我的滚动横幅代码哪里出了问题以使其正确滚动

css - 如何在级联表的右侧放置一个 div - CSS

html - CSS:在段落中穿插图像