Javascript 为什么我的锚在第二次点击后表现不同?
当我加载网站并点击Wirüber uns时,它不会像我再次点击按钮(向上滚动后)那样向下滚动。它应该向下滚动到节的开头,当它向下滚动到第二节时,第一节中的图片不应该可见(就像第二次单击它时一样)Javascript 为什么我的锚在第二次点击后表现不同?,javascript,html,jquery,css,Javascript,Html,Jquery,Css,当我加载网站并点击Wirüber uns时,它不会像我再次点击按钮(向上滚动后)那样向下滚动。它应该向下滚动到节的开头,当它向下滚动到第二节时,第一节中的图片不应该可见(就像第二次单击它时一样) //标题为beim scrollen $(函数(){ var shrinkHeader=100; $(窗口)。滚动(函数(){ var scroll=getCurrentScroll(); 如果(滚动>=收缩页眉){ $('#navbar').addClass('shrink'); }否则{ $(“#
//标题为beim scrollen
$(函数(){
var shrinkHeader=100;
$(窗口)。滚动(函数(){
var scroll=getCurrentScroll();
如果(滚动>=收缩页眉){
$('#navbar').addClass('shrink');
}否则{
$(“#navbar”).removeClass('shrink');
}
});
函数getCurrentScroll(){
return window.pageYOffset | | document.documentElement.scrollTop;
}
});
//JavaScript文档
$(文档).ready(函数(){
var navTop=$('#navbar').offset().top;
var navHeight=$('#navbar').height();
var windowH=$(window.height();
$('.section')。高度(窗H);
$(文档)。滚动(函数(){
var st=$(this.scrollTop();
//对于导航栏:
如果(st>navTop){
$('#navbar').addClass('fix');
$('.section:eq(0)').css({
“页边距顶部”:导航高度
});//修复由于修复导航栏导致的滚动问题
}否则{
$(“#navbar”).removeClass('fix');
$('.section:eq(0)').css({
“页边距顶部”:“0”
});
}
$('.section')。每个(函数(索引,元素){
如果(st+navHeight>$(this).offset().top&&st+navHeight首次加载页面时,在#home
#home {
height: 100% !important;
display: block;
margin-top: -80px;
padding-top: 80px;
z-index: -1;
position: relative;
top: -80px;
}
当您开始滚动时,您会更改此页边距。但是,当再次滚动回顶部时,您不会将页边距重置为-80px
,这将导致您在第一次加载页面和第二次单击按钮时得到不同的结果。@JannisN另一个提示可能会考虑使用而不是修复。这非常有用您不必在开始滚动时使用填充或边距来修复页面。但是,请注意,它可能存在一些浏览器可对抗性问题。它是否简单到只需将“固定”替换为“粘滞”即可?有什么大的地方需要更改吗?@JannisN您需要删除负边距和顶部值,然后进行更改还有一些事情。我在这里编辑了你的JS提琴,并添加了一些评论,说“删除这个/添加这个到你需要更改的内容中”,这看起来很简单,效果也很好!非常感谢!我可以把主页
也移到哪里去吗?当我点击主页按钮时,它并不会真正把我带到页面的最顶端。导航栏看起来很滑当你向下滚动一点点的时候,它就会消失。出于某种原因,它开始抖动。这是我的JSFIDLE:
@charset "utf-8";
/* CSS Document */
* {
font-family: 'Roboto', sans-serif;
}
#container {
width: 1280px;
height: 100%;
margin-left: auto;
margin-right: auto;
}
body {
background-color: white;
margin: 128px 0 0 0;
}
/* Navigation */
#navlinks {
margin: 0;
}
ul {
color: black;
list-style: none;
float: right;
margin-right: 20px;
padding-top: 30px;
}
ul li {
display: inline-table;
margin-left: 5px;
padding: 5px;
border-bottom: 1.5px solid;
border-bottom-color: white;
}
ul li a {
color: black;
text-decoration: none;
padding: 10px;
}
/* Smart Navbar / weiß, wo man auf der Seite ist von https://stackoverflow.com/questions/19697696/change-underline-of-active-nav-by-section */
#navbar.fix {
position: fixed;
top: 0;
}
#navbar li.active {
border-bottom: 1.5px solid;
border-bottom-color: #f6bd60;
}
/* Smart Navbar Ende */
/* fixed Navigation von https://codepen.io/malZiiirA/pen/cbfED?css-preprocessor=none */
#navbar {
border-bottom-style: solid;
border-bottom-width: 1.25px;
box-shadow: 0px 2.5px 5px rgba(0, 0, 0, 0.2);
background-color: white;
height: 128px;
transition: 0.32s;
position: fixed;
top: 0;
width: 1280px;
}
#navbar.shrink {
height: 80px;
line-height: 18px;
}
#navbar li {
font-size: 1.2rem;
font-weight: normal;
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
transition: all 0.3s;
}
#navbar.shrink li {
font-size: 1.2rem;
margin-top: -30px;
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
transition: all 0.3s;
}
#navbar img {
height: 128px;
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
transition: all 0.3s;
}
#navbar.shrink img {
height: 80px;
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
transition: all 0.3s;
margin: auto;
}
/* fixed nav Ende */
/*
#spacer {
height: 128px;
border-bottom: 4px solid;
}
margin-top: 0 !important;
*/
#home {
height: 100% !important;
display: block;
margin-top: -80px;
padding-top: 80px;
z-index: -1;
position: relative;
top: -80px;
}
#wir-ueber-uns {
height: 50px;
display: block;
margin-top: -80px;
padding-top: 80px;
z-index: -2;
background-color: lightblue;
position: relative;
top: -80px;
}
#aktionen {
height: 100%;
display: block;
margin-top: -80px;
padding-top: 80px;
z-index: -3;
background-color: blue;
position: relative;
top: -80px;
}
#terminvereinbarung {
height: 100%;
display: block;
margin-top: -80px;
padding-top: 80px;
z-index: -4;
background-color: red;
position: relative;
top: -80px;
}
#infos {
height: 100%;
display: block;
margin-top: -80px;
padding-top: 80px;
z-index: -5;
background-color: yellow;
position: relative;
top: -80px;
}
#logo {
margin-left: 50px;
}
#homebild {
width: 100%;
height: 100%;
}
#impressum {
background-color: darkgreen;
}
/* Hover Effekt bei Navigation von https://github.com/IanLunn/Hover/blob/master/css/hover.css */
.hvr-sweep-to-top {
display: inline-block;
vertical-align: middle;
-webkit-transform: perspective(1px) translateZ(0);
transform: perspective(1px) translateZ(0);
box-shadow: 0 0 1px rgba(0, 0, 0, 0);
position: relative;
-webkit-transition-property: color;
transition-property: color;
-webkit-transition-duration: 0.3s;
transition-duration: 0.3s;
}
.hvr-sweep-to-top:before {
content: "";
position: absolute;
z-index: -1;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #f6bd60;
-webkit-transform: scaleY(0);
transform: scaleY(0);
-webkit-transform-origin: 50% 100%;
transform-origin: 50% 100%;
-webkit-transition-property: transform;
transition-property: transform;
-webkit-transition-duration: 0.3s;
transition-duration: 0.3s;
-webkit-transition-timing-function: ease-out;
transition-timing-function: ease-out;
}
.hvr-sweep-to-top:hover,
.hvr-sweep-to-top:focus,
.hvr-sweep-to-top:active {
color: white;
}
.hvr-sweep-to-top:hover:before,
.hvr-sweep-to-top:focus:before,
.hvr-sweep-to-top:active:before {
-webkit-transform: scaleY(1);
transform: scaleY(1);
}
/* Hover Effekt Ende */
/* Loader */
.loader {
display: inline-block;
width: 30px;
height: 30px;
position: relative;
border: 4px solid #Fff;
animation: loader 2s infinite ease;
}
.loader-inner {
vertical-align: top;
display: inline-block;
width: 100%;
background-color: #fff;
animation: loader-inner 2s infinite ease-in;
}
@keyframes loader {
0% {
transform: rotate(0deg);
}
25% {
transform: rotate(180deg);
}
50% {
transform: rotate(180deg);
}
75% {
transform: rotate(360deg);
}
100% {
transform: rotate(360deg);
}
}
@keyframes loader-inner {
0% {
height: 0%;
}
25% {
height: 0%;
}
50% {
height: 100%;
}
75% {
height: 100%;
}
100% {
height: 0%;
}
}
.loader-wrapper {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
background-color: #242f3f;
display: flex;
align-items: center;
justify-content: center;
}
/* loader Ende */
//header Effekt beim scrollen
$(function() {
var shrinkHeader = 100;
$(window).scroll(function() {
var scroll = getCurrentScroll();
if (scroll >= shrinkHeader) {
$('#navbar').addClass('shrink');
} else {
$('#navbar').removeClass('shrink');
}
});
function getCurrentScroll() {
return window.pageYOffset || document.documentElement.scrollTop;
}
});
// JavaScript Document
$(document).ready(function() {
var navTop = $('#navbar').offset().top;
var navHeight = $('#navbar').height();
var windowH = $(window).height();
$('.section').height(windowH);
$(document).scroll(function() {
var st = $(this).scrollTop();
//for the nav bar:
if (st > navTop) {
$('#navbar').addClass('fix');
$('.section:eq(0)').css({
'margin-top': navHeight
}); //fix scrolling issue due to the fix nav bar
} else {
$('#navbar').removeClass('fix');
$('.section:eq(0)').css({
'margin-top': '0'
});
}
$('.section').each(function(index, element) {
if (st + navHeight > $(this).offset().top && st + navHeight <= $(this).offset().top + $(this).height()) {
$(this).addClass('active');
var id = $(this).attr('id');
$('a[href="#' + id + '"]').parent('li').addClass('active');
// or $('#nav li:eq('+index+')').addClass('active');
} else {
$(this).removeClass('active');
var id = $(this).attr('id');
$('a[href="#' + id + '"]').parent('li').removeClass('active');
//or $('#nav li:eq('+index+')').removeClass('active');
}
});
});
});
//
#home {
height: 100% !important;
display: block;
margin-top: -80px;
padding-top: 80px;
z-index: -1;
position: relative;
top: -80px;
}