Javascript.RU

Создать новую тему Ответ
 
Опции темы Искать в теме
  #1 (permalink)  
Старый 08.08.2016, 14:03
Аватар для Black_Star
Профессор
Отправить личное сообщение для Black_Star Посмотреть профиль Найти все сообщения от Black_Star
 
Регистрация: 11.07.2016
Сообщений: 300

Центрирование pop-up окна
Добрый день уважаемые, вот столкнулся с вопросом, как сделать так что б при нажатии на объект выскакивало попап окошко по центру экрана в не зависимости от скролинга и размеров экрана устройства с которого оно будет проматриватся.
Я написал вот такое.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<style>
.field {
    display: block;
    position: relative;
    background: #0C0C0E no-repeat 50% 50%;
    width: 200px;
    height: 600px;
    margin: 1%;
    border-radius: 5px;
    box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.2);
    font-family: Arial, sans-serif;
    font-size: 1.2em;
    line-height: 1.5;
}

.popup-box {
    position: absolute;
    width: 50px;
    height: 50px;
    border-radius: 5px;
    background: rgba(0, 0, 0, 0.5);
    box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
    font-family: Arial, sans-serif;
    z-index: 9999999;
    font-size: 14px;
    border: 2px solid white;
    display: none;
}

#blackout {
    background: rgba(0, 0, 0, 0.7);
    position: absolute;
    top: 0;
    overflow: hidden;
    z-index: 999999;
    left: 0;
    display: none;
}

#tetra {
    width: 40px;
    height: 40px;
    background-color: green;
}
</style>

<body>
    <div class="field">
        <div id='tetra'></div>
    </div>
    <div class="popup-box"></div>
    <script>
    $('body').append('<div id="blackout"></div>');
    var boxWidth = 600; //// как отцентрировать ?

    function centerBox() {

        /* определяем нужные данные */
        var winWidth = $(window).width();
        var winHeight = $(document).height();
        var scrollPos = $(window).scrollTop();

        /* Вычисляем позицию */

        var disWidth = (winWidth - boxWidth) / 2
        var disHeight = scrollPos + 50;

        /* Добавляем стили к блокам */
        $('.popup-box').css({
            'left': disWidth + 'px',
            'top': disHeight + 'px'
        });
        $('#blackout').css({
            'width': winWidth + 'px',
            'height': winHeight + 'px'
        });

        return false;
    }
    centerBox();


    function showPopUpBox() {

        $('#tetra').on('click', (function() {


            $('.popup-box ').toggle();
            $('#blackout').toggle();

        }))



    }
    showPopUpBox();
    </script>
</body>

</html>

Это эскизный набросок, выдранный из проекта, но у меня почему то оно как-то не правильно отображается.
Ответить с цитированием
  #2 (permalink)  
Старый 08.08.2016, 14:18
Аватар для рони
Профессор
Отправить личное сообщение для рони Посмотреть профиль Найти все сообщения от рони
 
Регистрация: 27.05.2010
Сообщений: 33,068

Black_Star,

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<style>
html, body{
  height: 98%;
}

.field {
    display: block;
    position: relative;
    background: #0C0C0E no-repeat 50% 50%;
    width: 200px;
    height: 600px;
    margin: 1%;
    border-radius: 5px;
    box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.2);
    font-family: Arial, sans-serif;
    font-size: 1.2em;
    line-height: 1.5;
}

.popup-box {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    width: 50px;
    height: 50px;
    border-radius: 5px;
    background: rgba(0, 0, 0, 0.5);
    box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
    font-family: Arial, sans-serif;
    z-index: 9999999;
    font-size: 14px;
    border: 2px solid white;
    display: none;

}

#blackout { background: rgba(0, 0, 0, 0.7);
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    width: 100%;
    height: 100%;display: none;
}

#tetra {
    width: 40px;
    height: 40px;
    background-color: green;
}
</style>

<body>
    <div class="field">
        <div id='tetra'></div>
    </div>
    <div class="popup-box"></div>
    <script>
    $('body').append('<div id="blackout"></div>');
    $('#tetra, #blackout').on('click', (function() {
            $('.popup-box ').toggle();
            $('#blackout').toggle();
        }))



    </script>
</body>

</html>
Ответить с цитированием
Ответ



Опции темы Искать в теме
Искать в теме:

Расширенный поиск


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
FancyBox: Размер модального окна больше размеров окна браузера (высота), как сделать? clgs Events/DOM/Window 3 22.01.2015 17:13
затемнение по высоте страницы, а не окна qazibum Общие вопросы Javascript 4 07.09.2012 09:37
Не получается получить document открытого окна Dim@ Общие вопросы Javascript 1 20.05.2012 15:52
Помогите с настройкой лайтбокс окна obormot Элементы интерфейса 1 03.02.2012 16:14
Плавное открытие окна. Flashton Элементы интерфейса 3 20.10.2010 16:00