Search code examples
jqueryhtmlcssparallax

Dynamic change of html attributes with jQuery


I have this jQuery plugin - Plax: https://github.com/cameronmcefee/plax

I wanted it to be responsive, which I partly have: http://jsfiddle.net/4kRDL/

My problem is the html attributes:

<img src="/parallax/4.svg" data-xrange="20" data-yrange="10" id="plax-img4"/>
    <img src="/parallax/3.svg" data-xrange="5" data-yrange="40" id="plax-img3"/>

The problem is that the data-xrange and data-yrange isn't being responsive, and when you minimize the browser-window they have the same x-range and y-range no matter what size the window have. When minimized the ranges are to large.

So I must get size of window when it resize, then with an algorithm find xrange and yrange, and then, change the attributes with jquery and then reinitialize plaxify.

I was thinking somthing like this pseudo-code, but don't know how to do it so it works:

$('#my-element')
    .attr('data-xrange', 'New Value')
    .attr('data-yrange', 'New Value');

// You can dynamically redefine the range of a layer
// by running plaxify() on it again.
$('#my-element').plaxify();

$(window).resize(function(){
                var w = $(window).width();
                if(w > 320 && #my-element.is('New Value')) {
                    #my-element.removeAttr('data-xrange,data-yrange');
                }
            });

Solution

  • It's from documentation of Plax:

    $('#plax-octocat').plaxify({"xRange":40,"yRange":40})
    $('#plax-earth').plaxify({"xRange":20,"yRange":20,"invert":true})
    
    $('#plax-bg').plaxify({"xRange":10,"yRange":10,"invert":true})
    $.plax.enable()
    
    $('#my-btn').click(function(){
      // bigger range
      $('#plax-octocat').plaxify({"xRange":200,"yRange":200}) // LOOK HERE
    })
    

    As you can see, if you want new xrange and yrange, then you have to set it by yourself. Then your piece of code should look something like:

    $(window).resize(function(){
        var w = $(window).width();
        if(w > 320){
           $('#my-element').plaxify({"xRange":newValue,"yRange":newValue});
        }
    });     
    

    EDIT: Also after changing data attributes with plaxify() you have to set CSS top and left values to 0. Something like that: http://jsfiddle.net/instead/4kRDL/14/