基于视口的div定位

时间:2009-09-12 16:35:26

标签: jquery html positioning viewport

我想将div相对于浏览器窗口的视口定位。目前我有一些弹出窗口,其中一些jquery根据窗口大小动态定位,但由于它们是绝对定位的,因此它们基于页面顶部,因此当您向下滚动时,单击下方的项目页面,弹出窗口位于页面顶部,离开视口...

这可以在这里看到,特别是如果你点击“Redcat”项目。 http://www.samuelfarfsing.com/test.php

有没有办法将这些div相对于视口的当前位置定位?

HTML:

<div class="container">
    <div class="project">
    <a class="close">Close &times;</a>
    <img src="/img/lova_popup_slide01.jpg" width="500" height="530" alt="" />
    </div>
    <div class="description"><p>Description</p></div>
</div>

Jquery的:

$(document).ready(function() {
//Find & Open

$(".projectThumb").click(function(){
    $("#backgroundPopup").show();
        htmlName = $(this).find("img").attr("name");
        $("#data").load("/content/" + htmlName + ".html", null, function(){
            //Set Variables
            var container = $(".container");
            var project = $(".project");
            var popupWidth = container.find(".project img:first").width();
            var popupHeight = container.find(".project img:first").height()+35;
            var windowWidth = document.documentElement.clientWidth;
            var windowHeight = document.documentElement.clientHeight;

            //Set popup dimensions
            container.css("width" , popupWidth);
            container.css("height" , popupHeight);

            //Set popup CSS
            container.css({"position": "absolute", "background": "#000", "top": (windowHeight / 2) - (popupHeight / 2) + "px", "left": (windowWidth / 2) - (popupWidth / 2) + "px", "z-index": "2" });
            project.css({"width": (popupWidth), "height": (popupHeight) });

    //Slideshow Image to hide rest
            $(".container").each(function(){
            $(".project img", this).hide().filter(":first").show();
        });
    });
});

1 个答案:

答案 0 :(得分:1)

也许您正在寻找CSS { position: fixed }