admin管理员组文章数量:1334669
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 badges5 Answers
Reset to default 5If 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
本文标签: javascriptjQuery colorbox positioning image wrong ( not centering )Stack Overflow
版权声明:本文标题:javascript - jQuery colorbox positioning image wrong ( not centering ) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742374433a2462876.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论