javascript - 如何使用 jquery 模拟平滑滚动?

标签 javascript jquery html css

我正在尝试使用 jquery 模拟滚动,技术上我可以,但感觉真的很粗糙和笨拙。

JS:

$(document).ready(function(){
    $(this).bind('mousewheel', function(e){
        if(e.originalEvent.wheelDelta / 120 > 0) {
            movePage();
        }
    });
    var page1top = 0;
    function movePage() {
        page1top = page1top - 1;
        // page1 is the page that i want to move up when people scroll down.
        $('#page1').css('top': page1top + '%');
    }
});

HTML:

<div id='container'>
 <div id='page1'></div>
</div>

CSS:

#container {
    position: absolute;
    height: 100%;
    width: 100%;
    overflow: hidden;
    z-index: -100;
}

#page1 {
    width: 100%;
    height: 500%;
    z-index: 0;
}

我只是想知道如何才能顺利进行。请不要插件,谢谢。

最佳答案

您正在寻找 jquery animate。检查 fiddle 进行演示。

$('.to-target').click(function(){
  $('html, body').animate({
    scrollTop: $("#target").offset().top
  });
});
.box {
  width: 100vw;
  height: 100vh;
}

.long {
  background-color: aqua;  
}

.short {
  background-color: beige;
  height: 100vh;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="boxes">
  <div class="long box">
    <button class="to-target">Go to Target</button>
  </div>

  <div id="target" class="short box">

  </div>
</div>

关于javascript - 如何使用 jquery 模拟平滑滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38622257/

相关文章:

javascript - 访问导出默认值之外的数据字段 - Vue

javascript - jquery ajax 调用强制响应中的 XML 元素名称为小写

javascript - 使用 jquery 操作导航菜单

javascript - 为什么 jquery 正则表达式会产生奇怪的结果

javascript - svg #文档加载事件

javascript - jquery dlmenu动画不流畅

javascript - 使用 jQuery 显示和隐藏动态生成的表格元素

javascript - 如何让 Facebook Like Box "Dark Scheme"正常工作? (背景是透明的。)

php - 强制 JavaScript 按所需顺序执行

html - 为什么窗口最小化时div在另一个div之上