【发布时间】:2014-10-31 21:22:08
【问题描述】:
我有一个相当复杂的问题,我自己似乎无法回答。
在我的页面顶部,我有几个可以平滑滚动到文章的锚点:
我想通过旋转箭头告诉访问者他们在页面上的位置。这个箭头应该指向它所代表的文章。在下一张图片中,您可以看到我想如何实现它。在这里,用户已经滚动过了锚点 1(这是一篇动态高度的文章),现在正在阅读锚点 2 的文章:
当访问者在文章之外,但锚点3文章仍在滚动位置下方时,应该是这样的:
这给我带来了一些问题:
- 是否可以编写 jQuery 脚本以便动态添加锚点?
- 如何在不添加额外库的情况下添加旋转动画? (我现在正在使用 jquery.transit )
当前代码:(从另一个帖子得到这个:jsfiddle.net/m2zQE)
var topRange = 200, // measure from the top of the viewport to X pixels down
edgeMargin = 20, // margin above the top or margin from the end of the page
animationTime = 1200, // time in milliseconds
contentTop = [];
$(document).ready(function(){
// Stop animated scroll if the user does something
$('html,body').bind('scroll mousedown DOMMouseScroll mousewheel keyup', function(e){
if ( e.which > 0 || e.type == 'mousedown' || e.type == 'mousewheel' ){
$('html,body').stop();
}
})
// Set up content an array of locations
$('#navTopBar').find('a').each(function(){
contentTop.push( $( $(this).attr('href') ).offset().top );
})
// Animate menu scroll to content
$('#navTopBar').find('a').click(function(){
var sel = this,
newTop = Math.min( contentTop[ $('#navTopBar a').index( $(this) ) ], $(document).height() - $(window).height() ); // get content top or top position if at the document bottom
$('html,body').stop().animate({ 'scrollTop' : newTop }, animationTime, function(){
window.location.hash = $(sel).attr('href');
});
return false;
})
// rotate arrow
$(window).scroll(function(){
var winTop = $(window).scrollTop(),
bodyHt = $(document).height(),
vpHt = $(window).height() + edgeMargin; // viewport height + margin
$.each( contentTop, function(i,loc){
if ( ( loc > winTop - edgeMargin && ( loc < winTop + topRange || ( winTop + vpHt ) >= bodyHt ) ) ){
$('#navTopBar .anchor img')
.removeClass('open')
.eq(i).addClass('open');
}
})
})
})
提前致谢!
【问题讨论】:
-
你在哪里定义了edgeMargin?我想你被访问了:jsfiddle.net/m2zQE ?
-
是的,完全正确。这与我试图实现的目标非常接近。