【问题标题】:Jquery horizontal parallax effect using stellar js ?使用恒星js的jquery水平视差效果?
【发布时间】:2013-02-08 04:38:11
【问题描述】:

我阅读了 stellar.js 的文档,示例显示了垂直滚动的工作原理,我尝试了简单的水平滚动视差,但我不确定我哪里出错了。对于称为元素的小对象,我设置了 data-stellar-ratio,对于背景,我设置了 data-background-stellar-js。我看过很多关于垂直滚动而不是水平滚动的教程。是否有其他方法可以仅通过 jquery 来实现?

     <!DOCTYPE html>

<head>
    <title></title>
    <style type="text/css">
        * {
            margin:0;
            padding:0;
        }
        body {
            height:100%;
            margin:0px;
        }
        #bg1 {
            background:red;
        }
        #bg2 {
            background:green;
        }
        #bg3 {
            background:yellow;
        }
        .elements {
            width:40px;
            height:40px;
            background:black;
            position:fixed;
        }
    </style>
</head>

<body>
    <div id="bg1" data-stellar-background-ratio="0.5">
        <div class="elements" data-stellar-ratio="0.5"></div>
    </div>
    <div id="bg2" data-stellar-background-ratio="0.5">
        <div class="elements" data-stellar-ratio="0.5"></div>
    </div>
    <div id="bg3" data-stellar-background-ratio="0.5">
        <div class="elements" data-stellar-ratio="0.5"></div>
    </div>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="stellar.js"></script>
    <script type="text/javascript">
        $(function () {
            $.stellar({
                horizontalScrolling: true,
                verticalOffset: 40
            });
        });
    </script>
</body>

【问题讨论】:

    标签: jquery horizontal-scrolling parallax


    【解决方案1】:

    有用的链接:

    http://dev.jonraasch.com/scrolling-parallax/docs

    http://www.egstudio.biz/easy-parallax-with-jquery/

    有很多不同的方法可以做到这一点,你甚至可以在没有其他文件的情况下做到这一点。

    http://jsfiddle.net/4kG6s/1/

    HTML

    <div id="background">
    </div>
    
    <div id="middleground">
    </div>
    
    <div id="foreground">
    </div>
    

    CSS

    div{
        position:absolute;
        top:0;
        left:0;
        right:0;
        bottom:0;
    }
    
    #background{
        z-index:1;
        background:url('http://placehold.it/100x100&text=Background');
    }
    #middleground{
        z-index:2;
        background:url('http://placehold.it/250x250&text=Middleground');
        opacity:.5;
    }
    #foreground{
        z-index:3;
        background:url('http://placehold.it/350x350&text=Foreground');
        opacity:.25;
    }
    

    JS

    /*
    function AnimateMe(){
        $("#background").css("background-position", "-=2");
        $("#middleground").css("background-position", "-=4");
        $("#foreground").css("background-position", "-=8");    
    }
    
    setInterval(AnimateMe, 1000/60);
    */
    
    var strength1 = 50;
    var strength2 = 100;
    var strength3 = 200;
    $("html").mousemove(function(e){
        var pageX = e.pageX - ($(window).width() / 2);
        var pageY = e.pageY - ($(window).height() / 2);
        var newvalueX = 1* pageX * -1;
        var newvalueY = 1* pageY * -1;
        $('#background').css("background-position", (strength1 / $(window).width() * pageX * -1)+"px "+(strength1  / $(window).height() * pageY * -1)+"px");
        $('#middleground').css("background-position", (strength2 / $(window).width() * pageX * -1)+"px "+(strength2  / $(window).height() * pageY * -1)+"px");
        $('#foreground').css("background-position", (strength3 / $(window).width() * pageX * -1)+"px "+(strength3  / $(window).height() * pageY * -1)+"px");
    });
    

    希望这一切都有帮助!

    【讨论】:

      【解决方案2】:

      试试这个代码 见demohttp://jsfiddle.net/nilesh026/4eBDE/1/

       <html>
       <head>
      <title></title>
       <script type="text/javascript" src="jquery-1.6.1.js"></script>
      <script type="text/javascript" src="jquery.stellar.js"></script>
      
      <style type="text/css">
          * {
              margin:0;
              padding:0;
          }
          body {
              height:100%;
              margin:0px;
          }
          .photo {
              background-attachment: fixed;
              background-position: 50% 0;
              background-repeat: no-repeat;
              height: 450px;
              position: relative;
          }
          #bg1 {
              background:red;
          }
          #bg2 {
              background:green;
          }
          #bg3 {
              background:yellow;
          }
          .elements {
              width:400px;
              height:400px;
              background:black;
              position:fixed;
          }
      </style>
      

      <div class="photo" id="bg1" data-stellar-background-ratio="0.5">
          <div class="elements" data-stellar-ratio="0.5"></div>
      </div>
      <div class="photo" id="bg2" data-stellar-background-ratio="0.5">
         <div class="elements" data-stellar-ratio="0.5"></div>
      </div>
      <div class="photo" id="bg3" data-stellar-background-ratio="0.5">
         <div class="elements" data-stellar-ratio="0.5"></div>
      </div>
      <script type="text/javascript">
          $(function () {
              $.stellar({
                  horizontalScrolling: true,
                  verticalOffset: 40
              });
          });
      </script>
      

      【讨论】:

      • horizo​​ntalScrolling 设置为 true 但它垂直滚动?
      • 谢谢 :) 我理解我犯的 css 定位错误
      • 只是一个查询?为什么每个 div 中没有元素?
      猜你喜欢
      • 2010-09-24
      • 2018-01-31
      • 1970-01-01
      • 2015-03-20
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多