2016-11-14 56 views
2

我想創建一個純CSS3幻燈片或多或少像這樣的:HTML圖片的標籤和列表標籤結合

Pure CSS Slideshow

我的實際HTML已經使用了<picture>標籤根據顯示設置特定的圖像港口的方向,縱向或橫向。 幻燈片教程使用<ul>和幾個<li>元素來設置幻燈片的圖片。

當我嘗試在我的<picture>元素內創建<ul><li>元素時,瀏覽器無法再找到圖像。

這裏是我的代碼部分:

HTML:

<div id="image"> 
    <picture> 
    <source media="all and (orientation: portrait)" srcset="images/faust_portrait.jpg"> 
    <source media="all and (orientation: landscape)" srcset="images/faust_landscape.jpg"> 
    <img src="images/faust_1024.jpg" alt="faust"> 
    </picture> 
</div> 

CSS:

#image { 
    width: 100%; 
    height: auto; 
    line-height: 0; 
    animation-name: move; 
    animation-duration: 1s; 
    animation-timing-function: ease-in-out; 
    animation-delay: 0.5s; 
    animation-iteration-count: 2; 
    animation-direction: alternate; 
} 

@keyframes move { 
    from { 
    transform: translateX(0); 
    } 
    to { 
    transform: translateX(100%); 
    } 
} 

#image img { 
    width: 100%; 
} 

所以在相應的畫面滑出屏幕,然後再返回的那一刻,一個時間。我想要可能有5張照片,全部取決於視口的方向,有2個版本)不斷滑動。它是網頁的某種標題。 有關如何做到這一點的任何建議?

回答

1

WHATWG's standard指示picture標籤內的內容應該是零個或多個源後跟img標籤(和任選地與腳本的支撐元件混合)。 olul標記不是列出的類型,因此不屬於picture標記。

picture標記替換爲olul標記;它是一張圖片列表,而不是一個列表的圖片(儘管我不能100%確定那個......)。這應該解決取向依賴圖像的問題。

至於幻燈片部分,你已經鏈接了一個非常詳細的教程。我會說跟隨,你最終應該得到期望的結果。在教程中,每個圖片都使用一個唯一的ID,您可以用選擇1到5的nth-child選擇器或任意數量的圖片替換它們。

請記住,雖然使用這種創建展示卷軸的方法並不是非常靈活,但當您希望添加/刪除圖像時,您需要重新制作每個圖像的時間。另外不要忘記,最後的圖像必須返回到最後才能保持平滑。

運行snippit時,通過選擇「完整頁面」,您可以通過調整瀏覽器大小來操作客戶端大小(以及方向)。希望這可以幫助。

.showreel 
 
{ 
 
    /* Allow for absolute positionng of the child elements.*/ 
 
    position:relative; 
 
    z-index:0; 
 
    overflow:hidden; 
 

 
    /* Treat "Landscape" as default.*/ 
 
    width:64px; 
 
    height:32px; 
 

 
    /* Remove annoying list-style formatting. */ 
 
    list-style:none; 
 
    padding:0; 
 
    margin:0; 
 
} 
 
/* Resize the showreel when in portait mode. */ 
 
@media (orientation:portrait){.showreel { width:32px; height:64px;}} 
 

 
.showreel li 
 
{ 
 
    position:absolute; 
 
    left:0; top:0; 
 
} 
 

 
/* Using the nth-child() selector instead of ids. */ 
 
.showreel li:nth-child(1){animation: picture_1 3s linear infinite;} 
 
.showreel li:nth-child(2){animation: picture_2 3s linear infinite;} 
 
/* Repeat a number of time as necessary. */  
 

 
/* Timings need to be tweaked for each new image in the reel. */ 
 
@keyframes picture_1 
 
{ 
 
    0%{top:0; z-index:1;} /* Show first image 1/4 of the time.*/ 
 
    25% {top:0; z-index:1;} /* Keeps it in place for the time being.*/ 
 
    50% {top:-100%; z-index:0;} /* Move it out of sight, and the other in. */ 
 
    75% {top:100%; z-index:0;} /* Return from the bottom. */ 
 
    100% {top:0; z-index:1;} /* In view once again. */ 
 
} 
 
@keyframes picture_2 
 
{ 
 
    0% {top:100%; z-index:0;} 
 
    25% {top:100%; z-index:0;} 
 
    50% {top:0; z-index:1;} 
 
    75% {top:0; z-index:1;} 
 
    100%{top:-100%; z-index:0;} 
 
}
<ul class="showreel"> 
 
    <li> 
 
    <picture> 
 
     <source media="all and (orientation:portait)" srcset="https://i.stack.imgur.com/h2Ojs.png" /> 
 
     <source media="all and (orientation:landscape)" srcset="https://i.stack.imgur.com/ZcG03.png" /> 
 
     <img src="https://i.stack.imgur.com/h2Ojs.png" alt="Crudely drawn picture depicting a landscape or portrait depending on orientation." /> 
 
    </picture> 
 
    </li> 
 
    <!-- Any number of other pictures in the showreel. --> 
 
    <li> 
 
    <!-- I'd advice to use different images, unlike this example. --> 
 
    <picture> 
 
     <source media="all and (orientation:portait)" srcset="https://i.stack.imgur.com/h2Ojs.png" /> 
 
     <source media="all and (orientation:landscape)" srcset="https://i.stack.imgur.com/ZcG03.png" /> 
 
     <img src="https://i.stack.imgur.com/h2Ojs.png" alt="Same picture as before." /> 
 
    </picture> 
 
    </li> 
 
</ul>