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

added media-query option #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

added media-query option #65

wants to merge 1 commit into from

Conversation

jonex2
Copy link

@jonex2 jonex2 commented Apr 23, 2015

Because matching heights sometimes should only work at a special viewport, i added a media query option.
example of using the option:
$('li').matchHeight({
byRow: false,
property: 'height',
target: null,
remove: false,
mq: "(min-width: 400px) and (max-width: 800px)"
});

@blackwood
Copy link

blackwood commented Jul 13, 2016

If anyone is waiting for this to pull request to be resolved, here's an alternative way you can achieve a similar result, by wrapping matchHeight's internal _update function with a conditional.

// caching jQuery selectors is neat!
var $top = $('.card__top')
var $inner = $('.subcard-inner, .subcard-alone-inner')
var $title = $('.card-absent-title-shim')

// init bound variable which indicates whether or not we've bound the elements.
var bound

function bindHeights() {
    $top.matchHeight()
    $inner.matchHeight({
      byRow: false
    })

    $title.matchHeight({
      target: $('.subcard__title')
    })
    // indicate that we've bound them.
    bound = true
}
// bind our necessary elements with their options.
bindHeights()

// preserve matchHeight's internal update fn
var __update = $.fn.matchHeight._update

// overwrite the function so that it does a conditional check
$.fn.matchHeight._update = function() {
    if (window.innerWidth < 700) {
        // if we are below our desired breakpoint, we can unbind and skip update
        if (bound) {
            $top.matchHeight({ remove: true })
            $inner.matchHeight({ remove: true })
            $title.matchHeight({ remove: true })
            // no need to unbind twice
            bound = false
        }
        return false
    } else {
        // only rebind if we need to again
        if (!bound) {
            bindHeights()
        }
        // call our preserved matchHeight fn
        __update()
    }
}

@matthewellishanson
Copy link

@blackwood Used your alternative approach and it worked great. Just one problem though: when I reduce my viewport size the matchheight removes as desired, but when I go back to the larger viewport those binds don't return to the matched heights they had originally at that browser size. Do you have any idea how to make it return the matched heights when you resize to the original viewport?

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

Successfully merging this pull request may close these issues.

None yet

3 participants