简体   繁体   English

Javascript - 使用鼠标滚轮平滑视差滚动

[英]Javascript - Smooth parallax scrolling with mouse wheel

I have a page where I'm applying a parallax effect.我有一个页面,我正在应用视差效果。 This is accomplished using translate3d.这是使用 translate3d 完成的。 Now, while this works well, I'm wondering how I can override the default "steps" when scrolling with the mouse wheel?现在,虽然这很有效,但我想知道如何在使用鼠标滚轮滚动时覆盖默认的“步骤”?

If I scroll with the scrollbars, everything is fine.如果我用滚动条滚动,一切都很好。 But with the mouse wheel, it's all jumpy.但是使用鼠标滚轮,一切都变得神经质。

I'm doing this in a pretty straight forward way:我正在以一种非常直接的方式执行此操作:

    var prefix = Modernizr.prefixed('transform');
    $window.on('scroll', function(){
        var scroll_top = $window.scrollTop();
        if(scroll_top < forside_infographics_offset){
            $_('#slider').css(prefix , "translate3d(0,"+(scroll_top/3)+"px,0)");
        }

    });

Now, I've seen this site where the scrolling is super smooth, also with a mouse wheel with steps on it.现在,我已经看到这个网站的滚动非常流畅,还有一个带有步骤的鼠标滚轮。 I've tried to look at the code, and he's using requestAnimationFrame is seems, but how he accomplish this excact scrolling effect, I'm not sure.我试图查看代码,他似乎正在使用requestAnimationFrame ,但是他如何实现这种精确的滚动效果,我不确定。

http://cirkateater.no/ http://cirkateater.no/

Any ideas?有什么想法吗?

After doing a lot of research, I found a pretty easy solution :) http://bassta.bg/demos/smooth-page-scroll/经过大量研究,我找到了一个非常简单的解决方案:) http://bassta.bg/demos/smooth-page-scroll/

Interestingly enough, I didn't have to alter my existing code at all.有趣的是,我根本不需要更改我现有的代码。 This overrides the default scroll behaviour, while leaving the event open for me to use like I would normally do.这会覆盖默认的滚动行为,同时让我像往常一样使用该事件。

EDIT: This is a really bad idea.编辑:这是一个非常糟糕的主意。 Never ever hijack and override expected behavior.永远不要劫持和覆盖预期的行为。 I guess I was overly fascinated with animations back then and overdid everything.我想我当时对动画过于着迷并且把一切都做得太过分了。 Thankfully we all learn and expand our perceptions of good UX principles :)值得庆幸的是,我们都学习并扩展了我们对良好用户体验原则的看法:)

Scrolling using the mouse wheel requires special handling.使用鼠标滚轮滚动需要特殊处理。 Reason being each mouse wheel scroll doesn't scroll the content by a certain amount of pixels.原因是每个鼠标滚轮滚动不会将内容滚动一定数量的像素。 Each scroll causes your page to jump and then each jump results in the "jumpy" jittery animation as the background image is trying to position itself at these jumps.每次滚动都会导致您的页面跳跃,然后每次跳跃都会导致“跳跃”抖动动画,因为背景图像试图将自己定位在这些跳跃处。

Using a library will solve the problem most of the time, but it is also worth understanding what problems it is trying to solve under the hood.大多数情况下,使用库可以解决问题,但也值得了解它试图解决的问题。

Just for reference sakes, the mouse events are mousewheel and DOMMouseScroll仅供参考缘故,鼠标事件 鼠标滚轮DOMMouseScroll

This plugin for Chrome provides the functionality necessary for this.这个Chrome 插件提供了必要的功能。 Someone created a gist with a minified version of it .有人用它的缩小版本创建了一个要点。 It's from a pretty old version, but I think that's fine because, as I've checked, the latest version of the plugin adds too much stuff.它来自一个相当旧的版本,但我认为这很好,因为正如我所检查的,该插件的最新版本添加了太多东西。

A couple things with that gist though:不过,有几件事与该要点有关:

  • It checks if the browser is Chrome before initiating.它会在启动前检查浏览器是否为 Chrome。
  • It initiates automatically.它会自动启动。
  • It uses jQuery.它使用jQuery。

So I let myself create a version that addresses those points.所以我让自己创建一个版本来解决这些问题。 Just add the script and call SmoothScroll.init() to start.只需添加脚本并调用SmoothScroll.init()即可启动。

Edit: While testing I figured out this has a significant bug.编辑:在测试时我发现这有一个重大错误。 While my version behaves (in my opinion) tremendously better than the original code, it unfortunately does not account for scrolling by other means (scroll bar/middle click and drag).虽然我的版本(在我看来)比原始代码表现得更好,但遗憾的是它没有考虑通过其他方式(滚动条/中键单击和拖动)滚动。 Scrolling by one of these methods and then scrolling with the mouse wheel causes it to revert to wherever scroll location you were at when you last scrolled the mousewheel.通过这些方法之一滚动,然后使用鼠标滚轮滚动会使其恢复到您上次滚动鼠标滚轮时所在的任何滚动位置。 I'll update when I develop a solution to this.当我开发解决方案时,我会更新。

Kenny's referenced solution was a fine approach, but it's functionality drove me crazy. Kenny 引用的解决方案是一个很好的方法,但它的功能让我发疯。 If you scroll the wheel quickly it wouldn't scroll much faster.如果你快速滚动滚轮,它不会滚动得更快。

I improved it such that you scroll a given distance per click regardless of mouse wheel spin speed.我对其进行了改进,无论鼠标滚轮的旋转速度如何,每次单击都可以滚动给定的距离。

The reason his answer did not is because if you scroll the wheel a second time before the first animation is complete the new scroll to height is only the current scroll height plus however much it scrolls per wheel click.他的回答没有的原因是因为如果您在第一个动画完成之前第二次滚动滚轮,则新的滚动高度只是当前的滚动高度加上每次单击滚轮滚动的次数。 (So if scroll time is .5 seconds and you scroll a second time after .25 seconds then it will scroll 1.5 times the wheel scroll distance instead of 2 times that distance) (因此,如果滚动时间为 0.5 秒,并且您在 0.25 秒后再次滚动,则它将滚动滚轮滚动距离的 1.5 倍,而不是该距离的 2 倍)

It's late at night, I hope that makes sense.深夜了,我希望这是有道理的。

Regradless here's my code: Regradless 这是我的代码:

Required libraries所需的库

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenMax.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/gsap/latest/plugins/ScrollToPlugin.min.js"></script>

Scroll code滚动代码

<script>
    $(function(){   
        var $window = $(window)
        var $scoll = $('#page-container')
        var scrollTime = 0.5
        var scrollDistance = 120

        var scrollTop = $scoll.scrollTop()

        $window.on("mousewheel DOMMouseScroll", function(event){

            event.preventDefault()  

            var delta = event.originalEvent.wheelDelta/120 || -event.originalEvent.detail/3
            scrollTop = scrollTop - parseInt(delta*scrollDistance)
            scrollTop = Math.max(0, Math.min($scoll[0].scrollHeight, scrollTop))

            TweenMax.to($scoll, scrollTime, {
                scrollTo : { y: scrollTop, autoKill:true },
                    ease: Power1.easeOut,
                    overwrite: 5                            
                })

        })
    })
</script>

Great question.很好的问题。

The library I use is this one: https://github.com/cferdinandi/smooth-scroll我使用的库是这个: https : //github.com/cferdinandi/smooth-scroll

Simply include the smoothscroll.js file, and job done.只需包含 smoothscroll.js 文件,工作就完成了。 The mouse-wheel will now smoothly easy down the page, rather than jumping down in chunks of pixels.鼠标滚轮现在可以平滑地向下滚动页面,而不是在像素块中向下跳跃。

It really improves the look of parallax webpages.它确实改善了视差网页的外观。

Btw, for parallax images, I use this library:顺便说一句,对于视差图像,我使用这个库:

https://github.com/pederan/Parallax-ImageScroll https://github.com/pederan/Parallax-ImageScroll

It's really easy to add to a webpage, just remember to include and initialise this library at the bottom of your webpage, after your images and HTML.添加到网页真的很容易,只需记住网页底部您的图像和 HTML之后包含并初始化这个库。

(I didn't realise that this would make a difference, but it absolutely does !) (我没有意识到这会有所作为,但绝对有!)

I know I'm late to the game but I was researching the topic of smooth scrolling today after I stumbled on I Love me Wellness' site.我知道我玩游戏迟到了,但在偶然发现I Love me Wellness网站后,我今天正在研究平滑滚动的主题。 Analyzing the sites JavaScript I found that they are using a vanillaJS script called Luxy.js which is showcased at Luxy.js on GitHub.分析网站 JavaScript 我发现他们使用了一个叫做 Luxy.js 的 vanillaJS 脚本,在 GitHub 上的Luxy.js上展示。 I find the effect rather pleasing.我觉得效果比较满意。 I don't think it's a very bad idea if implemented well.如果实施得好,我不认为这是一个非常糟糕的主意。 And it's very easy to implement.而且它很容易实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM