gpt4 book ai didi

javascript - 滚动时更改网址

转载 作者:太空狗 更新时间:2023-10-29 13:30:59 25 4
gpt4 key购买 nike

当我点击导航按钮时,我正在使用 jQuery 更改 URL 散列。但是当我使用鼠标滚轮滚动时,URL 不会改变。我怎样才能做到这一点?如何使用

完成
window.history.pushState(“object or string”, “Title”, “/new-url”);

我无法理解。请帮忙我的 javascript 代码

$j(document).ready(function () {
$j("#start1").click(function (e) {
e.preventDefault();
var section = this.href,
sectionClean = section.substring(section.indexOf("#"));

$j("html, body").animate({
scrollTop: $j(sectionClean).offset().top
}, 1000, function () {
window.location.hash = sectionClean;
});
});
});
$j(document).ready(function () {
$j("#start2").click(function (e) {
e.preventDefault();
var section = this.href,
sectionClean = section.substring(section.indexOf("#"));

$j("html, body").animate({
scrollTop: $j(sectionClean).offset().top
}, 1000, function () {
window.location.hash = sectionClean;
});
});
});

html代码是

<a href="#home" id="start1"style="text-decoration:none;position:absolute;right:450px;top:37px;font-weight:bold;color:white;font-size:15px;z-index:200;transition:0.5s"  onmouseover="big(this)" onmouseout="small(this)"><span >HOME</span></a>
<span><a href="#products" id="start2" style="text-decoration:none;position:absolute;right:250px;top:37px;font-weight:bold;color:white;font-size:15px;transition:0.5s" onmouseover="big(this)" onmouseout="small(this)">PRODUCTS & SERVICES</a></span>

最佳答案

下面是一些可能对您有帮助的代码 - 它监听 scroll 事件,并检查视口(viewport)中是否有任何 anchor 元素。如果是,则使用 window.history.pushState 更新 URL 哈希。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Scrolling URL Hash</title>
<meta name="description" content="Webpage for xxxx">
<style>
body {
height: 2000px;
}
a > div {
min-height: 500px;
}
</style>
</head>
<body>
<a href="#anchor1" id="anchor1" class="anchor"><div>ANCHOR 1</div></a>
<a href="#anchor2" id="anchor2" class="anchor"><div>ANCHOR 2</div></a>
<a href="#anchor3" id="anchor3" class="anchor"><div>ANCHOR 3</div></a>
<a href="#anchor4" id="anchor4" class="anchor"><div>ANCHOR 4</div></a>
<a href="#anchor5" id="anchor5" class="anchor"><div>ANCHOR 5</div></a>
<a href="#anchor6" id="anchor6" class="anchor"><div>ANCHOR 6</div></a>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
// stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport
function isElementInViewport (el) {
//special bonus for those using jQuery
if (typeof jQuery === "function" && el instanceof jQuery) {
el = el[0];
}
var rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
);
}
// click-to-scroll behavior
$(".anchor").click(function (e) {
e.preventDefault();
var section = this.href;
var sectionClean = section.substring(section.indexOf("#"));
$("html, body").animate({
scrollTop: $(sectionClean).offset().top
}, 1000, function () {
window.location.hash = sectionClean;
});
});
// listen for the scroll event
$(document).on("scroll", function() {
console.log("onscroll event fired...");
// check if the anchor elements are visible
$(".anchor").each(function (idx, el) {
if ( isElementInViewport(el) ) {
// update the URL hash
if (window.history.pushState) {
var urlHash = "#" + $(el).attr("id");
window.history.pushState(null, null, urlHash);
}
}
});
});
</script>
</body>
</html>

关于javascript - 滚动时更改网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30734552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com