实现单页网站的导航
Implementing One page web site's navigation
我正在建立一个网页网站,其中有一个粘性的导航。我已经实现了几乎所有的东西,但是当用户通过鼠标滚轮或浏览器的滚动条滚动而不是使用导航时,我无法实现导航链接的高亮显示。我想这可以通过在最接近顶部的部分添加预样式类来实现?
我的第二个问题是如何停止滚动时,用户做一些事情,而页面正在滚动?
我的网站标记是
<nav class="columns col-12 main-nav">
<ul>
<li><a href="#page1" class="selected">a link</a></li>
<li><a href="#page2">another link</a></li>
<li><a href="#page3">cat</a></li>
<li><a href="#page4">dog</a></li>
</ul>
</nav>
<div class="container">
<section class="page" id="page1" data-stellar-background-ratio="1.75">
</section>
</div>
<div class="container">
<section class="page" id="page2" data-stellar-background-ratio="1">
</section>
</div>
这是用于滚动到对应id
的JQuery导航$(document).ready(function() {
$('a').click(function() {
var el = $(this).attr('href');
var elWrapped = $(el);
scrollToEle(elWrapped, 40);
return false;
});
function scrollToEle(element, navheight) {
var offset = element.offset();
var offsetTop = offset.top;
var totalScroll = offsetTop - navheight;
$('body,html').animate({
scrollTop : totalScroll,
}, 2000, 'easeInCirc');
}
});
这是当用户单击导航链接时突出显示的方式。(我认为这不是一个好方法来做这件事)
$(document).ready(function() {
$('nav a').click(function() {
$('nav .selected').removeClass('selected');
$(this).addClass('selected');
});
});
我真的为我糟糕的英语感到抱歉。我希望你能理解我的问题。我的问题是,当用户滚动页面而不使用导航时,如何突出显示链接。以及如何在页面滚动时,当用户点击页面时停止滚动
我从另一个帖子中找到了我的问题的答案。如果有人觉得这有用,就在这里
jQuery改变div #滚动到视图时的css导航
$(window).height() // returns the viewport height
$(document).height() // returns the height of the entire document
$(window).scrollTop() // returns the Y position of the document that is at the top of the viewport
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
$('#sidemenu').find('a').each(function(){
contentTop.push( $( $(this).attr('href') ).offset().top );
});
// Animate menu scroll to content
$('#sidemenu').find('a').click(function(){
var sel = this,
newTop = Math.min( contentTop[ $('#sidemenu 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;
});
// adjust side menu
$(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 ) ) ){
$('#sidemenu li')
.removeClass('selected')
.eq(i).addClass('selected');
}
});
});
});
这里
https://stackoverflow.com/posts/10144683/edit// Assign the HTML, Body as a variable...
var $viewport = $('html, body');
// Some event to trigger the scroll animation (with a nice ease - requires easing plugin )...
$('#element').click(function() {
$viewport.animate({
scrollTop: scrollTarget // set scrollTarget to your desired position
}, 1700, "easeOutQuint");
});
// Stop the animation if the user scrolls. Defaults on .stop() should be fine
$viewport.bind("scroll mousedown DOMMouseScroll mousewheel keyup", function(e){
if ( e.which > 0 || e.type === "mousedown" || e.type === "mousewheel"){
$viewport.stop().unbind('scroll mousedown DOMMouseScroll mousewheel keyup'); // This identifies the scroll as a user action, stops the animation, then unbinds the event straight after (optional)
}
});
相关文章:
- 带有url的单页网站导航
- Bootstrap一页导航Fluid网站最小化问题
- 使用 CSS 或 javascript/jQuery,我会使用哪种方法来使我的网站的导航栏看起来更 3d-ish
- 如何将jQuery文件添加到导航网站
- 我可以'我似乎没有得到我的网站'移动导航开始工作
- 如何在导航栏中突出显示单页网站的位置
- 为什么我的 2 个具有相同导航栏的网站页面显示不同
- 使用扩展/收缩部分/栏开发网站,同时滚动导航
- 单页网站粘性导航:更改内容元素的活动状态
- 网站中的单页体系结构或应用程序在 html 导航中使用 servlet 而不打开新页面
- 使用 Twitter 引导程序 3 的网站出现 IE11 导航问题
- 批准的在同一网站上的页面之间导航的方法
- 如果用户在我的响应网站中的纵向模式下单击切换按钮,则导航菜单将在横向模式下消失
- 如何在提供自定义HTTP标头的同时使用javascript导航到网站
- 导航网站在Chrome中处于全屏状态时会离开全屏
- 使用jquery平滑地更改每个导航菜单的网站背景图像
- 网站导航-PHP/HTML vs.Ajax vs.CSS/Javascript
- 尝试制作平板电脑网站的导航栏
- 使用主导航滚动网站问题
- 在服务器上的网站的不同页面之间导航