jquery - Uncaught ReferenceError: x is not defined, when trying to return object from function -
i'm trying create function positions of elements, when try bottom offset return not defined, missing?
this attempt:
var mydiv = jquery('.div'); function getoffset(el) { var off = jquery(el).offset(); return { left: off.left, right: off.left + el.outerwidth(), top: off.top, bottom: off.top + el.outerheight(), hcenter: (bottom + top) /2, wcenter: (right + left) /2 }; } var mydivposition = getoffset(mydiv); console.log( "top = " + mydivposition.top + "bottom = " + mydivposition.bottom + "hcenter = " + mydivposition.hcenter + "left = " + mydivposition.left + "right = " + mydivposition.right + "wcenter = " + mydivposition.wcenter );
i in console:
uncaught referenceerror: bottom not defined
you're trying use bottom
, right
variables haven't declared. note using unqualified bottom
or right
identifer doesn't in way retrieve them object you're building (in fact, there no reasonable way). them up-front (you need refer top
, left
on off
):
function getoffset(el) { var off = jquery(el).offset(); var bottom = off.top + el.outerheight(); // *** var right = off.left + el.outerwidth(); // *** return { left: off.left, right: right, // *** top: off.top, bottom: bottom, // *** hcenter: (bottom - off.top) /2, // *** wcenter: (right - off.left) /2 // *** }; }
Comments
Post a Comment