Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** paralax plugin **/
- (function($) {
- $.fn.jparalax = function ( options, startcallback, finishcallback ) {
- var $this = this;
- var settings = {
- effect : 'fixedbackground',
- speed : 100,
- container : window
- };
- startcallback = startcallback ? startcallback : [];
- finishcallback = finishcallback ? finishcallback : [];
- var options = $.extend( {}, settings, options );
- return this.each(function (i) {
- if($(this).attr('data-parent')) {
- var parent = "#" + $(this).attr('data-parent');
- var element = $(parent);
- var child = $(this);
- var childpos = $(child).position();
- /** data center **/
- if($(child).attr('data-top') == 'center'){
- var datatop = ( $(window).height() - $(child).height()) / 2;
- $(child).attr('data-top', datatop).css("top", datatop + "px");
- }
- } else {
- var element = $(this);
- }
- var position = $(element).offset();
- var o = options;
- var $container = $(o.container);
- var speed = o.speed;
- var effect = o.effect;
- var bgpos = $(element).css('backgroundPosition').split(" ");
- var useragent = detectbrowser();
- function detectbrowser() {
- if (navigator.userAgent.indexOf('Firefox') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Firefox') + 8)) >= 3.6){//Firefox
- return "firefox";
- } else if (navigator.userAgent.indexOf('Chrome') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Chrome') + 7).split(' ')[0]) >= 15){//Chrome
- return "chrome";
- } else if(navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Version') != -1 && parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf('Version') + 8).split(' ')[0]) >= 5){//Safari
- return "safari";
- } else{
- return "unknown";
- }
- }
- function calcpos() {
- var minpos = position.top - $container.height();
- var maxpos = position.top + $(element).height() + $('nav').height();
- return {
- 'min' : minpos,
- 'max' : maxpos,
- 'diff' : ( maxpos - minpos )
- };
- }
- function fixedbackground(ratio, scrollpos, dpos){
- var bgposresult = parseInt(bgpos[1]) - ( ratio * speed );
- var newposition = bgpos[0] + " " + bgposresult + "px";
- $(element).css('background-position', newposition);
- }
- function staytop(scrollpos, ratio, dpos) {
- var thistop = scrollpos - position.top + parseInt($(child).attr("data-top")) + $('nav').height();
- thistop = ( thistop - ( ratio * speed ) ) + "px";
- $(child).css("top", thistop);
- /*
- if( scrollpos > ( position.top - $('nav').height() ) ) {
- var unknownvar = ( ( position.top - $('nav').height() ) / dpos.diff ) * speed;
- var thistop = ( scrollpos - position.top + parseInt($(child).attr("data-top")) + $('nav').height() + unknownvar );
- thistop = ( thistop - ( ratio * speed ) ) + "px";
- $(child).css("top", thistop);
- } else {
- $(child).css("top", $(child).attr("data-top") + "px");
- }
- */
- }
- function staytopfixed(scrollpos, ratio, dpos) {
- if( scrollpos > ( position.top - $('nav').height() )) {
- } else {
- $(child).css("top", $(child).attr("data-top") + "px").css("position", "relative");
- }
- }
- function doparalax() {
- var dpos = calcpos();
- var scrollpos = $(window).scrollTop();
- if( scrollpos >= dpos.min && scrollpos <= dpos.max) {
- if(effect == 'fixedbackground') {
- var ratio = ( scrollpos - dpos.min ) / dpos.diff;
- fixedbackground(ratio, scrollpos, dpos);
- } else if (effect = 'staytop') {
- var ratio = ( scrollpos - dpos.min ) / dpos.diff;
- if(useragent == "firefox") {
- staytop(scrollpos, ratio, dpos);
- } else {
- staytopfixed(scrollpos, ratio, dpos);
- }
- }
- }
- }
- $container.bind('scroll load resize', function(e){
- doparalax();
- });
- });
- };
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment