1

Ok, so I've created my own parallaxing jQuery/JS script, but I just don't know how to target browsers that will support the following (needs the following):

  • Supports transition for background-position

The site (preview) I'm trying this at is: My Test Site

How can I detect whether a browser will support this though? Can I run media queries bases on window width? Do I have to browser sniff? I can't see anything on the WWW that gives me clues as to how I can feature-detect for this.

Any help would be greatly appreciated.

My JS code is here: (please feel free to use it if you like it)

var goTop = 0;
var thisGoTop = 0;
var thisHeight = 0;
var thisWinH = 0;
var bottomIntrusion = 0;
var thisMax = 0;
var bgPos = 0;

function parallax(){

    goTop = $(window).scrollTop();
    thisWinH = $(window).height();

    $('div.para').each(function(){

        thisGoTop = $(this).offset().top;
        thisHeight = $(this).height();
        thisMax = Math.floor(thisHeight * 0.3);
        bottomIntrusion = (thisGoTop + (thisHeight / 2) - goTop) / thisWinH;

        if (bottomIntrusion > 0) {
            bgPos = 0 - (Math.floor(thisMax * bottomIntrusion));
            $(this).css('background-position','center ' + bgPos + 'px');
        }

    });

}

parallax();

$(window).scroll(function(){

    parallax();

});

Edit: I've looked into the Modernizr library, and I haven't found anything that gives me clues about background-position transform support for CSS3. I'm getting real close to brower sniffing at this point, and I'd hate to go there.

3Dom
  • 795
  • 3
  • 10
  • 22
  • Take a look at http://modernizr.com/ or more specifically this section: http://modernizr.com/docs/#features-css – Jimmyt1988 Nov 21 '13 at 14:16
  • What section James? Can anyone point me in the right direction, based on solid experience? – 3Dom Nov 21 '13 at 14:20
  • Ah well waddya know... it doesn't specifically have that plumbed in... doh. So what you'll have to do (and feel free to make an extension of the modernizr library, but if you cba.. which I can never be... wait for my answer and that should do the job) – Jimmyt1988 Nov 21 '13 at 14:29

1 Answers1

2
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script>
        $( document ).ready( function() {
            var Compatibility = function()
            {
                var context = this;

                context.TestObject = function( id )
                {
                    function CreateObject( id )
                    {
                        $('body').prepend('<div id="CompatibilityTestObject_' + id + '"></div>');
                    }

                    // Constructor
                    CreateObject( id );

                    return {
                        GetObject: function( id )
                        {
                            try
                            {
                                // Fetch with raw Javascript so if the javascript method does not exist it will throw an error
                                var fetchedDomElement = document.getElementById( "CompatibilityTestObject_" + id );
                                return fetchedDomElement;
                            }
                            catch( e )
                            {
                                // Failed to fetch DOM Element
                                return false;
                            }
                        },
                        DeleteObject: function( id )
                        {
                            try
                            {
                                $( "#CompatibilityTestObject_" + id ).remove();
                                return true;
                            }
                            catch( e )
                            {
                                // Failed to remove object
                                return false;
                            }
                        }
                    }
                }

                context.Factory = function()
                {
                    return {
                        CreateTestObject: function( id )
                        {
                            var m_TestObject = new context.TestObject( id );
                            return m_TestObject.GetObject( id );
                        },
                        DeleteTestObject: function( id )
                        {
                            return context.DeleteObject( id );
                        }
                    }
                }();

                var CSS = function()
                {
                    return {
                        BackgroundPosition: function()
                        {
                            var testObject = context.Factory.CreateTestObject( "BackgroundPosition" );

                            try
                            {
                                // My first try at testing for background positioning
                                testObject.style.backgroundPosition = "center";
                                return true;
                            }
                            catch( e )
                            {
                                // Implement the handling of this error
                                //alert( "Does not support backgroundPosition: " + e.message );
                                return false;
                            }                                                              
                        }
                    }
                }();

                return {
                    CheckCSS: CSS
                }
            }();
        } );
    </script>

Take a copy of the above script... place it in your code there, then call upon it like so:

if( Compatibility.CheckCSS.BackgroundPostion() )
{
   // Compatible browser
}
else 
{
   // Not compatible
}
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
  • Very interesting. What have you given me here exactly? It seems like you've given me a bg position check that validates feature detection for bg pos only? I'm not stupid, but I need help to understand why and what you've given me... – 3Dom Nov 21 '13 at 14:56
  • Haven't finished yet :) sorry. – Jimmyt1988 Nov 21 '13 at 14:58
  • Thanks James. I can't fathom the kind of enthusiasm that goes into solving someone else's code at this level. If you can solve this, please feel free to use/share this code anywhere. – 3Dom Nov 21 '13 at 15:09
  • @3Dom - It'd be a crap place to live if other's thought their time was more important than someone else's; so this background transition part of your OP... I see you are just calling background-position... Do you know a browser that does not work with this property? So I can fire it up in browserstack.com ? – Jimmyt1988 Nov 21 '13 at 15:19
  • Sorry if i'm teaching you to suck eggs. but in this script above, I hope it at the very least gets you started in making a scalable compatibility testing tool for yourself. Modernizr doesn't have all the answers.. Here you can add more CSS tests or add a new class for JavaScript for example... Should be helpful.. I just need to test if this code fails if background-position does not work. – Jimmyt1988 Nov 21 '13 at 15:27
  • @3Dom - I think this is working dude, check out the latest iteration of this (Oh, and no, I haven't been working on this the whole time ^_^ ) – Jimmyt1988 Nov 21 '13 at 17:08
  • Thanks heaps for the help. Really appreciate it. I'm still wrapping my head around it, but this is easily the best advice I've gotten so far. – 3Dom Nov 22 '13 at 08:58
  • With JavaScript, it's about more than personal preference when deviating from the standard format: [Dangerous implications of Allman style in JavaScript](http://stackoverflow.com/q/11247328/594235) – Sparky Oct 04 '16 at 18:12