2017-01-31 6 views
3

Мой код был очень запутанным, прежде чем я приношу свои извинения. Позвольте мне сделать это простым и удобочитаемым. В принципе, я хочу, чтобы при нажатии любого из 4 разделов он переходит в полноэкранный режим. И затем, когда вы нажмете его снова, он закрывает/переходит обратно в исходное положение. Это отлично работает для div 4, но первые 3 divs задают проблемы перехода при закрытии. В случае первого div, когда он закрывается, переход вообще отсутствует. Надеюсь, это понятно, и любая помощь будет оценена по достоинству. Ниже приведен мой упрощенный код.Как я могу дать divs равную силу позиции

  <!DOCTYPE html> 
      <html xmlns="http://www.w3.org/1999/xhtml"> 
      <head> 
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
       <title>layout</title> 
       <style> 
       html, body 
       { 
        height: 100%; 
        padding: 0; 
        margin: 0; 
        font-family:'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif; 
        color:black; 
        font-size:300%; 

       } 

       #div1 
       { 
        background-color:red; 
        position:absolute; 
        transition:0.3s; 
        top:0; 
        left:0; 
        width: 50%; 
         height: 50%; 
       } 

       #div2 
       { 
        background-color:blue; 
        position:absolute; 
        top:0; 
        right:0; 
        transition:0.3s; 
        width: 50%; 
         height: 50%; 
       } 

       #div3 
       { 
        background-color:green; 
        position:absolute; 
        bottom:0; 
        left:0; 
        transition:0.3s; 
        width: 50%; 
         height: 50%; 
       } 

       #div4 
       { 
        background-color:yellow; 
        position:absolute; 
        bottom:0; 
        right:0; 
        transition:0.3s; 
        width: 50%; 
         height: 50%; 
       } 


       #div1.fullscreen 
       { 
        z-index: 9999; 
        width: 100%; 
        height: 100%; 
        position: fixed; 
        top: 0; 
        left: 0; 
       } 
       #div2.fullscreen 
       { 
        z-index: 9999; 
        width: 100%; 
        height: 100%; 
        position: fixed; 
        top: 0; 
        right: 0; 
       } 
       #div3.fullscreen 
       { 
        z-index: 9999; 
        width: 100%; 
        height: 100%; 
        position: fixed; 
        bottom: 0; 
        left: 0; 
       } 
       #div4.fullscreen 
       { 
        z-index: 9999; 
        width: 100%; 
        height: 100%; 
        position: fixed; 
        bottom: 0; 
        right: 0; 
       } 

       </style> 
      </head> 
      <body> 

        <div id="div1"> 
         div 1 
        </div> 

        <div id="div2"> 
         div 2 
        </div> 

        <div id="div3"> 
         div 3 
        </div> 

        <div id="div4"> 
         div 4 
        </div> 



       <script> 
        $('#div1').click(function (e) { 
         $('#div1').toggleClass('fullscreen'); 
        }); 

        $('#div2').click(function (e) { 
         $('#div2').toggleClass('fullscreen'); 
        }); 

        $('#div3').click(function (e) { 
         $('#div3').toggleClass('fullscreen'); 
        }); 

        $('#div4').click(function (e) { 
         $('#div4').toggleClass('fullscreen'); 
        }); 
       </script> 

      </body> 
      </html> 
+0

Ничто не полный экран, когда я нажимаю на что-либо, и текст белого цвета, так что я ничего не вижу. Можете ли вы обновить свою демоверсию, чтобы было легко воспроизвести поведение? –

ответ

0

Не получил это из предварительного просмотра кода, но позвольте мне немного сократить ваш код. Итак, что мы здесь получаем. Ваш контейнер #center - z-index: 15;, поэтому он всегда над другими.

Даже если мы изменим его z-index: 0; у нас проблема: каждый ваш DIV внутри строкового элемента a и потому ДИВ это абсолютно позиционирован они не влияют на родителей a размер элемента. Каждый a имеет размер 0px × 0px и расположен в начале страницы.

Параметры Float ничего не дает для absolute 'ly позиционированных элементов, потому что absolute принимает элемент из потока элементов. Что мы должны сделать, чтобы он работал так, как вы хотите?

Прежде всего - сделать родителя a элемент ценный: изменить имущество div {} до a {}.

enter image description here

Второй один, у меня вопрос, почему мы уверены в размерах внутреннего содержания и фона размер Div в: изменить background-size: 100% 100%; к background-size: cover;, чтобы выполнить ваши правильно плавающей a -элементов с div-х :

div { width: 100%; height: 100%;} 

Потому что у нас нет никакого position свойства adiv -элементов s выполнит всю страницу. Позволяет изменить a -положения свойства relative

a {position: relative;} 

Итак, теперь у нас есть это для внутренней точки зрения диапазона: enter image description here

#center {z-index: 0} 
a {z-index: 1;} 

Моего последним вопрос и последняя изменение коды: что вы хотите делать в окончательной реализации? Ваш контейнер контейнера #center будет работать, если никакие элементы выше не будут пересекаться с ним. Если вам нужен эффект наведения элемента раздела, вы должны разместить span внутри #center.

Попробуйте описать цели, и я постараюсь вам помочь.

$('#div1, #div2, #div3, #div4').click(function(e) { 
 
    $(e.target).toggleClass('fullscreen'); 
 
    $(e.target).find('p').fadeToggle(200); 
 
});
html, 
 
body { 
 
    height: 100%; 
 
    padding: 0; 
 
    margin: 0; 
 
    font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif; 
 
} 
 
a { 
 
    position: relative; 
 
    z-index: 1; 
 
    width: 50%; 
 
    height: 50%; 
 
    float: left; 
 
} 
 
div { 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 
#div1, 
 
#div2, 
 
#div3, 
 
#div4 { 
 
    background-size: 100% 100%; 
 
    background-position: center; 
 
    opacity: 0.7; 
 
    transition: all .5s ease; 
 
    position: absolute; 
 
} 
 
#div1 { 
 
    background-image: url("1.jpg"); 
 
} 
 
#div2 { 
 
    background-image: url("2.jpg"); 
 
} 
 
#div3 { 
 
    background-image: url("3.jpg"); 
 
} 
 
#div4 { 
 
    background-image: url("4.jpg"); 
 
} 
 
#div1:hover, 
 
#div2:hover, 
 
#div3:hover, 
 
#div4:hover { 
 
    opacity: 1.0; 
 
    background-size: 120% 120%; 
 
} 
 
.fullscreen { 
 
    z-index: 9999; 
 
    width: 100%; 
 
    height: 100%; 
 
    position: fixed; 
 
    top: 0; 
 
    left: 0; 
 
} 
 
#center { 
 
    position: absolute; 
 
    width: 600px; 
 
    height: 300px; 
 
    z-index: 0; 
 
    top: 50%; 
 
    left: 50%; 
 
    margin: -150px 0 0 -300px; 
 
    background: #706d6d; 
 
    transition: 0.3s; 
 
} 
 
#center:hover { 
 
    background: #232222; 
 
} 
 
#home { 
 
    width: 80%; 
 
    height: 80%; 
 
    color: white; 
 
    margin-left: 10%; 
 
    text-align: justify; 
 
    margin-top: 5%; 
 
    border-bottom: 3px solid white; 
 
    border-top: 3px solid white; 
 
} 
 
.words { 
 
    width: 100%; 
 
    margin-top: 25vh; 
 
    text-align: center; 
 
    color: white; 
 
    font-size: 300%; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<a href="#"> 
 
    <div id="div1"> 
 

 
    <div class="words"> 
 
     <span> section 1</span> 
 
     <p id="p1" style="display:none;font-size:50%">Lorem Ipsum is simply dummy text of the printing and 
 
     <br/>typesetting industry. Lorem Ipsum has been the industry's standard dummy 
 
     <br/>text ever since the 1500s, when an unknown printer took</p> 
 
    </div> 
 

 
    </div> 
 
</a> 
 
<a href="#"> 
 
    <div id="div2"> 
 
    <div class="words"> 
 
     <span> section 2</span> 
 
     <p id="p2" style="display:none;font-size:50%">Lorem Ipsum is simply dummy text of the printing and 
 
     <br />typesetting industry. Lorem Ipsum has been the industry's standard dummy 
 
     <br />text ever since the 1500s, when an unknown printer took</p> 
 
    </div> 
 
    </div> 
 
</a> 
 
<a href="#"> 
 
    <div id="div3"> 
 
    <div class="words"> 
 
     <span> section 3</span> 
 
     <p id="p3" style="display:none;font-size:50%">Lorem Ipsum is simply dummy text of the printing and 
 
     <br />typesetting industry. Lorem Ipsum has been the industry's standard dummy 
 
     <br />text ever since the 1500s, when an unknown printer took</p> 
 
    </div> 
 
    </div> 
 
</a> 
 
<a href="#"> 
 
    <div id="div4"> 
 
    <div class="words"> 
 
     <span> section 4</span> 
 
     <p id="p4" style="display:none;font-size:50%">Lorem Ipsum is simply dummy text of the printing and 
 
     <br />typesetting industry. Lorem Ipsum has been the industry's standard dummy 
 
     <br />text ever since the 1500s, when an unknown printer took</p> 
 
    </div> 
 
    </div> 
 
</a> 
 
<div id="center"> 
 
    <div id="home"> 
 
    <h3 style="text-align:center">HOME</h3> 
 
    <label>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It 
 
     has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop 
 
     publishing software like Aldus PageMaker including versions of Lorem Ipsum</label> 
 
    </div> 
 
</div>