简短的回答是我通常使用history.replaceState()
来实现这一点,但还有很多事情要做。 MDN描述了这种方法:
更新的历史堆栈上最新的条目有 指定的数据,标题,并且,如果提供的URL。数据被视为DOM不透明的 ;您可以指定任何可以序列化为 的JavaScript对象。
在实践中,这是如何解决的,我创建了一个状态对象,其状态是我的页面部分。我经常将这与优秀的Waypoints plugin一起使用,触发基于航点的状态更改。这段代码很容易理解,请阅读我的评论,这些评论将引导您阅读它。
// 1- Sets up state object that will store each page section 'state'
var stateObj = { state0: "group" };
// 2- Setups up waypoint to indicate a new section
var yourNameWaypoint = new Waypoint({
// 3- Target an ID for waypoints to watch, corresponds to a page section
element: document.getElementById('page-id-target'),
handler: function(direction) {
// 4- Create down direction handler
if (direction === 'down') {
// 5- Assign stateObj a page state that corresponds to your page section
stateObj = { state1: "page-section-1" };
history.replaceState(stateObj, "Section Title", "#new-page-section-slug-here");
}
// 6- Do the same thing in the other direction now, reseting the URL to what it was before the waypoint was hit
if (direction === 'up') {
stateObj = { state0: "original-state" };
history.replaceState(stateObj, "Original Page Section Title", "original-page-slug-here");
}
}
});
让哈希对应滚动位置有点困难。我修改了一个相当不错的脚本来使其工作(http://jsfiddle.net/ianclark001/rkocah23/),但我会在这里发布原文。
这个想法很简单。基本上,您正在读取您在init
之内的上述函数中创建的每个网址哈希值,然后使用history.pushState()
将这些网址哈希值在scrollToCurrent
部分匹配。我发现我喜欢在滚动动画中加入一些延迟,以使行为更加正常(设置在500毫秒以下,但可以调整)。
(function(document, history, location) {
var HISTORY_SUPPORT = !!(history && history.pushState);
var anchorScrolls = {
ANCHOR_REGEX: /^#[^ ]+$/,
OFFSET_HEIGHT_PX: 50,
/**
* Establish events, and fix initial scroll position if a hash is provided.
*/
init: function() {
this.scrollToCurrent();
$(window).on('hashchange', $.proxy(this, 'scrollToCurrent'));
$('body').on('click', 'a', $.proxy(this, 'delegateAnchors'));
},
/**
* Return the offset amount to deduct from the normal scroll position.
* Modify as appropriate to allow for dynamic calculations
*/
getFixedOffset: function() {
return this.OFFSET_HEIGHT_PX;
},
/**
* If the provided href is an anchor which resolves to an element on the
* page, scroll to it.
* @param {String} href
* @return {Boolean} - Was the href an anchor.
*/
scrollIfAnchor: function(href, pushToHistory) {
var match, anchorOffset;
if(!this.ANCHOR_REGEX.test(href)) {
return false;
}
match = document.getElementById(href.slice(1));
if(match) {
anchorOffset = $(match).offset().top - this.getFixedOffset();
$('html, body').delay(500).animate({ scrollTop: anchorOffset});
// Add the state to history as-per normal anchor links
if(HISTORY_SUPPORT && pushToHistory) {
history.pushState({}, document.title, location.pathname + href);
}
}
return !!match;
},
/**
* Attempt to scroll to the current location's hash.
*/
scrollToCurrent: function(e) {
if(this.scrollIfAnchor(window.location.hash) && e) {
e.preventDefault();
}
},
/**
* If the click event's target was an anchor, fix the scroll position.
*/
delegateAnchors: function(e) {
var elem = e.target;
if(this.scrollIfAnchor(elem.getAttribute('href'), true)) {
e.preventDefault();
}
}
};
$(document).ready($.proxy(anchorScrolls, 'init'));
})(window.document, window.history, window.location);
_Am我只是听滚动事件,然后检查是否一个元素在检视_是 - ?如果使用唯一的ID,页面将滚动到#ID的URL – mplungjan
它应该是'e.preventDefault()'。 –
@mplungjan它将是唯一的ID,但我需要的是在滚动内容时更改URL,而不是URL来滚动内容。 – cwal