【问题标题】:Lightbox Centering in jqueryjQuery中的灯箱居中
【发布时间】:2013-07-08 05:31:18
【问题描述】:

最近我尝试在灯箱中制作图像。如果您单击图像,它会以灯箱效果显示。但某些原因,Lightbox 没有在窗口大小中正确居中。例如,如果您单击它在灯箱中加载的图像,但第一次它在站点底部加载灯箱,然后再次单击它正确对齐的图像。 这是我所说的截图。 页面加载时单击图像时的第一个屏幕截图。

第一次点击图片

第二次点击图片

第一次遇到对齐问题。 第二次没有对齐问题(没有页面加载) Javascript:

 <script>
                jQuery(document).ready(function() {
                    jQuery("img").click(function() {
                          var img_path;               
            if ($(this).parent('a').length) {

                           img_path = $(this).parent('a').prop('href');
                            }
                        else
                            {
                         img_path = $(this).attr('src');
                            }

                        jQuery(".cplightbox1").html(jQuery("<img>").attr("src", img_path));
                        jQuery(".cpoutter").css('display', 'block');
                        jQuery('.cpoutter').animate({'opacity': '1'});
                        //jQuery('.lightbox').animate({'opacity':'1.00'});
                        var cplightbox = document.getElementsByClassName('cplightbox')[0];
                        var cpoutter = document.getElementsByClassName('cpoutter')[0];
                        cplightbox.style.marginTop = ((cpoutter.offsetHeight / 2) - (cplightbox.offsetHeight / 2)) + "px";
                        return false;
                    });
    });
    </script>

HTML 代码:

这是小提琴 http://jsfiddle.net/rCUGD/7/

但是这个脚本是如何在 jsfiddle.net 中正常工作的。可能是我搞砸了脚本或 css

我不在我犯错的地方

已编辑:

@JustAnil 之后的截图如下:

第二次点击后应该正常显示

【问题讨论】:

    标签: javascript jquery html css jquery-ui


    【解决方案1】:

    检查此工作JSFiddle

    您需要更改以下几行(计算偏移量的位置)。

    更改以下几行:

     var cplightbox = document.getElementsByClassName('cplightbox')[0];
     var cpoutter = document.getElementsByClassName('cpoutter')[0];
     cplightbox.style.marginTop = ((cpoutter.offsetHeight / 2) - (cplightbox.offsetHeight / 2)) + "px";
    

    收件人

    var cplightbox = document.getElementsByClassName('cplightbox')[0];
    
    // We need the actual height of the image so grab it from the "inner" container
    var cplightbox1 = document.getElementsByClassName('cplightbox1')[0]; // New Line
    
    var cpoutter = document.getElementsByClassName('cpoutter')[0];
    
    // Calculate the (negative) offset from the width & height
    cplightbox.style.marginLeft = "-"+$(cplightbox1).width() / 2 + "px";
    cplightbox.style.marginTop = "-"+$(cplightbox1).height() / 2 + "px";
    // ^ Negative offset so we can vertically and horizontally center it.
    

    终于

    从以下位置更改您的 CSS:

    .cplightbox {
        margin-left: auto;
        margin-right:auto;
        width:auto;
        height:auto;
        display:inline-block;
    }
    

    收件人:

    .cplightbox {
         position:fixed;
         top:50%;
         left:50%;
         display:inline-block;
     }
    

    检查这个问题CSS Vertically & Horizontally Center Div(这就是如何将 div 居中到屏幕中间)。

    然后更改您的 javascript 以计算 偏移量(取决于图片的大小[即宽度和高度的 50%])

    查看此工作JSFiddle

    【讨论】:

    • 谢谢@JustAnil 我试过你的代码,但不幸的是它在这里和那里得到了对齐。我添加了关于这个的截图我编辑了这个问题在添加你的 jsfiddle 脚本后看看截图
    • 嗯,对我来说似乎适用于所有浏览器,所有点击都以窗口为中心。你能在你的网站上试试吗?也许它只是一个 JSfiddle 问题?否则我帮不了你,它似乎对我来说很好,只是修补 javascript,逻辑似乎很好。
    • 查看您的 Jsfiddle 如果您刷新并单击第二张图像,它会显示我在屏幕截图中提到的内容,然后再次单击它会显示在中心。 jsfiddle 中是否发生任何事情或该脚本仅执行此问题
    • 在我的小提琴中,我提到的是,如果你深深地注意到,当你点击第二张图片时,它会略微向下显示。点击后它会显示中间
    • 删除console.log 语句,它们仅用于调试。您可以将变量打印到console.log 并在您的检查器中查看它们,这就是我使用它们的全部,您可以安全地删除它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 2010-11-05
    • 1970-01-01
    相关资源
    最近更新 更多