首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有鼠标轮效果的Jquery平滑滚动

问具有鼠标轮效果的Jquery平滑滚动
EN

Stack Overflow用户
提问于 2017-12-03 05:52:55
回答 1查看 392关注 0票数 0

当我的页面第一次加载时,我有一个全屏巨无霸。当用户使用鼠标轮向下滚动时,我希望使用Jquery动画效果将导航条(巨无霸之下)带到页面顶部。我已经有一个按钮,可以实现这一点,但我想做它的鼠标轮也。

我怎样才能做到这一点?谢谢

代码语言:javascript
复制
    <!-- 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:

代码语言:javascript
复制
$(document).ready(function() {
    $('#btnToNav').on('click', function(event) { 
        event.preventDefault();
        $('html, body').animate({
            scrollTop: $("#mainNavbar").offset().top
        }, 1000);
    });    
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-03 09:54:28

您可以使用

mousewheel或DOMMouseScroll

如果你也想在火狐中运行这个函数,你应该同时使用它们,因为火狐没有mousewheel,但是它们有DOMMouseScroll

​

代码语言:javascript
复制
$(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);
      }
    });
});
代码语言:javascript
复制
<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>

​

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47615685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档