Giter Site home page Giter Site logo

Plax Responsive-ness about plax HOT 10 CLOSED

jaredrethman avatar jaredrethman commented on July 26, 2024
Plax Responsive-ness

from plax.

Comments (10)

xaolas avatar xaolas commented on July 26, 2024 1

Its work on resize:

    $('.forest__man, .forest__lens').plaxify();
    $.plax.enable();

    $(window).resize(function() {
        $('.forest__man, .forest__lens').removeAttr('style');
    });

from plax.

cameronmcefee avatar cameronmcefee commented on July 26, 2024

Hi @Lawless55,

What you want to do definitely possible. There are two avenues you can choose, depending on the desired result

1. I want to reposition the plax illustration, but the ranges and element position can stay the same

If you're not rescaling the ranges, simply trying to move the whole plaxed illustration around, all it takes is a wrapper div. All of plax's elements are absolutely positioned, so all you would need to do is put them in a div is position:relative or position:absolute. Then in your mediaquery specific code, you just move the wrapper div around and plax will follow.

2. You want to change the ranges

If you need to actually change the distance elements travel or where they travel from this is possible, but a little more work.

First, because of the amount of calculation that happens, I'd avoid changing plax directly on resize(), since this event continually fires until you stop resizing. You could try it out on resize(), but I'm not sure what to expect. Stuff may jump around or your browser may burst into flames.

Essentially all you need to do is replaxify your element. Check out this example from the docs:

$('#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})
})

On the click, it just specifies the range for #plax-octocat again. Plax will just drop the old range and use the new one.

Since you're doing responsive stuff (something plax technically wasn't designed for) and the xy coordinates for where your elements need to start from are likely to change, you may need to have something in there that works like this pseudo code

on resize complete do:
  disable plax
  clear all plax element inline css
  reposition plax elements to their new base positions
  replaxify your elements with new preferred ranges
  enable plax again

This may seem like a lot of work to do for something simple like resizing, but keep in mind, mediaqueries are mostly intended for different device sizes, and not so much browser resizing (which doesn't happen all that often). Chances are, this won't happen more than once on a page.

from plax.

jaredrethman avatar jaredrethman commented on July 26, 2024

Hey @cameronmcefee

Thanks a mill for getting back to me.

Almost exactly "point for point" how you described it above is how i tried to tackle this. But i kept running into issues, for instance i was attaching the invert, x, yRange(s) to the elements like on the 404 github page, i didn't know how to (Cause i'm such a noob) update the elements when resized. So i eventually had success doing it the way you mentioned step for step, in the above two functions and then running this resize() function below, so it didn't set my computer alight :)

$(window).resize(function() {
disablePlax();
if (plaxDisabled)
clearTimeout(plaxDisabled);
plaxDisabled = setTimeout(function() {
enablePlax();
}, 20);
});

And it seamed to do the trick, it shudders a bit - but it works. Thanks again for Plax and your help mate.

The support for accelerometer is what sold it for me, so with a couple of hacks, it can also be responsive :) . Also i did try using the update from - #36, but it through an error and froze every time on my Galaxy note 10.1 - but i think that has something to do with another plugin i'm using... not sure, maybe if its cool i could send you the finished product and you could let me know what you think about how I've implemented your code?

from plax.

cameronmcefee avatar cameronmcefee commented on July 26, 2024

There isn't a built in resizestop event, so you'd have to write something on your own or grab a plugin. Since you're using jQuery, this looks like it might do the trick. https://github.com/misteroneill/resize-stop

Just be sure it loads after jQuery and before Plax.

from plax.

jaredrethman avatar jaredrethman commented on July 26, 2024

The reason i needed to use the resize event like this is that i needed to run disablePlax(); while/during the resize event and then once complete then start Plax up again - enablePlax();. Not sure, as i said, i'm still learning :)

from plax.

vbk avatar vbk commented on July 26, 2024

@Lawless55

Could you tell me if you are able to responsify? Please let me know how you achieved it and provide me the code if possible.

Thanks!

from plax.

jaredrethman avatar jaredrethman commented on July 26, 2024

Hey VBK,

What I did was create two sets of data objects on each div, like so.

"<"div class="js-plaxify" data-xrangesml="25" data-yrangesml="35" data-xrangelrg="50" data-yrangelrg="70" id="parallax_ele">"

Then I created three separate functions, one for each data object size (Small and large) and one to disable and clear all Plax'd elements.

For Desktop:
function enableDeskPlax(){
var layers = $('.js-plaxify')
var plaxDesk = $.each(layers, function(index, layer){
$(layer).plaxify({
xRange: $(layer).data('xrangelrg') || 0,
yRange: $(layer).data('yrangelrg') || 0,
invert: $(layer).data('invert') || false
})
})
$.plax.enable();
}

For Mobile:
function enableMobiPlax(){
var layers = $('.js-plaxify')
var plaxMobi = $.each(layers, function(index, layer){
$(layer).plaxify({
xRange: $(layer).data('xrangesml') || 0,
yRange: $(layer).data('yrangesml') || 0,
invert: $(layer).data('invert') || false
})
})
$.plax.enable();
}

Clearing function:
function disablePlax(){
$.plax.disable({ "clearLayers": true })
$('div.js-plaxify[style]').css('top', ''); //use jQuery to clear all inline styles generated by Plax
$('div.js-plaxify[style]').css('left', ''); //use jQuery to clear all inline styles generated by Plax
}

Then I setup a function that gets run through on load and on resize();

function resizeShit() {
disablePlax(); //Start by clearing Plax, especially for resize()
var windowSize = $(window).width();
if (windowSize < 769){
enableMobiPlax();
} else {
enableDeskPlax();
}
}

I would suggest using something like the debounce resize method, or Camerons above mentioned plugin to do your resizing, otherwise each of those calls gets fired a gizilian times and your "browser might burst into flames".

Also you will need to use Media Queries to swap out the background images, in conjunction with something like
respond.js for cross browser support And you will need to size down the wrapper on each iteration too.

Lastly, I haven't had good luck with the later versions of plax the earlier versions have worked better for me.

Hope this helps.

from plax.

vbk avatar vbk commented on July 26, 2024

Thank you!

from plax.

Dwtbc avatar Dwtbc commented on July 26, 2024

Hello Lawless55

I tried your code but I can't seem to get it working.

When I use your code, the PLAX effect gets turned off.

Have you got a demo of what you've made?

Daniel

from plax.

jaredrethman avatar jaredrethman commented on July 26, 2024

Sorry mate, its my portfolio... and i have put it on hold until i get some more free time :)

You need to make sure you run either enableDeskPlax() or enableMobiPlax() onResize. I would suggest putting in a console.log("Plax Enabled"); into both functions and check the console to see if they get called.

Could you post your code to have a look.

from plax.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.