I have a function called:
showModal(data);
which when called, will create a modal with the content passed in. I was trying to center it, but i dont think ‘this’ is referring to the right object.
function showModal(data){
$("<div class = 'modal'>").css({
"z-index": Number($("#overlay1").css("z-index")) + 5,
top: $(window).height() / 2 - $(this).height() / 2,
left: $(window).width() / 2 - $(this).width() / 2
}).append(data).appendTo("body").fadeIn();
}
It will just keep it at 0,0 on screen. How do i adjust it?
Try this:
In your context,
thisrefers to the functionshowModalI think. But you’re trying to refer to the newly created div with the class “modal”. You need to store a reference to it because the.csscall doesn’t change the reference tothisinside of it. You’re probably thinking of normal event bindings, where the anonymous function you pass to it has a new context that jQuery definesthisas the element in question. Withcss, you aren’t providing a callback, and therefore there is no way for jQuery to change or setthissince there is no new scope/context. I’m probably using the wrong terms (context, scope, etc.)…UPDATE:
The problem also seemed to be that since the
datawas not appended before the.csscall, the modal would not have any substantial width/height. Not sure if the modal being appended to the body needs to be done before the.csscall or not, but that might help too.