最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery colorbox positioning image wrong ( not centering ) - Stack Overflow

programmeradmin0浏览0评论

I am using the jQuery plugin Colorbox for some images on This page.

If you look at the main product image in the center, right below it are some little polaroid-looking thumbnails. If you click on one it pops up an image using Colorbox

If you make your browser window about 800px wide (the width of the page content) the popup will be centered. However as you widen the browser window the popup will drift more and more to the right (not staying in center).

Anyone know how I can fix this?

I am using the jQuery plugin Colorbox for some images on This page.

If you look at the main product image in the center, right below it are some little polaroid-looking thumbnails. If you click on one it pops up an image using Colorbox

If you make your browser window about 800px wide (the width of the page content) the popup will be centered. However as you widen the browser window the popup will drift more and more to the right (not staying in center).

Anyone know how I can fix this?

Share Improve this question asked Jun 10, 2010 at 16:24 JD IsaacksJD Isaacks 58k96 gold badges279 silver badges431 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

If the body tag is set to "position:relative" ColorBox will not accurately calculate the center of the page.

Well I was able to fix it by changing this line:

posLeft = Math.max(document.documentElement.clientWidth - settings.w - loadedWidth - interfaceWidth,0)/2 + $window.scrollLeft();

to

posLeft = Math.max(800 - settings.w - loadedWidth - interfaceWidth,0)/2 + $window.scrollLeft();

in jquery.colorbox.js

However a solution that doesn't envolve changing the source of the plugin and making it less dynamic would be ideal.

colorbox.css

#cboxLoadingOverlay{background:#fff url(images/loading.gif) no-repeat 5px 5px;}

replace with

#cboxLoadingOverlay{background:#fff url(images/loading.gif) no-repeat center center;}

I was having a problem with it not vertically aligning correctly, and I figured out that the solution is to use jQuery's .height() and .width() functions instead of document.documentElement.clientHeight and .clientWidth. So the posTop and posLeft variables should be set like this:

posTop = Math.max($window.height() - settings.h - loadedHeight - interfaceHeight, 0) / 2 + $window.scrollTop(),

posLeft = Math.max($window.width() - settings.w - loadedWidth - interfaceWidth, 0) / 2 + $window.scrollLeft();

I had the same problem, and my body was not set to "position:relative".

This may not be the best solution, but you can fix it without modifying the plugin code, just by using the onComplete event :

$('img.colorbox').colorbox({onComplete: function() {
     $('#colorbox').css('left', ((window.innerWidth - $('#colorbox').width()) / 2) + 'px');
} });

The colorbox will load and appear not centered, and then be immediately repositionned. The user will see the transition, but the box is centered.

This is still not ideal, but does not involve changes in the plugin

发布评论

评论列表(0)

  1. 暂无评论