Jquery 使用鼠标滚轮效果平滑滚动
Jquery smooth scroll with mousewheel effect
当我的页面首次加载时,我有一个全屏超大屏幕。当用户使用鼠标滚轮向下滚动时,我想使用 Jquery 动画效果将导航栏(在超大屏幕下方)带到页面顶部。
我已经有一个可以实现此目的的按钮,但我也想用鼠标滚轮来实现。
我怎样才能做到这一点?
谢谢
<!-- Jumobtron-->
<div class="jumbotron" style="height: 100vh;">
<a href="#mainNavbar">
<button type="button" class="btn" id="btnToNav">To Navbar</button>
</a>
</div>
<!-- Navbar -->
<nav class="navbar sticky-top" id="mainNavbar">
<div class="container">
<a asp-controller="Home" asp-action="Index" class="navbar-brand"> Brand </a>
</div>
</div>
</nav>
Jquery:
$(document).ready(function() {
$('#btnToNav').on('click', function(event) {
event.preventDefault();
$('html, body').animate({
scrollTop: $("#mainNavbar").offset().top
}, 1000);
});
});
您可以使用
mousewheel
或 DOMMouseScroll
如果您想 运行 在 Firefox 中也使用该功能,您应该同时使用它们,因为 Firefox 没有 mousewheel
,但它们有 DOMMouseScroll
$(document).ready(function() {
$('#btnToNav').on('click', function(event){
event.preventDefault();
$('html, body').animate({
scrollTop: $("#mainNavbar").offset().top
}, 1000);
});
$('html, body').on('mousewheel', function(e){
e.preventDefault();
var delta = event.wheelDelta;
if(delta < 0){
// scroll from top to bottom
$('html, body').animate({
scrollTop: $("#brand").offset().top
}, 1000);
}else{
// scroll from bottom to top
$('html, body').animate({
scrollTop: $("#btnToNav").offset().top
}, 1000);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Jumobtron-->
<div class="jumbotron" style="height: 100vh;">
<a href="#mainNavbar">
<button type="button" class="btn" id="btnToNav">To Navbar</button>
</a>
</div>
<!-- Navbar -->
<nav class="navbar sticky-top" id="mainNavbar">
<div class="container">
<a id="brand" asp-controller="Home" asp-action="Index" class="navbar-brand"> Brand </a>
</div>
</nav>
当我的页面首次加载时,我有一个全屏超大屏幕。当用户使用鼠标滚轮向下滚动时,我想使用 Jquery 动画效果将导航栏(在超大屏幕下方)带到页面顶部。 我已经有一个可以实现此目的的按钮,但我也想用鼠标滚轮来实现。
我怎样才能做到这一点? 谢谢
<!-- Jumobtron-->
<div class="jumbotron" style="height: 100vh;">
<a href="#mainNavbar">
<button type="button" class="btn" id="btnToNav">To Navbar</button>
</a>
</div>
<!-- Navbar -->
<nav class="navbar sticky-top" id="mainNavbar">
<div class="container">
<a asp-controller="Home" asp-action="Index" class="navbar-brand"> Brand </a>
</div>
</div>
</nav>
Jquery:
$(document).ready(function() {
$('#btnToNav').on('click', function(event) {
event.preventDefault();
$('html, body').animate({
scrollTop: $("#mainNavbar").offset().top
}, 1000);
});
});
您可以使用
mousewheel
或 DOMMouseScroll
如果您想 运行 在 Firefox 中也使用该功能,您应该同时使用它们,因为 Firefox 没有 mousewheel
,但它们有 DOMMouseScroll
$(document).ready(function() {
$('#btnToNav').on('click', function(event){
event.preventDefault();
$('html, body').animate({
scrollTop: $("#mainNavbar").offset().top
}, 1000);
});
$('html, body').on('mousewheel', function(e){
e.preventDefault();
var delta = event.wheelDelta;
if(delta < 0){
// scroll from top to bottom
$('html, body').animate({
scrollTop: $("#brand").offset().top
}, 1000);
}else{
// scroll from bottom to top
$('html, body').animate({
scrollTop: $("#btnToNav").offset().top
}, 1000);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Jumobtron-->
<div class="jumbotron" style="height: 100vh;">
<a href="#mainNavbar">
<button type="button" class="btn" id="btnToNav">To Navbar</button>
</a>
</div>
<!-- Navbar -->
<nav class="navbar sticky-top" id="mainNavbar">
<div class="container">
<a id="brand" asp-controller="Home" asp-action="Index" class="navbar-brand"> Brand </a>
</div>
</nav>