dyn*_*mic 61 javascript jquery
有没有办法使用jQuery向下滚动到锚链接?
喜欢:
$(document).ready(function(){
$("#gotomyanchor").click(function(){
$.scrollSmoothTo($("#myanchor"));
});
});
Run Code Online (Sandbox Code Playgroud)
?
han*_*noo 121
我是这样做的:
var hashTagActive = "";
$(".scroll").on("click touchstart" , function (event) {
if(hashTagActive != this.hash) { //this will prevent if the user click several times the same link to freeze the scroll.
event.preventDefault();
//calculate destination place
var dest = 0;
if ($(this.hash).offset().top > $(document).height() - $(window).height()) {
dest = $(document).height() - $(window).height();
} else {
dest = $(this.hash).offset().top;
}
//go to destination
$('html,body').animate({
scrollTop: dest
}, 2000, 'swing');
hashTagActive = this.hash;
}
});
Run Code Online (Sandbox Code Playgroud)
然后你只需要像这样创建你的锚:
<a class="scroll" href="#destination1">Destination 1</a>
Run Code Online (Sandbox Code Playgroud)
你可以在我的网站上看到它.
这里也有一个演示:http://jsfiddle.net/YtJcL/
小智 47
我会使用CSS-Tricks.com的简单代码片段:
$(function() {
$('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;
}
}
});
});
Run Code Online (Sandbox Code Playgroud)
资料来源:http://css-tricks.com/snippets/jquery/smooth-scrolling/
aru*_*zca 38
到目前为止我见过的最佳解决方案: jQuery:Smooth Scrolling Internal Anchor Links
HTML:
<a href="#comments" class="scroll">Scroll to comments</a>
Run Code Online (Sandbox Code Playgroud)
脚本:
jQuery(document).ready(function($) {
$(".scroll").click(function(event){
event.preventDefault();
$('html,body').animate({scrollTop:$(this.hash).offset().top}, 500);
});
});
Run Code Online (Sandbox Code Playgroud)
Mar*_*iek 12
jQuery.scrollTo会做你想要的一切,甚至更多!
你可以传递各种不同的东西: