gpt4 book ai didi

html - 如何使用鼠标滚轮跳转到 html 中的下一个或上一个 # 部分

转载 作者:太空狗 更新时间:2023-10-29 16:35:14 24 4
gpt4 key购买 nike

我有一个带有顶部导航菜单的站点:

<nav id="menu">
<ul id="menu-nav">
<li class="current"><a href="#home-slider">Home</a></li>
<li><a href="#Cakes">Cakes</a></li>
<li><a href="#Candy">Candy</a></li>
<li><a href="#Marshmellow">Marshmellow</a></li>
<li><a href="#Honey">Honey</a></li>
</ul>
</nav>

我所有的部分都是:

<div id="Cakes" class="page">
<div class="container">
This is sweet
</div>
</div>

如何使用鼠标滚轮直接跳到下一节或上一节?

目前,鼠标滚轮将正常滚动页面。但我希望它只需转动一个轮子即可跳转到每个部分,而无需滚动整个页面。

最佳答案

如果没有插件,我们需要做的就是使用 mousewheel 事件捕获鼠标滚轮 -在 Firefox 上我们使用 DOMMouseScroll 代替 - 并且取决于关于事件的 originalEvent.wheelDelta 的值 -再次在 Firefox 中它是 originalEvent.detail,感谢 Firefox - 如果这个值是 positive 则用户向上滚动,如果negative 则方向向下。

JS Fiddle 1

jQuery (1) :

//initialize
var winHeight = $(window).height(),
pages = $('.page'),
navLinks = $('#menu-nav a'),
currentPage = 0;

$('html, body').animate({ scrollTop: 0}, 0);

// listen to the mousewheel scroll
$(window).on('mousewheel DOMMouseScroll', function(e){

//by default set the direction to DOWN
var direction = 'down',
$th = $(this),
// depending on the currentPage value we determine the page offset
currentPageOffset = currentPage * winHeight;

// if the value of these properties of the even is positive then the direction is UP
if (e.originalEvent.wheelDelta > 0 || e.originalEvent.detail < 0) {
direction = 'up';
}

// if the direction is DOWN and the currentPage increasing won't exceed
// the number of PAGES divs, then we scroll downward and increase the value
// of currentPage for further calculations.
if(direction == 'down' && currentPage <= pages.length - 2){
$th.scrollTop(currentPageOffset + winHeight);
currentPage++;
} else if(direction == 'up' && currentPage >= 0) {
// else scroll up and decrease the value of currentPage IF the direction
// is UP and we're not on the very first slide
$th.scrollTop(currentPageOffset - winHeight);
currentPage--;
}
});

// as final step we need to update the value of currenPage upon the clicking of the
// navbar links to insure having correct value of currentPage
navLinks.each(function(index){
$(this).on('click', function(){
navLinks.parent().removeClass('current');
$(this).parent().addClass('current');
currentPage = index;
});
});

(1) 更新

如果您不想使用 jQuery,下面是 javascript 代码,与上面的功能相同,但是这在 IE8 和以下版本中不起作用:

JS Fiddle 2

纯Javascript:

//initialize
var winHeight = window.innerHeight,
pages = document.getElementsByClassName('page'),
navLinks = document.querySelectorAll('#menu-nav a'),
currentPage = 0;

window.addEventListener('mousewheel', function(e) {
scrollPages(e.wheelDelta);
});
window.addEventListener('DOMMouseScroll', function(e) {
scrollPages(-1 * e.detail);
});

function scrollPages(delta) {
var direction = (delta > 0) ? 'up' : 'down',
currentPageOffset = currentPage * winHeight;

if (direction == 'down' && currentPage <= pages.length - 2) {
window.scrollTo(0, currentPageOffset + winHeight);
currentPage++;
} else if (direction == 'up' && currentPage > 0) {
window.scrollTo(0, currentPageOffset - winHeight);
currentPage--;
}
}

for (var i = 0; i < navLinks.length; i++) {
navLinks[i].addEventListener('click', updateNav(i));
}

function updateNav(i) {
return function() {
for (var j = 0; j < navLinks.length; j++) {
navLinks[j].parentNode.classList.remove('current');
}
navLinks[i].parentNode.classList.add('current');
currentPage = i;
}
}

关于html - 如何使用鼠标滚轮跳转到 html 中的下一个或上一个 # 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35299646/

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