JQuery scrollTop 问题
JQuery scrollTop issue
我直接跳到问题上。
我的网页中有一些部分,每个部分都有一个 id,我有一个固定的导航栏,其中包含指向这些部分的链接。
所以我想滚动到这些部分并使用偏移量,这样它就不会与我的导航栏重叠。我使用了这段代码:
$('a[href^="#"]').on('click', function(event) {
var target = $(this.getAttribute('href'));
if( target.length ) {
event.preventDefault();
$('html, body').stop().animate({
scrollTop: target.offset().top
}, 1000);
}
});
好吧,它在 chrome 中运行良好,但在边缘和 chrome 隐身模式下都不起作用。它有点停用链接,它们不起作用。
我在这个网站和其他网站上测试了很多不同的解决方案,其中 none 解决了这个问题。
唯一的解决方案是这个 css 代码:
:target::before {
content: "";
display: block;
height: 45px; /* fixed header height*/
margin: -45px 0 0; /* negative fixed header height */
}
这是完美的,但唯一的问题是我在 wordpress 上有这个网站,所以有这个管理栏,正如我所说的,我也有一个固定的导航栏,所以在某些情况下偏移值必须改变。
我还试图用 jquery 更改该值,但我遇到了另一个问题,即选择 css 属性。我找不到 select ':target::before' 的方法并更改它的 属性。
我真的不关心动画。我只是想让这东西起作用。
感谢您的帮助。
您可以尝试参考下面的示例,可能会帮助您解决问题。
该代码在 MS Edge 中运行良好,在 Chrome 隐身模式下也运行良好。
代码在固定导航栏和页面上的其他元素之间留出适当数量的 space。
示例:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#tp").click(function(){
var jump = $("#top").attr('href');
var new_position = $(jump).offset();
var n= new_position.top - 60;
$('html, body').stop().animate({ scrollTop: n}, 500);
});
$("#mdl").click(function(){
var jump = $("#middle").attr('href');
var new_position = $(jump).offset();
var n= new_position.top - 60;
$('html, body').stop().animate({ scrollTop: n }, 500);
});
$("#btm").click(function(){
var jump = $("#bottom").attr('href');
var new_position = $(jump).offset();
var n= new_position.top - 60;
$('html, body').stop().animate({ scrollTop: n }, 500);
});
});
</script>
<style>
body {
padding-top:50px;
}
.navbar {
overflow: hidden;
background-color: #333;
position: fixed;
top: 0;
width: 100%;
}
.navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.navbar a:hover {
background: #ddd;
color: black;
}
.main {
padding: 16px;
margin-top: 10px;
height: 1500px; /* Used in this example to enable scrolling */
}
a {
background: orange;
color: #444;
font-family: sans-serif;
text-align: center;
text-decoration: none;
padding: 20px;
display: block;
}
#star {
text-align: center;
font-size: 100px;
line-height: 500px;
color: #ddd;
}
Resources
</style>
</head>
<body>
<div class="main">
<div class="navbar">
<a href="#1" id="tp">Top</a>
<a href="#2" id="mdl">Middle</a>
<a href="#3" id="btm">Bottom</a>
</div>
<a href="#top" id="top">Top element</a>
<div id="star">☆</div>
<a href="#middle" id="middle">Middle element</a>
<div id="star">☆</div>
<a href="#bottom" id="bottom">Bottom element</a>
<div id="star">☆</div>
<div id="star">☆</div>
</div>
</body>
</html>
MS Edge 浏览器中的输出:
Chrome隐身模式下的输出:
我直接跳到问题上。 我的网页中有一些部分,每个部分都有一个 id,我有一个固定的导航栏,其中包含指向这些部分的链接。 所以我想滚动到这些部分并使用偏移量,这样它就不会与我的导航栏重叠。我使用了这段代码:
$('a[href^="#"]').on('click', function(event) {
var target = $(this.getAttribute('href'));
if( target.length ) {
event.preventDefault();
$('html, body').stop().animate({
scrollTop: target.offset().top
}, 1000);
}
});
好吧,它在 chrome 中运行良好,但在边缘和 chrome 隐身模式下都不起作用。它有点停用链接,它们不起作用。 我在这个网站和其他网站上测试了很多不同的解决方案,其中 none 解决了这个问题。 唯一的解决方案是这个 css 代码:
:target::before {
content: "";
display: block;
height: 45px; /* fixed header height*/
margin: -45px 0 0; /* negative fixed header height */
}
这是完美的,但唯一的问题是我在 wordpress 上有这个网站,所以有这个管理栏,正如我所说的,我也有一个固定的导航栏,所以在某些情况下偏移值必须改变。 我还试图用 jquery 更改该值,但我遇到了另一个问题,即选择 css 属性。我找不到 select ':target::before' 的方法并更改它的 属性。 我真的不关心动画。我只是想让这东西起作用。 感谢您的帮助。
您可以尝试参考下面的示例,可能会帮助您解决问题。
该代码在 MS Edge 中运行良好,在 Chrome 隐身模式下也运行良好。
代码在固定导航栏和页面上的其他元素之间留出适当数量的 space。
示例:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#tp").click(function(){
var jump = $("#top").attr('href');
var new_position = $(jump).offset();
var n= new_position.top - 60;
$('html, body').stop().animate({ scrollTop: n}, 500);
});
$("#mdl").click(function(){
var jump = $("#middle").attr('href');
var new_position = $(jump).offset();
var n= new_position.top - 60;
$('html, body').stop().animate({ scrollTop: n }, 500);
});
$("#btm").click(function(){
var jump = $("#bottom").attr('href');
var new_position = $(jump).offset();
var n= new_position.top - 60;
$('html, body').stop().animate({ scrollTop: n }, 500);
});
});
</script>
<style>
body {
padding-top:50px;
}
.navbar {
overflow: hidden;
background-color: #333;
position: fixed;
top: 0;
width: 100%;
}
.navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.navbar a:hover {
background: #ddd;
color: black;
}
.main {
padding: 16px;
margin-top: 10px;
height: 1500px; /* Used in this example to enable scrolling */
}
a {
background: orange;
color: #444;
font-family: sans-serif;
text-align: center;
text-decoration: none;
padding: 20px;
display: block;
}
#star {
text-align: center;
font-size: 100px;
line-height: 500px;
color: #ddd;
}
Resources
</style>
</head>
<body>
<div class="main">
<div class="navbar">
<a href="#1" id="tp">Top</a>
<a href="#2" id="mdl">Middle</a>
<a href="#3" id="btm">Bottom</a>
</div>
<a href="#top" id="top">Top element</a>
<div id="star">☆</div>
<a href="#middle" id="middle">Middle element</a>
<div id="star">☆</div>
<a href="#bottom" id="bottom">Bottom element</a>
<div id="star">☆</div>
<div id="star">☆</div>
</div>
</body>
</html>
MS Edge 浏览器中的输出:
Chrome隐身模式下的输出: