如何让元素在进入页面时滚动到滚动条的中心

2024-06-09

我正在制作一个 Angular 项目,它有一个“时间轴”组件,其中有一个overflow-x: scroll元素:

.container {
    width: 30vw;
    height: 100vh;
    background-color: aquamarine;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
}

.timeline-box {
    width: 90%;
    position: relative;
}

.timeline-strip {
    background-color: yellow;
    height: 200px;
    display: flex;
    overflow-x: scroll; 
}

.timeline-box:before {
    content: '';
    position: absolute;
    top: 50%;
    border-top: 2px solid black;
    width: 100%;
    height: 0px;
}

.point {
    margin: 0 40px;
    align-self:center;
}
.point:before {
    content: '';
    position: relative;
    left: 50%;
    border-left: 2px solid black;
    top: 20px;
}
  <div class="container">
    <div class="timeline-box">
      <div class="timeline-strip">

        <div class="point">1</div>
        <div class="point">2</div>
        <div class="point">3</div>
        <div class="point">4</div>
        <div class="point">5</div>
        <div class="point">6</div>
        <div class="point">7</div>
        <div class="point">8</div>
        <div class="point">9</div>
        <div class="point">10</div>

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

我想要的滚动条.timeline-strip当用户进入页面时,元素(不是页面本身的滚动条)自动位于中间。我怎样才能实现这个目标? (如果有帮助的话我正在使用 Angular)


您可以通过以下方式做到这一点scrollWidth财产减去clientWidth属性并将结果除以 2。这将是滚动位置的一半,因此您可以将此值设置为scrollLeft财产。

如果您的 div 包含图像,那么您需要在窗口加载事件上执行此操作。在文档 DOMContentLoaded 事件中,图像尚未加载(除非已兑现),因此当加载图像时,div 的大小可能会发生变化。

//Can be document.addEventListener('DOMContentLoaded', () => {}) when you don't care about images and stuff
window.addEventListener('load', () => {
  let scrollElement = document.querySelector('.timeline-strip');
  scrollElement.scrollLeft =  (scrollElement.scrollWidth - scrollElement.clientWidth ) / 2;
});
.container {
    width: 30vw;
    height: 100vh;
    background-color: aquamarine;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
}

.timeline-box {
    width: 90%;
    position: relative;
}

.timeline-strip {
    background-color: yellow;
    height: 200px;
    display: flex;
    overflow-x: scroll; 
}

.timeline-box:before {
    content: '';
    position: absolute;
    top: 50%;
    border-top: 2px solid black;
    width: 100%;
    height: 0px;
}

.point {
    margin: 0 40px;
    align-self:center;
}
.point:before {
    content: '';
    position: relative;
    left: 50%;
    border-left: 2px solid black;
    top: 20px;
}
<div class="container">
    <div class="timeline-box">
      <div class="timeline-strip">

        <div class="point">1</div>
        <div class="point">2</div>
        <div class="point">3</div>
        <div class="point">4</div>
        <div class="point">5</div>
        <div class="point">6</div>
        <div class="point">7</div>
        <div class="point">8</div>
        <div class="point">9</div>
        <div class="point">10</div>

      </div>
    </div>
  </div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何让元素在进入页面时滚动到滚动条的中心 的相关文章

  • 如何使用jQuery加载跨域html

    我有 2 个不同的 java web 项目在 2 个不同的 tomcat 服务器上运行 假设 projA 和 projB 在这里 我尝试从 projA 加载 projB 中可用的 html 我只是使用 jQuery load 来实现这一点
  • 如何让机器人状态显示为“手机在线”

    我看到 NotSoBot 的状态为在线在移动 https i stack imgur com IiF4k png 有没有办法可以在discord js 中做到这一点 我知道这已经很旧了 但更简单的方法是将其添加到定义客户端 机器人的位置 c
  • 递归process.nextTick警告

    作为我的应用程序的一部分 我有以下代码行 process nextTick function pre populate cache with all users console log scanning users table in ord
  • 在没有 Flash 的情况下用 HTML / js 录制音频?

    从麦克风录制音频并将 ajax 发回服务器 真的不知道如何在 Flash 中做到这一点 并希望它保持简单 目前还没有浏览器实现WHATWG 设备 API http www whatwg org specs web apps current
  • 闭包和异步 Node.js 函数

    All 尝试了解 Node js 上下文中的闭包 异步调用 我有以下代码 timer setInterval pollOID 1000 function pollOID for channel in channels session get
  • Angularjs 使用 ng-init 为 ng-model 赋值

    您好 我有以下问题 看起来很简单并且应该有效 但事实并非如此 在我的代码中我输入了
  • xsl 方法中的自关闭标签:xml

    我正在使用一个使用 xsl method xml 创建 html 模板的网站 但是 当 xsl 引擎呈现 html 页面时 我遇到了标签自动关闭的问题 div div 转换为 gt div div 该方法需要保留 xml 否则页面的其他组件
  • 如何在我的库中声明/包含导入的 scss 文件夹,以便我可以在应用程序的 scss 文件中调用 @import 它?

    目标 我正在尝试包含我的库中的路径 以便我可以使用 import声明将 scss 文件包含在我的应用程序中 如下所示 import some scss in my lib 问题 不幸的是 每次我尝试为我的应用程序提供服务时 我都会收到以下编
  • Backbone.View:delegateEvents 未将事件重新绑定到子视图

    我已将这个问题分解为尽可能小的示例 即 它只是为了演示问题 不一定代表现实世界的场景 假设我有一个父视图 此处为 MainView 其中包含一个子视图 此处为 SubView 如果在任何时候我需要重新渲染父视图 从而重新渲染子视图 我就会丢
  • 将变量从一个 jsp 发送到另一个 jsp

    我有一个 JSP 文件jsp 1 jsp和另一个 JSP 文件jsp 2 jsp 我已经包括了jsp 2 jsp in jsp 1 jsp using 现在我需要某个元素上的单击事件 在该事件中 我想将字符串变量传输到包含的 jsp 中 假
  • 右侧对齐不浮动

    我有一个聊天小部件 我正在向其中添加一些样式 但是 我很难使 用户 聊天气泡与屏幕右侧对齐 当我使用向右浮动和向左浮动 另一侧 时 div 不再正确定位 因为它们似乎只是转到相对 div 的右侧 我希望它也能够附加 div 这将导致溢出 y
  • 使用Rxjs映射函数时如何保留对象属性大小写

    我是 Angular2 的新手 正在使用 http 服务从 asp net mvc webapi 获取数据 API返回的数据格式如下所示 RequestResponse public string Message get set publi
  • 在 *ngFor 中创建局部变量

    是否可以在 ngFor 中创建局部变量 例如 div 您可以将 ng container 与 ngIf 结合使用来实现此目的 div class message list div div
  • Firefox 本地主机上的 Twilio 屏幕共享?

    目前 Firefox 中本地主机上的屏幕共享会引发以下错误 The request is not allowed by the user agent or the platform in the current context 这是我的代码
  • 如何捕获 google 地图的无效 API 密钥

    我有这个代码 如果密钥无效 则会弹出警报 但我想在这种情况下执行一些操作 但我不知道如何连接它 有任何想法吗 Google 不提供检查 Google 地图 API 密钥的外部方法 因此 您无法使用例如查询某些服务 此代码有效吗abcde12
  • 事件监听器如何/何时附加到 d3.js 中?

    我正在尝试制作一个 SVG 编辑器 长话短说 我需要将鼠标事件附加到
  • 在 Angular 6 和 ASP.net 中启用 CORS

    我正在尝试从 Angular 6 应用程序向发送电子邮件的 API 发送 POST 请求 我用邮递员测试了该请求并且它有效 但是当我在 Angular 应用程序中执行 POST 请求时 我的控制台中出现错误 我在 Angular 应用程序中
  • 如何防止外部 CSS 添加和覆盖 ReactJS 组件样式

    我有一个自定义的 ReactJS 组件 我想以某种方式设置样式 并将其作为插件提供给许多不同的网站 但是 当网站使用全局样式 Twitter bootstrap 或其他 css 框架 时 它会添加并覆盖我的组件的样式 例如 全局 css l
  • 将画布下载为 PNG 图像[重复]

    这个问题在这里已经有答案了 当我尝试将画布下载为 PNG 图像时 浏览器会在新页面中打开该图像 但不下载它 我的下载代码 btnScaricaEtichetta click function console log Download loc
  • 如何使相对div居中?

    我一直在尝试让以下代码工作几个小时 但没有成功 您能帮我将项目 div 居中吗 即使页面放大和缩小时 这是我的 HTML 和 CSS bottom position absolute top 100 left 0 right 0 backg

随机推荐