Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need ability to reset cycleH and cycleW values on resize #169

Open
info-lvsys opened this issue Jan 27, 2015 · 0 comments
Open

Need ability to reset cycleH and cycleW values on resize #169

info-lvsys opened this issue Jan 27, 2015 · 0 comments

Comments

@info-lvsys
Copy link

Hi There, we're big fans of your work. Also, let me say we can't use cycle2 at all for responsiveness - we have too many deployments of cycle in production.

We're able to tweak cycle and make it responsive fairly easily. but for that, we're resorting to a hack we don't like: accessing cycle internal DOM data cycleH and cycleW:

$(slideshow).children("div").each(function(){
            this.cycleW = containerWidth;
            this.cycleH = maxHeight;
        });

We know by reading the code that we could also set them to 0, but in that case, the end result might not be responsive.

It would be great if you could add a function to reset the cycleH and cycleW to values we pass, or default to recomputing dimensions.

Looking at the code base, this would be a small new function.

Here's our full example:

html looks like this:

<div class="slideshow">
  <div class="slide"><img src=".." width="100%" height="auto"/></div>
  <div class="slide"><p>Copy copy copy no photo on slide </p></div>
  <div class="slide"><img src=".." width="100%" height="auto"/></div>
  <div class="slide"><p>Copy copy copy no photo on slide </p></div>
  ...
</div>

on window resize function: (snippet):

$(window).resize(function(){
    // first time around, save original width & height for ratios and complicated preloading
    $(".slideshow").each(function(i,elt){
        var originalWidth = $(elt).attr("originalWidth"); // check existence of our ratio cache
        if (typeof originalWidth == "undefined") {
            var containerWidth = $(elt).parent().first().width(); // auto scale to container width.
            var containerHeight = $(elt).parent().first().height();
            $(elt).attr("originalWidth",containerWidth);
            $(elt).attr("originalHeight",containerHeight);
        }
    });

    // fix slideshows to their container width
    $(".slideshow").each(function(i,elt){
        var containerWidth = $(elt).parent().first().width();
        var originalWidth = $(elt).attr("originalWidth")*1;
        var originalHeight = $(elt).attr("originalHeight")*1;
        var height = Math.floor(originalHeight*containerWidth/originalWidth);   // maintain original ratio
        $(elt).css("width", containerWidth+"px").css("height",height+"px"); // container actual dimensions
        $(elt).children("div").css("width", containerWidth+"px"); // reset each slide width 
                                                                 // (fails once slides move, because the cycle plugin will auto compute based on the panel in it.
        $(elt).children("div").children().css("width", containerWidth+"px"); // fix direct children (images or divs - images should have height auto).

        // recompute max height based on inside slide, not just photos.
        // some slideshows have variable height slides.
        var maxHeight = 0;
        var panelArray = $(elt).children("div");
        for (var i = 0; i < panelArray.length; i++) {
            var height = $(panelArray[i]).height();
            if (height > maxHeight) maxHeight = height;
        }
        if (maxHeight > 0) {
            $(elt).height(maxHeight);
        }

        // fix internal cycle dimension cache (cycleW and cycleH on each slide)
        $(elt).children("div").each(function(){
            this.cycleW = containerWidth;
            this.cycleH = maxHeight;
        });
    });
});

Thank you so much for considering this small new function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant