【发布时间】:2020-09-13 19:35:03
【问题描述】:
我已尝试将边距和填充都设置为 0,但左侧仍有间隙。
我在填充上尝试了 0,这往往会使我的条形太窄,不符合我的口味,但仍然无法修复左侧的事实差距。我似乎找不到一个地方说除了填充 0 之外可以做任何事情;和边距0;所以我有点不知所措。这可能是我完全忽略了。
让我的标题随着我的页面上下移动然后锁定到顶部的 javascript
window.onscroll = function() {
getsticky()
};
var header = document.getElementById('myHeader');
var sticky = header.offsetTop;
function getsticky() {
if (window.pageYOffset > sticky) {
header.classList.add('sticky');
} else {
header.classList.remove('sticky');
}
}
.header {
background-color: #141414;
padding: 10px 16px;
border-radius: 5px;
width: 100%;
margin: 0;
border-right: 0;
}
.sticky {
position: fixed;
top: 0;
width: 100%;
}
.Buttons {
background-color: #141414;
border: none;
color: white;
font-size: larger;
font-weight: bold;
padding: 20px 24px text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
border-radius: 8px
}
a {
color: white;
text-decoration: none;
}
<div class='header' id="myHeader">
<H2><button class="Buttons"><a href = 'Pricing.html'>Pricing!</a></button>
<button class="Buttons"><a href = 'About me.html'>About me</a></button>
<button class="Buttons"><a href = 'placeholder.html'>Contact me!</a></button>
</H2>
</div>
<div style="height:2000px">
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
<p>Scroll</p>
</div>
【问题讨论】:
-
为什么不定位:固定,那么就不需要javascript
标签: javascript html css