在鼠标滚轮上平滑滚动
Smooth scrolling on mouse wheel?
与普通的鼠标滚动速度不同,我想让它在现代浏览器中更慢、更流畅、更一致。
我已经尝试过使用一些插件,比如NiceScroll(https://nicescroll.areaaperta.com/)。
但在安装后,由于某种原因,它会出现溢出:隐藏;在我的内容上,之后无法滚动到任何位置。我不需要任何自定义设计的滚动条。我只需要在使用鼠标滚动或垂直滚动条时滚动更平滑,如下所示:
http://pervolo.com/
有人能告诉我这件事吗?我计划使用skrollr插件(https://github.com/Prinzhorn/skrollr)以及平滑滚动。
这可能会让你开始:
$(window).on('mousewheel DOMMouseScroll', function(e) {
var dir,
amt = 100;
e.preventDefault();
if(e.type === 'mousewheel') {
dir = e.originalEvent.wheelDelta > 0 ? '-=' : '+=';
}
else {
dir = e.originalEvent.detail < 0 ? '-=' : '+=';
}
$('html, body').stop().animate({
scrollTop: dir + amt
},500, 'linear');
})
DOMMouseScroll
和e.originalEvent.detail
是Firefox所必需的。将amt
更改为您想要的滚动距离,将500
更改为您需要的滚动速度。
Fiddle:http://jsfiddle.net/utcsdyp1/1
我成功地使鼠标滚轮滚动看起来像黄油一样光滑!复制粘贴以下片段,然后自己尝试。
let easeInOutQuint = t => t<.5 ? 16*t*t*t*t*t : 1+16*(--t)*t*t*t*t; // Easing function found at https://gist.github.com/gre/1650294
// With this attempt I tried to make the scroll by mouse wheel look smooth
let delay = ms => new Promise(res => setTimeout(res, ms));
let dy = 0;
window.addEventListener("wheel", async e => {
// Prevent the default way to scroll the page
e.preventDefault();
dy += e.deltaY;
let _dy = dy; // Store the value of "dy"
await delay(150); // Wait for .15s
// If the value hasn't changed during the delay, then scroll to "start + dy"
if (_dy === dy) {
let start = window.scrollY || window.pageYOffset;
customScrollTo(start + dy, 600, easeInOutQuint);
dy = 0;
}
}, { passive: false });
function customScrollTo(to, duration, easingFunction) {
let start = window.scrollY || window.pageYOffset;
let time = Date.now();
let timeElapsed = 0;
let speed = (to - start) / duration;
(function move() {
if (timeElapsed > duration) {
return;
}
timeElapsed = Date.now() - time;
// Get the displacement of "y"
let dy = speed * timeElapsed;
let y = start + dy;
// Map "y" into a range from 0 to 1
let _y = (y - start) / (to - start);
// Fit "_y" into a curve given by "easingFunction"
_y = easingFunction(_y);
// Expand "_y" into the original range
y = start + (to - start) * _y;
window.scrollTo(0, y);
window.requestAnimationFrame(move);
})();
}
当用户使用$(element).on( "scroll", handler )
滚动时,您可以捕捉到一个操作,然后,例如,使用以下代码
$('html, body').animate({
scrollTop: $("#target-element").offset().top
}, 1000);
使用jQuery 滚动到某个元素
相关文章:
- Safari(Mac OS)上的jQuery平滑滚动问题
- jquery平滑滚动问题
- javascript mootools平滑滚动'x'像素数量
- jQuery平滑滚动
- jQuery平滑滚动未滚动
- 更新Wordpress和我的平滑滚动停止工作
- 具有子菜单和平滑滚动的粘性导航
- 平滑滚动&懒惰
- 平滑滚动时更改 ID 的颜色
- 使用我的基于标签的平滑滚动脚本而不带“a”的解决方案
- 调整平滑滚动代码
- 使用 window.scrollBy) 平滑滚动
- 平滑滚动代码
- 如何在ng视图中进行页面渲染后的平滑滚动
- 如何使用requestAnimationFrame实现平滑滚动
- 如何由于浏览器的平滑滚动而忽略大量滚动事件
- 使用普通JavaScript平滑滚动到元素
- 平滑滚动并突出显示导航问题
- 在函数goToNext上平滑滚动+偏移
- 如何在HTML5页面上启用平滑滚动