一个小巧的jQuery平滑滚动插件,允许您将页面垂直平滑地滚动到文档的任何位置。
这个插件最棒的地方是,它允许您将平滑滚动行为绑定到任何事件,如触摸点击和鼠标点击,而不仅仅是锚链接。
1.å è½½ä¸»èæ¬平滑滚动.jså¨jQueryä¹åã
<script src="/path/to/cdn/jquery.slim.min.js"></script> <script src="/path/to/SmoothScrolling.js"></script>
2.将页面滚动到特定位置:
smoothScrolling.scrollToTargetY(targetYInPixels);
3.将平滑滚动行为绑定到任何事件,如下所示:
// for Safari
let allowScrolling = true;
$(window).on("touchmove", function(e) {
// for Safari
allowScrolling = false;
});
$(window).on("click touchend", function(e) {
if (e.target.tagName != "A" && allowScrolling) {
// for Safari
event.stopPropagation();
event.preventDefault();
const maxHeight = $(document).height();
const randomPercentage = Math.random();
const randomHeight = randomPercentage * maxHeight;
smoothScrolling.scrollToTargetY(randomHeight);
}
else
// for Safari
allowScrolling = true;
});