I am completely new to JavaScript/jQuery/programming in general, so please bear with me if I come across as painfully naive.
I've written a script that converts the footnotes on a Tumblr blog to "sidenotes." It works pretty well, for the most part. You can see examples on my blog.
I do this by appending the innerHTML of the footnotes to an empty div, then hiding the original footnotes with CSS (display: none
). I don't want to alter the markup of the posts at all. The idea is that I can remove the script and sidenotes revert or "degrade" back to footnotes.
The sidenotes are absolutely positioned relative to their container, and their position depends on several factors: the position of the preceding sidenote, the position of the reference link in the text, and the position of the container. Ideally, the sidenote should appear at the same vertical offset as the reference link in the text, but if the preceding element is in the way, it shifts down to avoid overlapping.
The script is pretty simple. Basic arithmetic. To avoid overlapping elements, I calculate the offset from the top of the page to the bottom of the preceding element. If its larger than the offset of the reference link (and the offset of the container), the element shifts down.
As you can see, everything works perfectly if the footnotes only contain text. But if they contain images, too, the sidenotes start to overlap. I believe it's because I'm using .outerHeight() to get the height of the element, but if the image hasn't loaded yet, it returns the wrong value. You can see an example of what I'm talking about on one of my test pages.
I think I've isolated the problem to the function below.
I thought that if I used .load, it would wait for the element's content to load before executing the code. But that doesn't seem to be happening.
Please tell me what I might be doing wrong. I'm pretty sure the script's problem is contained in the code below, but if you think this looks fine, I can post the rest of the script as well. Or you can view it at http://resources.contentioninvain.com/scripts/FootnotesToSidenotes.js
(Sorry, I'm a new user so I can't post more than two links, haha).
Thanks in advance for any suggestions. This is the first time I've posted a question, but I've found this site really useful in the past. Aside from tinkering, this is the first time I've tried to write a script from scratch (I'm kind of proud that I've gotten this far without help, haha).
// GetBottomOffset gets the vertical offset to the bottom of an element
// It adds the element's vertical offset and its height:
function GetBottomOffset(Element) {
$(Element).load(function() {
var ElementTop = Element.offset().top; // Vert offset of element
var ElementHeight = Element.outerHeight(true); // Height of element
// Offset to bottom of element
var ElementBottom = ElementTop + ElementHeight;
// Returns ElementBottom
return ElementBottom;
});
}