【发布时间】:2010-11-16 19:17:03
【问题描述】:
有没有办法使用 jQuery 向下滚动到锚链接?
喜欢:
$(document).ready(function(){
$("#gotomyanchor").click(function(){
$.scrollSmoothTo($("#myanchor"));
});
});
?
【问题讨论】:
标签: javascript jquery
有没有办法使用 jQuery 向下滚动到锚链接?
喜欢:
$(document).ready(function(){
$("#gotomyanchor").click(function(){
$.scrollSmoothTo($("#myanchor"));
});
});
?
【问题讨论】:
标签: javascript jquery
到目前为止我见过的最佳解决方案: jQuery: Smooth Scrolling Internal Anchor Links
HTML:
<a href="#comments" class="scroll">Scroll to comments</a>
脚本:
jQuery(document).ready(function($) {
$(".scroll").click(function(event){
event.preventDefault();
$('html,body').animate({scrollTop:$(this.hash).offset().top}, 500);
});
});
【讨论】:
作品
$('a[href*=#]').each(function () {
$(this).attr('href', $(this).attr('href').replace('#', '#_'));
$(this).on( "click", function() {
var hashname = $(this).attr('href').replace('#_', '');
if($(this).attr('href') == "#_") {
$('html, body').animate({ scrollTop: 0 }, 300);
}
else {
var target = $('a[name="' + hashname + '"], #' + hashname),
targetOffset = target.offset().top;
if(targetOffset >= 1) {
$('html, body').animate({ scrollTop: targetOffset-60 }, 300);
}
}
});
});
【讨论】:
我会使用来自 CSS-Tricks.com 的简单代码 sn-p:
$(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;
}
}
});
});
【讨论】:
$('a[href*="#"]:not([href="#"])')
这是我的做法:
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;
}
});
那么你只需要像这样创建你的锚:
<a class="scroll" href="#destination1">Destination 1</a>
你可以在我的website上看到它。
此处也提供演示:http://jsfiddle.net/YtJcL/
【讨论】:
scrollTop: veryHighNumber 只会将您带到页面底部。
id 而非锚name 为目标元素。在我更仔细地查看您的 Fiddle 之前,我假设是后者并且对此感到非常沮丧。干杯!
使用hanoo's script我创建了一个jQuery函数:
$.fn.scrollIntoView = function(duration, easing) {
var dest = 0;
if (this.offset().top > $(document).height() - $(window).height()) {
dest = $(document).height() - $(window).height();
} else {
dest = this.offset().top;
}
$('html,body').animate({
scrollTop: dest
}, duration, easing);
return this;
};
用法:
$('#myelement').scrollIntoView();
持续时间和缓动的默认值为 400 毫秒和“摆动”。
【讨论】:
jQuery.scrollTo 会做你想做的一切,甚至更多!
你可以给它传递各种不同的东西:
【讨论】:
这是我用来快速将 jQuery scrollTo 绑定到所有锚链接的代码:
// Smooth scroll
$('a[href*=#]').click(function () {
var hash = $(this).attr('href');
hash = hash.slice(hash.indexOf('#') + 1);
$.scrollTo(hash == 'top' ? 0 : 'a[name='+hash+']', 500);
window.location.hash = '#' + hash;
return false;
});
【讨论】:
我想要一个适用于 <a href="#my-id"> 和 <a href="/page#my-id"> 的版本
<script>
$('a[href*=#]:not([href=#])').on('click', function (event) {
event.preventDefault();
var element = $(this.hash);
$('html,body').animate({ scrollTop: element.offset().top },'normal', 'swing');
});
</script>
【讨论】:
试试这个。这是我修改的 CSS 技巧中的代码,它非常简单,并且可以进行水平和垂直滚动。需要 jQuery。这是demo
$(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-10, scrollLeft:target.offset().left-10
}, 1000);
return false;
}
}
});
});
【讨论】:
我在我的网站中使用了这个:
$(document).ready(function(){
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 1200, 'swing', function () {
window.location.hash = target;
});
});
});
您可以更改滚动速度,更改我默认使用的“1200”,它在大多数浏览器上都运行良好。
将代码放在页面的<head> </head> 标记之间后,您需要在<body> 标记中创建内部链接:
<a href="#home">Go to Home</a>
希望对你有帮助!
Ps:别忘了打电话:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
【讨论】:
我在http://plugins.jquery.com/smooth-scroll/ 使用了插件Smooth Scroll。有了这个插件,您只需要包含一个指向 jQuery 和插件代码的链接:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="javascript/smoothscroll.js"></script>
(链接需要有 smoothScroll 类才能工作)。
Smooth Scroll的另一个特点是URL中不显示ancor名称!
【讨论】:
我讨厌在我的代码中添加以函数命名的类,所以我把它放在一起。如果我停止使用平滑滚动,我会觉得应该检查我的代码,并删除所有 class="scroll" 的东西。使用这种技术,我可以注释掉 5 行 JS,整个网站更新。 :)
<a href="/about">Smooth</a><!-- will never trigger the function -->
<a href="#contact">Smooth</a><!-- but he will -->
...
...
<div id="contact">...</div>
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
// Smooth scrolling to element IDs
$('a[href^=#]:not([href=#])').on('click', function () {
var element = $($(this).attr('href'));
$('html,body').animate({ scrollTop: element.offset().top },'normal', 'swing');
return false;
});
</script>
要求:
1.<a>元素必须有一个以#开头的href属性,并且不能只是#
2. 页面上具有匹配id 属性的元素
它的作用:
1.函数使用href值创建anchorID对象
- 在示例中,它是$('#contact'),/about 以/ 开头
2.HTML和BODY动画到anchorID的顶部偏移
- speed = 'normal' ('fast','slow', 毫秒, )
- 缓动 = 'swing'('线性'等......谷歌缓动)
3. return false -- 防止浏览器在 URL 中显示哈希值
- 没有它,脚本也能工作,但它不像“顺利”。
【讨论】: