简体   繁体   English

ScrollTo和Localscroll的视差背景效果

[英]Parallax background effect with ScrollTo and Localscroll

I have looked around for hours trying to figure this out. 我花了好几个小时试图解决这个问题。 I have a site built up like this jsfiddle . 我有一个像jsfiddle这样的网站

<div id="window">
 <div id="section-wrapper">
     <div id="one" class="section"><p>One</p>
     </div>
     <div id="two" class="section"><p>Two</p>
     </div>
     <div id="three" class="section"><p>Three</p>
     </div>
     <div id="four" class="section"><p>Four</p>
     </div>
     <div id="five" class="section"><p>Five</p>
     </div>
     <div id="six" class="section"><p>Six</p>
     </div>
     <div id="seven" class="section"><p>Seven</p>
     </div>
     <div id="eight" class="section"><p>Eight</p>
     </div>
     <div id="nine" class="section"><p>Nine</p>
     </div>
</div>

I use ScrollTo and localscroll to navigate the site and I wonder how I can implement a parallax effect to the section wrapper and make the background move slower. 我使用ScrollTo和localscroll来导航站点,我想知道如何实现对节包装器的视差效果并使背景移动变慢。 Everywhere I look the parallax plugins and scripts is based on a scroll function, but since I dont use scroll to navigate I have no idea how to add the effect. 我在所有地方看到的视差插件和脚本都是基于滚动功能的,但是由于我不使用滚动来导航,所以我不知道如何添加效果。

Is there a way to only adress the actual background-image, ie use .animate and/or .css to background-position property or should I put an element behind the section-wrapper? 有没有办法只处理实际的背景图像,即使用.animate和/或.css作为background-position属性,还是应该在节包装器后面放置一个元素? Im fairly new to jquery and javascript but if anyone could point me in the right direction i think i could figure it out. 我对jQuery和JavaScript相当陌生,但是如果有人可以指出正确的方向,我想我可以解决。 I have just have no idea where to start.. 我只是不知道从哪里开始。

Attach this file to header of your site https://github.com/markdalgleish/stellar.js/tree/master/src 将此文件附加到您网站的标题https://github.com/markdalgleish/stellar.js/tree/master/src

initiate the script 启动脚本

<script type='text/javascript'>
<!--
(function($) {
    $(document).ready(function() {
        $.stellar({
            horizontalScrolling: false,
            verticalOffset: 40
        });
    });
})(jQuery);
// -->
</script>

now you need to add the tag below to div that wraps the background 现在您需要将下面的标签添加到环绕背景的div中

<div data-stellar-background-ratio="0.5">

You can update the value 0.5 - it defines the speed of background image. 您可以更新值0.5-它定义背景图像的速度。

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

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