gpt4 book ai didi

javascript - 实现单页网站的导航

转载 作者:行者123 更新时间:2023-11-28 02:14:23 25 4
gpt4 key购买 nike

我正在构建一个页面网站,上面有一个粘性导航。我已经实现了几乎所有内容,但是当用户通过鼠标滚轮或浏览器滚动条滚动而不是使用导航时,我无法实现导航链接的突出显示。我想这可以通过向最接近顶部的部分添加预样式类来实现?

我的第二个问题是当用户在页面滚动时执行某些操作时如何停止滚动?

我的网站标记是

<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');
});



});

我对我糟糕的英语感到非常抱歉。我希望你能理解我的问题。我的问题是当用户滚动页面而不使用导航时如何突出显示链接。以及当页面滚动时用户点击页面时如何停止滚动。

最佳答案

我从另一篇文章中找到了我的问题的答案。如果有人觉得这有用的话,这就是

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)
}
});

关于javascript - 实现单页网站的导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16626312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com