Last active
August 29, 2015 14:03
-
-
Save drifterz28/6e9abf4584dcfeddfa26 to your computer and use it in GitHub Desktop.
simple parallax to my understanding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $.fn.parallax = function() { | |
| var $parallax = this; | |
| $(window).on('scroll', function () { | |
| var win_offset = window.pageYOffset; | |
| $parallax.each(function (i){ | |
| var $local_this = $(this); | |
| var type = $local_this.attr('data-type'); | |
| var speed = $local_this.attr('data-parallax'); | |
| var direction = $local_this.attr('data-direction') || 'up'; | |
| var parallax = win_offset / +speed; | |
| if (type === 'background') { | |
| var current_pos = window.getComputedStyle($local_this[i], null).backgroundPosition.split(' '); | |
| if ($local_this.attr('data-bg_pos')) { | |
| current_pos = $local_this.attr('data-bg_pos').split(','); | |
| } | |
| var current_pos_top = parseInt(bg_pos[1].replace('/px%/i', ''), 10); | |
| var current_pos_left = parseInt(bg_pos[0].replace('/px%/i', ''), 10); | |
| $local_this.attr('data-bg_pos', bg_pos); | |
| $local_this.css({ | |
| 'background-position': current_pos[0] + ' ' + (current_pos_top - parallax) + 'px' | |
| }); | |
| } else { | |
| $local_this.css('top', '-' + parallax + 'px'); | |
| } | |
| }); | |
| }); | |
| }; | |
| $('.js-parallax').parallax(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment