Added feature
This commit is contained in:
parent
db5afc91f9
commit
271efb8565
1 changed files with 19 additions and 8 deletions
17
parallax.js
17
parallax.js
|
@ -2,10 +2,12 @@ var Parallax = {
|
||||||
initialise: function(createEvents) {
|
initialise: function(createEvents) {
|
||||||
$(this.ident).each(function(){
|
$(this.ident).each(function(){
|
||||||
// Calculate Offset
|
// Calculate Offset
|
||||||
|
if ($(this).attr('parallax-inset') && $(this).attr('parallax-mode') == 'inset') {
|
||||||
inset = $(this).attr('parallax-inset');
|
inset = $(this).attr('parallax-inset');
|
||||||
image_height = $(this).height();
|
image_height = $(this).height();
|
||||||
range = image_height / inset;
|
range = image_height / inset;
|
||||||
$(this).attr('parallax-offset', range / 2);
|
$(this).attr('parallax-offset', range / 2);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
console.log("Parallax Initialised.");
|
console.log("Parallax Initialised.");
|
||||||
if (createEvents) {this.createEvents();}
|
if (createEvents) {this.createEvents();}
|
||||||
|
@ -19,14 +21,23 @@ var Parallax = {
|
||||||
|
|
||||||
$(this.ident).each(function(){
|
$(this.ident).each(function(){
|
||||||
// If the element isn't on the screen, or wasn't initialised, ignore it.
|
// If the element isn't on the screen, or wasn't initialised, ignore it.
|
||||||
if (!is_on_screen(this) || !$(this).attr('parallax-offset')) { return; }
|
if (!is_on_screen(this)) { return; }
|
||||||
element = $(this);
|
element = $(this);
|
||||||
//Calculate the center position of the image
|
//Calculate the center position of the image
|
||||||
center_pos = (element.offset().top - $(window).scrollTop()) + (element.height() / 2);
|
center_pos = (element.offset().top - window_top) + (element.height() / 2);
|
||||||
// Calculate the position of the center point, relative to the center.
|
// Calculate the position of the center point, relative to the center.
|
||||||
perc = -(0.5 - ((window_top - center_pos) / window_bottom));
|
perc = -(0.5 - ((window_top - center_pos) / window_bottom));
|
||||||
// Calculate the position of the center of the image from the percentage through the screen.
|
|
||||||
|
switch(element.attr('parallax-mode')) {
|
||||||
|
case 'inset':
|
||||||
pos = Math.round((perc * element.attr('parallax-offset')) * 100) / 100;
|
pos = Math.round((perc * element.attr('parallax-offset')) * 100) / 100;
|
||||||
|
console.log(pos)
|
||||||
|
break;
|
||||||
|
case 'linear':
|
||||||
|
default:
|
||||||
|
pos = perc*(element.height()/2);
|
||||||
|
break;
|
||||||
|
}
|
||||||
// Apply the calculated value.
|
// Apply the calculated value.
|
||||||
element.css('background-position',"0px " + pos + "px");
|
element.css('background-position',"0px " + pos + "px");
|
||||||
});
|
});
|
||||||
|
|
Reference in a new issue