我正在构建一个页面网站,其上有一个粘性导航。我已经实现了几乎所有内容,但是当用户通过鼠标滚轮或浏览器的滚动条滚动而不是使用导航时,我无法实现导航链接的突出显示。我想这可以通过在最接近顶部的部分添加预先设置的类来实现吗?
我的第二个问题是当用户在页面滚动时做某事时如何停止滚动?
我的网站标记
<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>
这是我的JQuery导航滚动到相应的id
$(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');
});
});
对于我可怕的英语,我真的很抱歉。我希望你理解我的问题。我的问题是当用户在不使用导航的情况下滚动页面时如何突出显示链接。以及当用户在页面滚动时点击页面时如何停止滚动。
答案 0 :(得分:1)
我从另一篇文章中找到了我的问题的答案。在这里,如果有人发现这个有用的
jQuery changing css on navigation when div # scrolls into view
$(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)
}
});