0

I've found a jquery script to make divs have equal heights. It uses window.load and window.resize to call the function. When i load the page or resize it works, however if i go to the page via a link on the site the divs become their old height again. I think this has to do with the rails turbolinks (similar to rails 4 with turbolinks and window load), but i can not get it to work (even with the solution in the link). this is the script (it is loaded through the asset pipeline

equalheight = function(container){

  var currentTallest = 0,
    currentRowStart = 0,
    rowDivs = new Array(),
    $el,
    topPosition = 0;
  $(container).each(function() {
    $el = $(this);
    $($el).height('auto')
    topPostion = $el.position().top;

    if (currentRowStart != topPostion) {
      for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
        rowDivs[currentDiv].height(currentTallest);
      }
      rowDivs.length = 0; // empty the array
      currentRowStart = topPostion;
      currentTallest = $el.height();
      rowDivs.push($el);
    } else {
      rowDivs.push($el);
      currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
    }
    for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
      rowDivs[currentDiv].height(currentTallest);
    }
  });
}

$(window).load(function() {
  equalheight('.container-fluid box');
});

$(window).resize(function(){
  equalheight('.container-fluid box');
});
5
  • Please elaborate "if i click on the home button it does not load". What is this button, a link or u ve an event attached to it? Commented Nov 23, 2015 at 12:07
  • Possibly a duplicate of this question: stackoverflow.com/questions/21226357/… Commented Nov 23, 2015 at 12:07
  • i've tried that duplicate but could not get it to work properly, and updated my question Commented Nov 23, 2015 at 12:10
  • what error are you getting in your browser's console ? Commented Nov 23, 2015 at 12:13
  • i'm not getting an error, it just doesn't do anything because the .load or .resize events are not called Commented Nov 23, 2015 at 12:14

2 Answers 2

1

Use this :-

$(document).on('ready page:load', function() {
    equalheight('.container-fluid box');
})

OR this :-

$(document).on('ready', function() {
    equalheight('.container-fluid box');
});
Sign up to request clarification or add additional context in comments.

2 Comments

if i use this it completely messes up my css for some reason and makes rows collapse over different rows
got it to work, as long as I add this instead of replace the window.load. not sure it's the cleanest solution but it works. thanks
0
$(document).on('ready', function() {
    equalheight('.container-fluid box');
});

Does not always work. instead us:

$(document).on('turbolinks:load', function() {
    equalheight('.container-fluid box');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.