我已经添加顶部菜单作为与位置的div:固定。 它被放置在页面的顶部,因此它覆盖了部分内容。 我将布局向下移动,所以一般情况下都可以,但如果用户单击任何锚点链接,页面会滚动到锚点位于顶部的位置。但它被顶部菜单覆盖。 有没有办法来捕获锚事件和处理它与JavaScript(和jQuery,如果有必要)?如何在锚定导航后向下滚动页面?
1
A
回答
2
您可以使用这样的事情:
$('a').click(function(){
$('html').scrollTop($($(this).attr('href')).position().top + menu_height_offset)
})
要获得锚位置
$($(this).attr('href')).position().top
为了使偏移有关的固定菜单
menu_height_offset
为了使移动滚动
$('html').scrollTop()
1
你需要计算元素和sroll的偏移,offset of element - height of the navigationbar
- 位置:
$("a").on("click",function(){
// height of the navigation bar
var height= $("#nav").outerHeight();
// position of the referenced dom-element
var pos = $($(this).attr("href")).position().top;
// scroll to the element
$("body").scrollTop(pos - height);
// suppress default
return false;
})
0
/* START --- scroll till anchor */
(function($) {
$.fn.goTo = function() {
var top_menu_height=$('#div_menu_header').height() + 5 ;
//alert ('top_menu_height is:' + top_menu_height);
$('html, body').animate({
scrollTop: (-1)*top_menu_height + $(this).offset().top + 'px'
}, 500);
return this; // for chaining...
}
})(jQuery);
$(document).ready(function(){
var url = document.URL, idx = url.indexOf("#") ;
var hash = idx != -1 ? url.substring(idx+1) : "";
$(window).load(function(){
// Remove the # from the hash, as different browsers may or may not include it
var anchor_to_scroll_to = location.hash.replace('#','');
if (anchor_to_scroll_to != '') {
anchor_to_scroll_to = '#' + anchor_to_scroll_to ;
$(anchor_to_scroll_to).goTo();
}
});
});
/* STOP --- scroll till anchror */
相关问题
- 1. 滚动以固定导航栏锚定,导航滚动后修复导航
- 2. 网页设计:“向下滚动导航”
- 3. 向上和向下滚动页面JavaScript导航
- 4. 在页面上向下滚动时突出显示导航点
- 5. 当页面向下滚动时,导航栏移动到顶部
- 6. 如何使导航栏滚动页面?
- 7. 固定导航向下滚动时
- 8. 如何在向下滚动页面时使导航栏更改颜色?
- 9. 如何“向上”/“向下”动画页面导航?
- 10. 导航到页面的特定锚
- 11. Fancybox在页面滚动后向下打开页面
- 12. 如何使用固定导航栏重定向到页面并向下滚动到DIV
- 13. 淡出固定导航栏向下滚动时,淡入当页面顶部
- 14. Div向下滚动或滚动页面
- 15. 如何使用固定导航滚动到锚链接
- 16. 向下滚动时显示导航
- 17. 向下滚动时隐藏导航
- 18. 修复导航栏上向下滚动
- 19. jQuery Mobile:页面滚动后向下滚动
- 20. 如何在Ionic 3的页面导航上向上/向下滑动?
- 21. 引导,如何使用视口高度向下滚动后导航栏粘滞?
- 22. 导航到PHP页面,滚动
- 23. 页面滚动jQuery的导航
- 24. 保留Listview滚动页面导航
- 25. 如何通过向下滚动页面来向下滚动元素?
- 26. 如何在页面加载时向下滚动网页20px-50px?
- 27. 如何更改:在用户向下滚动时进行WordPress导航之后?
- 28. 如何在滚动时显示导航后面的div
- 29. 向下滚动页面从上到下
- 30. 如何防止JScrollBar滚动页面向下的特定事件?
向我们展示您到目前为止尝试过的方法。 – Lowkase
是的,代码请 –
改写你的问题,强调这部分 - *'捕获锚事件和处理它* –