【发布时间】:2015-06-09 14:11:12
【问题描述】:
单击导航按钮时,我正在使用 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>
【问题讨论】:
-
你的导航按钮怎么样?
-
我正在编辑问题,请参阅
-
你说你想在用户滚动页面时做点什么,但是你的代码在哪里负责处理onscroll event?
-
@sergey 实际上在我的页面上,我有在点击链接时更改 url 的效果,但我在滚动时无法执行此操作。所以我想知道怎么做,因为我不知道这个
-
您可能会发现这个问题的答案很有帮助:stackoverflow.com/questions/14660580/…
标签: javascript jquery html try-catch