简体   繁体   English

javascript水平视差与缓和和stellar.js

[英]javascript horizontal parallax with easing and stellar.js

I'm trying to create an horizontal parallax scrolling site. 我正在尝试创建一个水平视差滚动网站。 For now everything is working except the easing part. 现在一切正常,除了缓和部分。 Here is the code: 这是代码:

<script type='text/javascript' src='http://code.jquery.com/jquery-latest.min.js'></script>
<script type='text/javascript' src='./js/jquery.mousewheel.js'></script>
<script type='text/javascript' src='./js/jquery.easing.js'></script>
<script type='text/javascript' src='./js/jquery.stellar.min.js'></script>
<script type='text/javascript'>
    $(function() {
        $("body").mousewheel(function(event, delta) {
            $('html body').animate({ scrollLeft: $('div.ease').offset.left - 40}, 6000, 'easeInOutExpo');
            this.scrollLeft -= (delta * 30);
            event.preventDefault();
        });
        $.stellar({
            horizontalScrolling: true
        });
    });
</script>
<style type="text/css" style="display: none !important;">
    * {
        margin: 0;
        padding: 0;
    }
    body {
        overflow-x: hidden;
    }
</style>

<div style="width: 3000px;">
    <div class="main" style="height: 100%; width: 1000px; background-color: #ff00ff; float: left; position: relative;">
        <div class="ease" style="font-size: 55px; color: #ffffff; margin-top: 30px; margin-left: 300px; position: relative;" data-stellar-ratio="3" >a</div>
        <div class="ease" style="font-size: 55px; color: #ffff00; margin-top: 95px; margin-left: 600px; position: relative;" data-stellar-ratio="0.9">b</div>
        <div class="ease" style="font-size: 55px; color: #0f0fff; margin-top: 200px; margin-left: 450px; position: relative;" data-stellar-ratio="0.9">c</div>
    </div>
    <div class="main" style="height: 100%; width: 1000px; background-color: #ffff00; float: left; position: relative;">
        <div class="ease" style="font-size: 55px; color: #ffffff; margin-top: 30px; margin-left: 300px; position: relative;" data-stellar-ratio="0.9" data-stellar-horizontal-offset="10">d</div>
        <div class="ease" style="font-size: 55px; color: #ff00ff; margin-top: 95px; margin-left: 600px; position: relative;" data-stellar-ratio="0.15" data-stellar-horizontal-offset="30">e</div>
        <div class="ease" style="font-size: 55px; color: #0f0fff; margin-top: 200px; margin-left: 450px; position: relative;" data-stellar-ratio="0.55" data-stellar-horizontal-offset="20">f</div>
    </div>
</div>

To try this (without easing) just remove the line 试试这个(没有缓和)只需删除该行

$('html body').animate({ scrollLeft: $('div.eases').offset.left - 40}, 6000, 'easeInOutExpo');

How can I add easing to the scrolling? 如何在滚动中添加缓动?

I'm trying to achieve something like this: http://hotdot.pro/en/ 我正在努力实现这样的目标: http//hotdot.pro/en/

Thank you! 谢谢!

http://jsfiddle.net/67grK/1/ http://jsfiddle.net/67grK/1/

I noticed that in your code, you call $('div.ease').offset.left This is where your code is breaking. 我注意到在你的代码中,你调用$('div.ease').offset.left这是你的代码破解的地方。 I've updated the fiddle to have the correct code. 我已经更新了小提琴以获得正确的代码。 It should be $('.ease').offset().left (It is considered bad practice to over qualify your css selectors, so you can leave the div out of it) 它应该是$('.ease').offset().left (过度限定你的css选择器被认为是不好的做法,所以你可以把div留下来)

The animate code should be solid, however, your logic on choosing where to animate to still needs some work. 动画代码应该是可靠的,但是,您选择动画放置位置的逻辑仍然需要一些工作。 You select $('div.ease') and don't specify which index of the indicies you are referencing, so jquery assumes the first node in the DOM. 您选择$('div.ease')并且不指定您引用的指标的哪个索引,因此jquery假定DOM中的第一个节点。

Secondly, every time a mouse event is registered, it is trying to animate it. 其次,每次注册鼠标事件时,都会尝试对其进行动画处理。 I would unbind the mouse scroll and in a callback function rebind the mouse scroll, and update some kind of counter to select which index you wanted to go to. 我将取消绑定鼠标滚动并在回调函数中重新绑定鼠标滚动,并更新某种计数器以选择您想要转到的索引。

This is a really good example of what you are trying to create 这是您尝试创建的一个非常好的示例

http://webdesign.tutsplus.com/tutorials/complete-websites/create-a-parallax-scrolling-website-using-stellar-js/ http://webdesign.tutsplus.com/tutorials/complete-websites/create-a-parallax-scrolling-website-using-stellar-js/

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

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