snipt

Ctrl+h for KB shortcuts

JavaScript

Scrolling Background

1
2
3
4
5
$('body').css('background-position', "center 0" );
$(window).scroll(function() {
    var x = $(this).scrollTop();
    $('body').css('background-position', 'center ' + parseInt(-x / 6) + 'px');
});
https://snipt.net/embed/e4ff0cd303f93929a00e15a96d679617/
/raw/e4ff0cd303f93929a00e15a96d679617/
e4ff0cd303f93929a00e15a96d679617
js
JavaScript
5
2019-07-04T15:47:33
True
False
False
/api/public/snipt/24280/
scrolling-background-1
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="nx">$</span><span class="p">(</span><span class="s1">&#39;body&#39;</span><span class="p">).</span><span class="nx">css</span><span class="p">(</span><span class="s1">&#39;background-position&#39;</span><span class="p">,</span> <span class="s2">&quot;center 0&quot;</span> <span class="p">);</span> </span><span id="L-2"><a name="L-2"></a><span class="nx">$</span><span class="p">(</span><span class="nb">window</span><span class="p">).</span><span class="nx">scroll</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span> </span><span id="L-3"><a name="L-3"></a> <span class="kd">var</span> <span class="nx">x</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">scrollTop</span><span class="p">();</span> </span><span id="L-4"><a name="L-4"></a> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;body&#39;</span><span class="p">).</span><span class="nx">css</span><span class="p">(</span><span class="s1">&#39;background-position&#39;</span><span class="p">,</span> <span class="s1">&#39;center &#39;</span> <span class="o">+</span> <span class="nb">parseInt</span><span class="p">(</span><span class="o">-</span><span class="nx">x</span> <span class="o">/</span> <span class="mi">6</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;px&#39;</span><span class="p">);</span> </span><span id="L-5"><a name="L-5"></a><span class="p">});</span> </span></pre></div> </td></tr></table>
"background position", "better world", nike, scroll