我正在寻找一种方法,以便在您单击页面上方或下方的本地 anchor 链接时包含幻灯片效果。
我想要一些你有这样链接的东西:
<a href="#nameofdivetc">link text, img etc.</a>
也许添加了一个类,这样您就知道您希望此链接成为一个滑动链接:
<a href="#nameofdivetc" class="sliding-link">link text, img etc.</a>
然后,如果单击此链接,页面将向上或向下滑动到所需位置(可能是 div、标题、页面顶部等)。
这是我之前的:
$(document).ready(function(){
$(".scroll").click(function(event){
//prevent the default action for the click event
event.preventDefault();
//get the full url - like mysitecom/index.htm#home
var full_url = this.href;
//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
var parts = full_url.split("#");
var trgt = parts[1];
//get the top offset of the target anchor
var target_offset = $("#"+trgt).offset();
var target_top = target_offset.top;
//goto that anchor by setting the body scroll top to anchor top
$('html, body').animate({scrollTop:target_top}, 1500, 'easeInSine');
});
});
最佳答案
描述
您可以使用 jQuery.offset()
和 jQuery.animate()
来做到这一点。
示例
function scrollToAnchor(aid){
var aTag = $("a[name='"+ aid +"']");
$('html,body').animate({scrollTop: aTag.offset().top},'slow');
}
scrollToAnchor('id3');
更多信息
关于jquery - 如何使用 jQuery 将页面向上或向下滚动到 anchor ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8579643/