[英]Smooth scrolling on mouse wheel?
Instead of the normal mouse scroll speed, I'd like to make it slower and smoother and as consistent as it can in modern browsers.我想让它变得更慢、更流畅,并尽可能在现代浏览器中保持一致,而不是正常的鼠标滚动速度。
I have already tried using a few plugins like NiceScroll ( https://nicescroll.areaaperta.com/ ).我已经尝试过使用一些插件,比如 NiceScroll ( https://nicescroll.areaaperta.com/ )。
But after installing it, for some reason it puts an overflow: hidden;但是安装后,由于某种原因,它会出现溢出:隐藏; on my content and can't scroll anywhere after.在我的内容上,之后无法滚动到任何地方。 I don't need any custom-designed scrollbars.我不需要任何定制设计的滚动条。 I just need the scrolling to be smoother when using the mouse scroll or vertical scroll bar like this:当使用鼠标滚动或垂直滚动条时,我只需要滚动更平滑:
http://pervolo.com/ http://pervolo.com/
Would anyone please enlighten me about this?有没有人请教我这个? I plan to use the skrollr plugin ( https://github.com/Prinzhorn/skrollr ) along with the smooth scrolling.我计划使用 skrollr 插件 ( https://github.com/Prinzhorn/skrollr ) 以及平滑滚动。
This may get you going:这可能会让你继续:
$(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
and e.originalEvent.detail
are required for Firefox. Firefox 需要DOMMouseScroll
和e.originalEvent.detail
。 Change amt
to be your desired scroll distance, and change 500
to be your desired scroll speed.将amt
更改为所需的滚动距离,将500
更改为所需的滚动速度。
Fiddle: http://jsfiddle.net/utcsdyp1/1小提琴: http : //jsfiddle.net/utcsdyp1/1
You can catch an action when user scrolls using $(element).on( "scroll", handler )
, then, using, for example this code您可以在用户使用$(element).on( "scroll", handler )
时捕获操作,然后使用例如此代码
$('html, body').animate({
scrollTop: $("#target-element").offset().top
}, 1000);
scroll to some element using jQuery使用 jQuery 滚动到某个元素
I succeeded in making the scroll by mouse wheel look smooth as butter!我成功地使鼠标滚轮的滚动看起来像黄油一样光滑! Copy-paste the following snippet and try yourself.复制粘贴以下代码片段并自己尝试。
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);
})();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.