jQuery 和 CSS 将元素定位在屏幕中央

发布于 2024-12-08 18:41:58 字数 5729 浏览 0 评论 0原文

标题听起来很简单,但我保证事实并非如此。我有一个网站 http://sarahnwatson.com。我有一个模态窗口,可以通过单击“关于”和“联系”选项卡来打开。这些选项卡都位于位于左侧的无序列表中。模态窗口直接编码在应该打开它的选项卡下方。

我似乎无法弄清楚如何在 jQuery 代码和 CSS 代码之间将不同大小的模式窗口放置在屏幕中央。因为它们位于左侧的元素内,所以它们尝试留在该元素中。

这是选项卡和模态窗口的 CSS:

ul.st_navigation{
    position:relative;
    width:100%;
    top:140px;
    left:-300px;
    list-style:none;
}
ul.st_navigation li {
    float:left;
    clear:both;
    margin-bottom:8px;
    position:relative;
    width:100%;
}
ul.st_navigation li span.st_link{
    background: rgba(0,0,0,.8);
    float:left;
    position:relative;
    line-height:50px;
    padding:0px 20px;
    -moz-box-shadow:0px 0px 2px #000;
    -webkit-box-shadow:0px 0px 2px #000;
    box-shadow:0px 0px 2px #000;
}
ul.st_navigation li span.st_arrow_down,
ul.st_navigation li span.st_arrow_up{
    position:absolute;
    margin-left:20px;
    width:40px;
    height:50px;
    cursor:pointer;
    -moz-box-shadow:0px 0px 2px #000;
    -webkit-box-shadow:0px 0px 2px #000;
    box-shadow:0px 0px 2px #000;
}
ul.st_navigation li span.st_arrow_down{
    background: rgba(0,0,0,.8) url(../images/icons/down.png) no-repeat center center;
}
ul.st_navigation li span.st_arrow_up{
    background: rgba(0,0,0,.8) url(../images/icons/up.png) no-repeat center center;
}
ul.st_navigation li span.st_modal{
    position:absolute;
    margin-left:20px;
    width:40px;
    height:50px;
    cursor:pointer;
    -moz-box-shadow:0px 0px 2px #000;
    -webkit-box-shadow:0px 0px 2px #000;
    box-shadow:0px 0px 2px #000;
    background: rgba(0,0,0,.8) url(../images/icons/modal.png) no-repeat center center;
}

这是 jQuery:

// Create a modal window
                function createModal(elm) {
                    var $this = $(this);
                    var $body = $('body');
                    var winHeight = $(window).height();
                    var winWidth = $(window).width();

                    $body.prepend('<div id="blackout">');
                    $("#blackout").fadeIn(1000, function() {

                    $(elm).css({ left: '-9999px', display: 'block' });

                        var modalHeight = $(elm).height();
                        var modalWidth = $(elm).width();

                        var offsetH1 = winHeight/2;
                        var offsetH2 = (winHeight-modalHeight)/2;
                        var offsetW = (winWidth-modalWidth)/2;

                        $(elm)
                        .css({ top:offsetH1, left:offsetW, height:'0px' })
                        .animate({ top:offsetH2, height:modalHeight });



                        });
                    });

                    });

                }

                $list.find('.st_modal').live('click',function(){
                    var $this = $(this);
                    hideThumbs();
                    $this.closest('li').animate({ left: "0px" }, function() {
                        var $elem = $this.parent().next('div#modal_window');
                        createModal($elem);
                    });
                });

HTML 结构如下:

<ul id="st_nav" class="st_navigation">
                <li>
                    <span class="st_link">About<span class="st_modal"></span></span>
                    <div id="modal_window"  style="width:600px;">
                    <div class="about">
                        <span class="line"><span class="left">Name: </span>Sarah Watson</span>
                        <span class="line"><span class="left">Age: </span>16</span>
                        <span class="line"><span class="left">Genre: </span>Folk, Pop, Alternative</span>
                        <span class="line"><span class="left">Bio: </span></span><br />
                        I am 16yrs old and an aspiring singer/songwriter. I live in Northern California. My goals and ambitions in life are to someday have a popular song on the radio, whether I am singing it or someone else is. I love to hear just about any kind of music, learning something new is always fun!
                    </div>
                    </div>
                </li>
                <li>
                <span class="st_link">Contact<span class="st_modal"></span></span>
                    <div id="modal_window" style="width: 450px;">
                        <h2>Contact</h2>
                        <form id="contact_form" method="POST" action="#">
                        <label>Name:</label><br />
                        <input type="text" name="name" /><br />
                        <label>Email:</label><br />
                        <input type="text" name="email" /><br />
                        <label>Reason:</label> &nbsp;
                        <label><input type="radio" name="reason" value="praise" checked='checked' /> Praise</label> <label><input type="radio" name="reason" value="booking" /> Booking</label><br />
                        <label>Message:</label><br />
                        <textarea name="name"></textarea><br />
                        <input type="submit" value="Submit" name="submit" /> <span class="status_message"></span>
                        </form>
                    </div>
                </li>
            </ul>

The title makes it sound easy, but I guarantee it's not. I have a site at http://sarahnwatson.com. I have modal windows that I have gotten to open by clicking on the tabs About and Contact. The tabs are all in an unordered list that is positioned to the left. The Modal window is coded in directly under the tab that is supposed to open it.

What I can't seem to figure out is how to, between my jQuery code and my CSS code, position the different sized modal windows in the center of the screen. Because they are within an element that is positioned to the left, they try to stay in that element.

Here is the CSS for the tabs and the modal window:

ul.st_navigation{
    position:relative;
    width:100%;
    top:140px;
    left:-300px;
    list-style:none;
}
ul.st_navigation li {
    float:left;
    clear:both;
    margin-bottom:8px;
    position:relative;
    width:100%;
}
ul.st_navigation li span.st_link{
    background: rgba(0,0,0,.8);
    float:left;
    position:relative;
    line-height:50px;
    padding:0px 20px;
    -moz-box-shadow:0px 0px 2px #000;
    -webkit-box-shadow:0px 0px 2px #000;
    box-shadow:0px 0px 2px #000;
}
ul.st_navigation li span.st_arrow_down,
ul.st_navigation li span.st_arrow_up{
    position:absolute;
    margin-left:20px;
    width:40px;
    height:50px;
    cursor:pointer;
    -moz-box-shadow:0px 0px 2px #000;
    -webkit-box-shadow:0px 0px 2px #000;
    box-shadow:0px 0px 2px #000;
}
ul.st_navigation li span.st_arrow_down{
    background: rgba(0,0,0,.8) url(../images/icons/down.png) no-repeat center center;
}
ul.st_navigation li span.st_arrow_up{
    background: rgba(0,0,0,.8) url(../images/icons/up.png) no-repeat center center;
}
ul.st_navigation li span.st_modal{
    position:absolute;
    margin-left:20px;
    width:40px;
    height:50px;
    cursor:pointer;
    -moz-box-shadow:0px 0px 2px #000;
    -webkit-box-shadow:0px 0px 2px #000;
    box-shadow:0px 0px 2px #000;
    background: rgba(0,0,0,.8) url(../images/icons/modal.png) no-repeat center center;
}

And here is the jQuery:

// Create a modal window
                function createModal(elm) {
                    var $this = $(this);
                    var $body = $('body');
                    var winHeight = $(window).height();
                    var winWidth = $(window).width();

                    $body.prepend('<div id="blackout">');
                    $("#blackout").fadeIn(1000, function() {

                    $(elm).css({ left: '-9999px', display: 'block' });

                        var modalHeight = $(elm).height();
                        var modalWidth = $(elm).width();

                        var offsetH1 = winHeight/2;
                        var offsetH2 = (winHeight-modalHeight)/2;
                        var offsetW = (winWidth-modalWidth)/2;

                        $(elm)
                        .css({ top:offsetH1, left:offsetW, height:'0px' })
                        .animate({ top:offsetH2, height:modalHeight });



                        });
                    });

                    });

                }

                $list.find('.st_modal').live('click',function(){
                    var $this = $(this);
                    hideThumbs();
                    $this.closest('li').animate({ left: "0px" }, function() {
                        var $elem = $this.parent().next('div#modal_window');
                        createModal($elem);
                    });
                });

The HTML structure is as follows:

<ul id="st_nav" class="st_navigation">
                <li>
                    <span class="st_link">About<span class="st_modal"></span></span>
                    <div id="modal_window"  style="width:600px;">
                    <div class="about">
                        <span class="line"><span class="left">Name: </span>Sarah Watson</span>
                        <span class="line"><span class="left">Age: </span>16</span>
                        <span class="line"><span class="left">Genre: </span>Folk, Pop, Alternative</span>
                        <span class="line"><span class="left">Bio: </span></span><br />
                        I am 16yrs old and an aspiring singer/songwriter. I live in Northern California. My goals and ambitions in life are to someday have a popular song on the radio, whether I am singing it or someone else is. I love to hear just about any kind of music, learning something new is always fun!
                    </div>
                    </div>
                </li>
                <li>
                <span class="st_link">Contact<span class="st_modal"></span></span>
                    <div id="modal_window" style="width: 450px;">
                        <h2>Contact</h2>
                        <form id="contact_form" method="POST" action="#">
                        <label>Name:</label><br />
                        <input type="text" name="name" /><br />
                        <label>Email:</label><br />
                        <input type="text" name="email" /><br />
                        <label>Reason:</label>  
                        <label><input type="radio" name="reason" value="praise" checked='checked' /> Praise</label> <label><input type="radio" name="reason" value="booking" /> Booking</label><br />
                        <label>Message:</label><br />
                        <textarea name="name"></textarea><br />
                        <input type="submit" value="Submit" name="submit" /> <span class="status_message"></span>
                        </form>
                    </div>
                </li>
            </ul>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话墙 2024-12-15 18:41:58

找到 li 与窗口的相对位置并平移模态:

 $this.closest('li').animate({ left: "0px" }, function() {
var parentLiModalPos = $(this).position();
                        var $elem = $this.parent().next('div#modal_window');
                        createModal($elem, parentLiModalPos.top);
                    });


function createModal(elm, parentLiTop) {
                    var $this = $(this);
                    var $body = $('body');
                    var winHeight = $(window).height();
                    var winWidth = $(window).width();

                    $body.prepend('<div id="blackout">');
                    $("#blackout").fadeIn(1000, function() {

                    $(elm).css({ left: '-9999px', display: 'block' });

                        var modalHeight = $(elm).height();
                        var modalWidth = $(elm).width();

                        var offsetH1 = winHeight/2;
                        var offsetH2 = (winHeight-modalHeight)/2;
                        var offsetW = (winWidth-modalWidth)/2;

                        $(elm)
                        .css({ top:offsetH1 - parentLiTop, left:offsetW, height:'0px' })
                        .animate({ top:offsetH2, height:modalHeight });



                        });
                    });

                    });

                }

Find the relative position of the li to the window and translate the modal:

 $this.closest('li').animate({ left: "0px" }, function() {
var parentLiModalPos = $(this).position();
                        var $elem = $this.parent().next('div#modal_window');
                        createModal($elem, parentLiModalPos.top);
                    });


function createModal(elm, parentLiTop) {
                    var $this = $(this);
                    var $body = $('body');
                    var winHeight = $(window).height();
                    var winWidth = $(window).width();

                    $body.prepend('<div id="blackout">');
                    $("#blackout").fadeIn(1000, function() {

                    $(elm).css({ left: '-9999px', display: 'block' });

                        var modalHeight = $(elm).height();
                        var modalWidth = $(elm).width();

                        var offsetH1 = winHeight/2;
                        var offsetH2 = (winHeight-modalHeight)/2;
                        var offsetW = (winWidth-modalWidth)/2;

                        $(elm)
                        .css({ top:offsetH1 - parentLiTop, left:offsetW, height:'0px' })
                        .animate({ top:offsetH2, height:modalHeight });



                        });
                    });

                    });

                }
打小就很酷 2024-12-15 18:41:58

我无法真正确定问题出在哪里,但看起来居中的计算是正确的。

我会首先编写正确的代码,而不是尝试在下一个 div 中创建一个模式,其中 id 为“modal_window”将是开始的地方,因为你不能有多个具有相同名称的 ID,并尝试当有两个同名的 ID 时,在 ID 上使用 jQuery 函数可能会出现问题。

你让事情变得比实际情况复杂得多。您的屏幕左侧有一个 li 元素,当您单击该 li 元素(或更具体地说 li 中的跨度)时,您希望 li 元素在屏幕左侧进行动画处理,同时为模态进入屏幕。

您应该为每个模态创建一个具有唯一 id 的 div,并且不要将这些模态放置在您尝试设置动画的 li 元素的结构内,该元素也是您尝试以不同方向设置动画的模态的父级。现在的情况是,模态框位于 li 元素内部,这使得一切都变得更加困难,因为按钮父级(以及模态父级)和模态动画不同,但模态框位于按钮父级内部。

I can't really pinpoint exactly where the problem is, but it looks like the calculations for centering are correct.

I would start by writing proper code, and not trying to create a modal in the next div whith an id of "modal_window" would be the place to start, as you can NOT have more than one ID with the same name, and trying to use jQuery functions on the ID could be problamatic when there are two ID's with the same name.

Your making this a lot more complicated than it has to be. You have a li element on the left side on your screen, when you click that li element (or more specifically a span within the li) you want the li element to animate to the left off the screen, and at the same time animate a modal into the screen.

You should create a div with a uniqe id for each modal, and NOT PLACE those modals inside the structure for the li element you are trying to animate, that is also the parent of the modal you are trying to animate in a different direction. The way it is now, your modals are inside the li elements, and this makes everything a lot harder to do as the buttons parent (and also the modals parent) and the modal animates differently but the modal is inside the buttons parent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文