The idea is simple get the height
of the bigger-objet (h1
) and of the smaller-object (h2
), substract them and divide over 2. In other words h3=(h1-h2)/2
. And use said h3
as margin-top
and margin-bottom
for the smaller-object.
From what I understood from this reference. I came up with this:
(function($) {
$.fn.vAlign = function() {
return this.each(function() {
var bigger_height_object = document.getElementsByClassName('object-aligned-against');
var smaller_height_object = document.getElementsByClassName('aligned-object');
var h1 = bigger_height_object.height();
var h2 = smaller_height_object.height();
var h3 = (h1-h2) / 2;
smaller_height_object.css('margin-top', h3);
smaller_height_object.css('margin-bottom', h3);
});
};
}) (jQuery);
From what I read, it is possible to use multiple selectors, so would it be possible to call the plugin as something like this: $('.aligned-object' '.object-aligned-against').vAlign()
?
Or what would be the appropriate way to do it?
Edit: missed the .
on the selector classes.
Edit2: removed the ,
on the selector, since I'm looking for both of the classes independentlty.
`