首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置可滚动DIV的默认位置?

如何设置可滚动DIV的默认位置?
EN

Stack Overflow用户
提问于 2019-10-27 13:35:30
回答 1查看 2.4K关注 0票数 1

我使用scroll-snap-type来浏览DIVs并将它们放到视图中。我想要做的是在默认情况下将视图设置为第二个DIV,所以当页面加载带有“在默认情况下应该查看”的div时,用户就可以向左或向右滚动。

我怎么才能用CSS来完成这个任务呢?(没有联合来文)

https://jsfiddle.net/2hcgoL1b/2/

代码语言:javascript
复制
html, body, .holster {
  height: 100%;
}


.container {
  display: flex;
  overflow: auto;
  outline: 1px dashed lightgray;
  flex: none;
}

.container.x {
  width: 100%;
  height: 128px;
  flex-flow: row nowrap;
}


/* scroll-snap */
.x.mandatory-scroll-snapping {
  scroll-snap-type: x mandatory;
}


.container > div {
  text-align: center;
  scroll-snap-align: center;
  flex: none;
}

.x.container > div {
  line-height: 128px;
  font-size: 64px;
  width: 100%;
  height: 128px;
}






/* appearance fixes */
.y.container > div:first-child {
  line-height: 1.3;
  font-size: 64px;
}
/* coloration */
.container > div:nth-child(even) {
  background-color: #87EA87;
}

.container > div:nth-child(odd) {
  background-color: #87CCEA;
}
代码语言:javascript
复制
<div class="container x mandatory-scroll-snapping" dir="ltr">
  <div>1</div>
  <div>This should be viewed by default</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-27 13:58:38

您将需要javascripttabindex属性和idCSS在这里没有任何帮助--

免责声明--这对你的访问者来说是件烦人的事,就像老把戏<div tabindex="0" autofocus="autofocus">一样。

代码语言:javascript
复制
window.onload= document.getElementById("focus").focus();
代码语言:javascript
复制
/* setup */
html, body, .holster {
  height: 100%;
}


.container {
  display: flex;
  overflow: auto;
  outline: 1px dashed lightgray;
  flex: none;
}

.container.x {
  width: 100%;
  height: 128px;
  flex-flow: row nowrap;
}


/* scroll-snap */
.x.mandatory-scroll-snapping {
  scroll-snap-type: x mandatory;
}


.container > div {
  text-align: center;
  scroll-snap-align: center;
  flex: none;
}

.x.container > div {
  line-height: 128px;
  font-size: 64px;
  width: 100%;
  height: 128px;
}






/* appearance fixes */
.y.container > div:first-child {
  line-height: 1.3;
  font-size: 64px;
}
/* coloration */
.container > div:nth-child(even) {
  background-color: #87EA87;
}

.container > div:nth-child(odd) {
  background-color: #87CCEA;
}
代码语言:javascript
复制
<div class="container x mandatory-scroll-snapping" dir="ltr">
  <div>1</div>
  <div tabindex="0" id="focus">This should be viewed by default</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>

要允许元素获得焦点,如果它不是链接或表单元素,则需要一个属性。

https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex

tabindex全局属性指示其元素可以聚焦,并参与顺序键盘导航(通常使用Tab键,因此名称)。

注意:一旦元素有了id,您应该能够链接它:http://site.html#MyId

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58579905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档