I got some code which centers a div in a middle of the webpage. The code works great on chrome, but for some reason, on IE(I think it's 9 or 10) it works for the first time, and then it doesn't.
To demonstrate the issue, here are some photos: (Ignore the signs you don't understand, thats my language)
Chrome:
IE first time I click on an object to open up:
IE second time or more, when I click on an object to open up:
This is the code I'm using to center the div:
jQuery.fn.center = function ()
{
this.css("position","fixed");
this.css("top", ($(window).height() / 2) - (this.outerHeight() / 2));
this.css("left", ($(window).width() / 2) - (this.outerWidth() / 2));
return this;
}
The code that uses the center()
function:
$('#products,#search-result').on("click",'.product-item-info',function() {
var pid = $(this).find('input').val();
$.get('product_info.php',{pid:pid},function(data){
$('#product-lb').html(data).append('<span class="x"><span>X</span></span>');
$('.x').click(function() {
$('#dimmer').click();
});
$('#dimmer').css({width:$('html').width(),height:$('html').height()});
$('#product-lb').center();
$('#product-lb').show(800);
$('#dimmer').show();
$(window).resize(function() {
$('#product-lb').center();
});
});
});
explanations:
#products
is the div that contains all of the colorized divs in blue and green as you can see in the picture.product-item-info
is the class of the rectangle that can be found in each product DIV.#dimmer
is the black background#product-lb
is the div that has to be centered and shown on the page
After testing lot of potentail things, I've come up with a clue. When I try to console the heights properties like this:
jQuery.fn.center = function ()
{
console.log($(window).height());
console.log(this.outerHeight());
this.css("position","fixed");
this.css("top", ($(window).height() / 2) - (this.outerHeight() / 2));
this.css("left", ($(window).width() / 2) - (this.outerWidth() / 2));
return this;
}
I saw diffrences between chrome, explorer first time, and explorer second time:
Chrome - First time open the div console log:
Chrome - After closing div console:
Internet Explorer - First time open the div:
Now comes the first step of the weird part, it seems like when the div is being closed, the function is being triggered, therefore the console looks like that:
Now, this is the most disturbing thing that I've found, on the second attempt to open up a div to view, look at the console:
And after closing this div:
It seems like the event is trigerred whenever I open the div, close the div, and it triggeres up to 4-5 times.
this is my close function:
$('#dimmer').click(function() {
$('#dimmer').fadeOut(800);
$('#product-lb').hide(800);
});
$('.x').click(function() {
$('#dimmer').click();
});
I have no clue why it's happening! In chrome everything is just fine, but when it comes to internet explorer, its all messy.
Thanks in advance!