更改自动滚动的 URL(推送状态?
Change URL for autoscroll (pushstate?)
我目前正在使用我发现的这段代码来自动滚动到锚点:
$(function() {
$('nav a[href*=#]').bind('click',function(event){
var $anchor = $(this);
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top - 100
}, 1000);
event.preventDefault();
});
});
我可以添加哪些内容以确保 #anchor 在单击时添加到 URL 中?
我已经看到这个用过,但我不确定如何添加它。
function() {
if(history.pushState) {
history.pushState(null, null, target);
}
else {
location.hash = target;
}
});
非常感谢!!
动画完成后运行它:(这里有很好的演示)
$(function () {
$('nav a[href*=#]').on('click', function (event) {
var $anchor = $(this),
name = $anchor.attr("href").match(/#(.+)/i)[1];
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top - 100
}, 1000, function () {
if (history.pushState) {
history.pushState(null, null, "#" + name);
} else {
location.hash = name;
}
});
event.preventDefault();
});
});
工作演示:http://jsfiddle.net/DerekL/uPS9W/show
相关文章:
- 使用应用程序状态js和视图在angular js url上传递两个参数
- 使url匹配'/''/索引'和'/index.html'到单一状态
- 如何同步Redux状态和url哈希标记参数
- 限制与AngularJS状态匹配的动态URL参数的最佳方式
- 在AngularJS ui路由器中,状态已更改,url已更改,但模板url未更改
- URL中包含特殊字符的HTTP状态代码
- Angular ui路由器,通过url/url变量保存状态
- 将状态的 URL 数据获取到模板中
- Angular ui路由器单元测试(状态为url)
- AngularUI路由器-无法通过URL访问状态
- Ember.js URL如何表示所有可能的路由器状态
- 可以't使用URL导航到ui路由器状态
- 使用url字符串更改js/jquery的状态
- 当鼠标悬停在链接上时,从 href URL 中隐藏状态栏值
- 更改自动滚动的 URL(推送状态?
- 获取在 <脚本 src >标记中添加的请求 URL 的状态
- 嵌套状态 URL 在 angular-ui-router 中不正确
- 使用ui路由器AngularJS 1.x获取给定的状态url
- 在Angularjs中,ui-router访问状态url开头的参数
- Angularjs的状态url依赖于数值url