2016-12-03 42 views
0

我正在使用CSS3和交替類與jQuery的模態。用css3過渡動畫 - 打開並關閉

模態打開正確(與css3效果),但我的麻煩是當我關閉模態。我無法以相反的方式使模式與css3效果接近。換句話說,我想打開帶有效果的模態,並以相反的方式關閉模態。

模態打開0%: -70deg%´ ---- ´100%: 0。我嘗試創建一個名爲closemodalanimation,具有相反的效果。 0%: 0 ---- 100%: -70deg但沒有成功。

如何打開和關閉出現效果的模式...效果出來?

PLS,看起來片段:

$(document).ready(function(){ 
 
\t $('.modal-open').click(function(){ 
 
\t \t $('.overlay').addClass('modal-show'); 
 
\t \t $('.modal').addClass('modal-show modal-perspective'); 
 
\t \t $('.modal-content').addClass('modal-animate'); 
 
\t }); 
 
\t $('.modal-close').click(function(){ 
 
\t \t $('.overlay').removeClass('modal-show'); 
 
\t \t $('.modal').removeClass('modal-show modal-perspective'); 
 
\t \t $('.modal-content').removeClass('modal-animate'); 
 
\t  $('.modal-content').addClass('modal-remove'); 
 

 
\t }); 
 
});
html, body{ 
 
\t margin: 0; 
 
\t padding: 0; 
 
\t box-sizing: border-box; 
 
\t background: #FF3300; 
 
\t font-family: 'Ubuntu', sans-serif; 
 
} 
 

 
.modal{ 
 
\t position: fixed; 
 
\t top: 50%; 
 
\t left: 50%; 
 
\t transform: translate(-50%, -50%); 
 
\t width: 50%; 
 
\t height: auto; 
 
\t min-width: 300px; \t 
 
\t z-index: 2; 
 
\t visibility: hidden; \t 
 
\t display: flex; 
 
\t align-items: center; 
 
\t justify-content: center; 
 

 
} 
 

 
.modal-content{ 
 
\t padding: 120px 10px; 
 
\t text-align: center; 
 
\t color: #000; 
 
\t background: #bd330f; 
 
\t border-radius: 10px; \t 
 
\t border: 1px solid #909090; \t 
 
\t box-shadow: 10px 10px 20px rgba(0, 0, 0, 0.9); \t 
 
} 
 

 
.modal-open{ 
 
\t position: absolute; 
 
\t top: 50%; 
 
\t left: 50%; 
 
\t transform: translate(-50%, -50%); 
 
\t border: transparent; 
 
\t background: #fff; 
 
\t font-size: 15px; 
 
\t font-weight: bold; 
 
\t padding: 20px 50px; 
 
\t border-radius: 5px; 
 
} 
 

 
.modal-open:active{ 
 
\t outline: transparent; 
 
} 
 

 
.modal-close{ 
 
\t padding: 8px 40px; 
 
    border-radius: 8px; 
 
    border: transparent; 
 
    background: #212121; 
 
    color: #fff; 
 
    outline: transparent; \t 
 
} 
 

 
.overlay { 
 
\t position: fixed; 
 
\t width: 100%; 
 
\t height: 100%; 
 
\t visibility: hidden; 
 
\t top: 0; 
 
\t left: 0; 
 
\t z-index: 1; 
 
\t opacity: 0; 
 
\t background: rgba(0, 0, 0, 0.5); 
 
\t transition: all 0.7s; 
 
} 
 

 
.modal-show { 
 
\t opacity: 1; 
 
\t visibility: visible; \t 
 
} 
 

 
.modal-perspective{ 
 
\t  -webkit-perspective: 900px; 
 
\t  perspective: 900px; \t \t \t 
 
} 
 

 
.modal-animate{ 
 
\t animation: openmodal 300ms ease-in forwards; \t 
 
} 
 

 
.modal-remove{ 
 
\t animation: closemodal 300ms ease-in-out forwards; \t 
 
} 
 

 
@keyframes openmodal{ 
 
\t 0%{ 
 
\t \t transform: rotateX(-70deg); 
 
\t \t -webkit-transform: rotateX(-70deg); 
 
\t \t -moz-transform: rotateX(-70deg); 
 
\t \t -ms-transform: rotateX(-70deg); 
 
\t } \t 
 

 
\t 100%{ 
 
\t \t transform: rotateX(0deg); 
 
\t \t -webkit-transform: rotateX(0deg); 
 
\t \t -moz-transform: rotateX(0deg); 
 
\t \t -ms-transform: rotateX(0deg); 
 
\t } 
 
} 
 

 
@keyframes closemodal{ 
 
\t 0%{ 
 
\t \t transform: rotateX(0deg); 
 
\t \t -webkit-transform: rotateX(0deg); 
 
\t \t -moz-transform: rotateX(0deg); 
 
\t \t -ms-transform: rotateX(0deg); 
 
\t } \t 
 

 
\t 100%{ 
 
\t \t transform: rotateX(-70deg); 
 
\t \t -webkit-transform: rotateX(-70deg); 
 
\t \t -moz-transform: rotateX(-70deg); 
 
\t \t -ms-transform: rotateX(-70deg); \t \t 
 
\t } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="modal"> 
 
\t <div class="modal-content"> 
 
\t \t <h3>Modal Dialog</h3> 
 
\t \t <div> \t \t \t \t 
 
\t \t \t <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi gravida libero nec velit. Morbi scelerisque luctus velit. Etiam dui sem, fermentum vitae, sagittis id, malesuada in, quam. Proin mattis lacinia justo. Vestibulum facilisis auctor urna. Aliquam in lorem sit amet leo accumsan.</p> 
 
\t \t \t <button class="modal-close">CLOSE ME!</button> 
 
\t \t </div> 
 
\t </div> 
 
</div> 
 

 
<button class="modal-open">~ OPEN MODAL ~</button> 
 

 
<div class="overlay"></div>

此外,在我的jQuery的代碼中,有一個更好的方式來切換類?我認爲這是切換它們的更好方法。我如何做到這一點?

+0

對於類切換,http://api.jquery.com/toggleclass/可能是你正在尋找的。 – Gerrit0

回答

2

當您需要反向效果時,您必須避免使用CSS3關鍵幀動畫。你真正應該使用(和需要)的是CSS3轉換,只要刪除了添加前向效果的類,就可以產生反向效果。

只需設置transform: rotateX(-70deg)到原來的狀態(即.modal-content),然後在打開狀態(.modal-animate)設置transform: rotateX(0deg)。這意味着元素最初將不可見,並在打開時顯示。最後,將transition屬性添加到元素。

注:正如評論和this post它一般是不會好於transition使用all但在這個例子中的影響是很小的,我認爲討論。)

$(document).ready(function() { 
 
    $('.modal-open').click(function() { 
 
    $('.overlay').addClass('modal-show'); 
 
    $('.modal').addClass('modal-show modal-perspective'); 
 
    $('.modal-content').addClass('modal-animate'); 
 
    }); 
 
    $('.modal-close').click(function() { 
 
    $('.overlay').removeClass('modal-show'); 
 
    $('.modal').removeClass('modal-show modal-perspective'); 
 
    $('.modal-content').removeClass('modal-animate'); 
 
    }); 
 
});
html, 
 
body { 
 
    margin: 0; 
 
    padding: 0; 
 
    box-sizing: border-box; 
 
    background: #FF3300; 
 
    font-family: 'Ubuntu', sans-serif; 
 
} 
 
.modal { 
 
    position: fixed; 
 
    top: 50%; 
 
    left: 50%; 
 
    transform: translate(-50%, -50%); 
 
    width: 50%; 
 
    height: auto; 
 
    min-width: 300px; 
 
    z-index: 2; 
 
    visibility: hidden; 
 
    display: flex; 
 
    align-items: center; 
 
    justify-content: center; 
 
} 
 
.modal-content { 
 
    padding: 120px 10px; 
 
    text-align: center; 
 
    color: #000; 
 
    background: #bd330f; 
 
    border-radius: 10px; 
 
    border: 1px solid #909090; 
 
    box-shadow: 10px 10px 20px rgba(0, 0, 0, 0.9); 
 
    
 
    transition: all 300ms ease-in; 
 
    
 
    /* added for original state - vendor prefixed version should come before unprefixed property */ 
 
    -webkit-transform: rotateX(-70deg); 
 
    -moz-transform: rotateX(-70deg); 
 
    -ms-transform: rotateX(-70deg); 
 
    transform: rotateX(-70deg); 
 
} 
 
.modal-open { 
 
    position: absolute; 
 
    top: 50%; 
 
    left: 50%; 
 
    transform: translate(-50%, -50%); 
 
    border: transparent; 
 
    background: #fff; 
 
    font-size: 15px; 
 
    font-weight: bold; 
 
    padding: 20px 50px; 
 
    border-radius: 5px; 
 
} 
 
.modal-open:active { 
 
    outline: transparent; 
 
} 
 
.modal-close { 
 
    padding: 8px 40px; 
 
    border-radius: 8px; 
 
    border: transparent; 
 
    background: #212121; 
 
    color: #fff; 
 
    outline: transparent; 
 
} 
 
.overlay { 
 
    position: fixed; 
 
    width: 100%; 
 
    height: 100%; 
 
    visibility: hidden; 
 
    top: 0; 
 
    left: 0; 
 
    z-index: 1; 
 
    opacity: 0; 
 
    background: rgba(0, 0, 0, 0.5); 
 
    transition: all 0.7s; 
 
} 
 
.modal-show { 
 
    opacity: 1; 
 
    visibility: visible; 
 
} 
 
.modal-perspective { 
 
    -webkit-perspective: 900px; 
 
    perspective: 900px; 
 
} 
 
.modal-animate { 
 
    transition: all 300ms ease-in-out; 
 
    /* remove animation, set below for open state */ 
 
    -webkit-transform: rotateX(0deg); 
 
    -moz-transform: rotateX(0deg); 
 
    -ms-transform: rotateX(0deg); 
 
    transform: rotateX(0deg); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="modal"> 
 
    <div class="modal-content"> 
 
    <h3>Modal Dialog</h3> 
 
    <div> 
 
     <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi gravida libero nec velit. Morbi scelerisque luctus velit. Etiam dui sem, fermentum vitae, sagittis id, malesuada in, quam. Proin mattis lacinia justo. Vestibulum facilisis auctor urna. 
 
     Aliquam in lorem sit amet leo accumsan.</p> 
 
     <button class="modal-close">CLOSE ME!</button> 
 
    </div> 
 
    </div> 
 
</div> 
 

 
<button class="modal-open">~ OPEN MODAL ~</button> 
 

 
<div class="overlay"></div>

+1

加1 ...根據自己的經驗和根據[本文](http://stackoverflow.com/a/8962837/2827823),不推薦使用'transition:all'。你對此有何看法? – LGSon

+0

@LGSon:是的,這是正確的。感謝這裏的鏈接,因爲OP需要知道它。但我認爲在這個例子中使用'all'的影響很小。 – Harry

+0

感謝您的幫助@哈利!只添加和刪除'transform:rotate()'。很簡單!請回答我一個問題:爲什麼我必須避免使用關鍵幀來產生反向效應?它僅適用於模態或一般方式? – Zkk