问题只属于AMP。我相信这是一个简单的问题。尝试为元素定位,它在js控制台中显示警告,标头移动了几个像素到它应该在的位置。我看过示例,但是代码/样式太多了,所以不明白它们是如何完成这个任务的。
我需要使元素固定在顶部,而滚动页,这个元素包含两个按钮-菜单和共享和网站标题,好如果按钮可以浮动左右。
在通常的页面上,无论是移动页面还是桌面页面,我都没有遇到类似的问题,但我是AMP的新手。
发布于 2018-10-10 21:50:45
我在那里找到了一个解决方案:https://amp-demos.glitch.me/sticky-header.html --它不同于我所需要的--它根据文章的一部分选择菜单项,目前使用动画和观察者插件进行阅读。然而,我发现它很容易修改。在我的例子中,菜单按其应有的方式移动,并有子菜单项。动画删除,这很容易押回来,只需比较两个版本。
CSS:
:root {
--header-height: 3em;
}
.sticky-header {
position: sticky;
position: -webkit-sticky;
top: 0;
left: 0;
right: 0;
height: var(--header-height);
z-index: 1000;
}
.sticky-header .item {
position: relative;
}
.sticky-header .item > * {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
line-height: var(--header-height);
}
.sticky-header .item .selected {
opacity: 0;
}
/* move anchor sections below the sticky header */
:target {
display: block;
position: relative;
top: calc(-1 * var(--header-height) - 8px);
visibility: hidden;
}
/* general styling */
h1 {
text-align: center;
}
h1, main {
margin: 1rem;
}
a, a:visited {
color: inherit;
text-decoration: inherit;
background-color: inherit;
}
.sticky-header {
display: flex;
align-items: center;
justify-content: space-around;
background-color: inherit;
}
.sticky-header .item {
height: 100%;
width: 100%;
}
.sticky-header .item > * {
text-align: center;
color: inherit;
}
.sticky-header .item .selected {
color: red;
}
.submenu {
display:none;
}
.item:hover .submenu {
display: block;
position: relative;
top: var(--header-height);
line-height: 2em; //calc(1em + 12px);
background: black;
}
.item:hover .submenu a {
display: block;
}Html部件,简化:
<div class="sticky-header">
<div class="item" tabindex="0" role="button">
<a href="#item1">Item 1</a>
<div class="submenu">
<div tabindex="0" role="button"><a href="#s1">Sub menu</a></div>
<div><a href="#s2">Sub menu</a></div>
<div><a href="#s3">Sub menu</a></div>
</div>
</div>
<div class="item" tabindex="0" role="button">
<a href="#item2">Item 2</a>
</div>
<div class="item" tabindex="0" role="button">
<a href="#item3">Item 3</a>
</div>
@Ryan在评论中提供了他自己的方法,但是我发现很难检查站点所需的代码,然后只是一个简单的小例子。
https://stackoverflow.com/questions/52540619
复制相似问题