【问题标题】:On click scroll to anchor point单击时滚动到锚点
【发布时间】:2013-11-13 12:25:52
【问题描述】:
【问题讨论】:
标签:
jquery
html
css
scroll
【解决方案1】:
给你
html
<a href="#red">red</a> <a href="#blue">blue</a>
<div class="section red" id="red"></div>
<div class="section blue" id="blue"></div>
CSS
.section {
height:550px;
}
.red {
background-color:#F00;
}
.blue {
background-color:#036;
}
JS
$('a[href*=#]:not([href=#])').click(function () {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') || location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
演示
http://jsfiddle.net/cancerian73/k2j5X/