2016-11-14 46 views
1

我有一個只用CSS創建的旋轉木馬,但我發現了一些困難。所有圖像都在4:3縱橫比,並且可以具有640×480像素的最大(即,它們可以比該值更小),所以:只有CSS旋轉木馬:定心和固定大小的問題

  • 我不能居中下一個和前一個箭頭;
  • 箭頭不能在屏幕上「浮動」(如果下一個圖像是 小於前一個,箭頭不應該改變 的位置)。
  • 我不知道如何根據當前使用的最大圖像 來設置輪播的大小。

代碼:

html { 
 
    font-size: 10px; 
 
} 
 
body { 
 
    padding: 2rem; 
 

 
    background: #F8F8F8; 
 

 
    font-size: 16px; 
 
} 
 

 
.wrap { 
 
    max-width: 70rem; 
 
    margin: auto; 
 
    padding: 2rem; 
 

 
    background: #FFF; 
 
} 
 
.carousel { 
 
    position: relative; 
 

 
    display: -webkit-box; 
 
    display: flex; 
 

 
    max-width: 64rem; 
 
    min-height: 40rem; 
 
    margin: auto; 
 

 
    background: red; 
 

 
    -webkit-box-pack: center; 
 
    justify-content: center; 
 
} 
 
.group { 
 
    display: -webkit-box; 
 
    display: flex; 
 

 
    background: blue; 
 

 
    -webkit-box-pack: justify; 
 
    justify-content: space-between; 
 
    -webkit-box-align: center; 
 
    align-items: center; 
 
} 
 
.group input { 
 
    position: absolute; 
 
    top: -100%; 
 
    left: -100%; 
 

 
    display: none; 
 
} 
 
.group input ~ .content { 
 
    /* tentar fixar tamanho aqui */ 
 
    display: none; 
 

 
    -webkit-box-ordinal-group: 3; 
 
    order: 2; 
 
} 
 
div.group input:checked ~ div.content { 
 
    display: block; 
 
} 
 
div.group input:checked ~ label.previous, 
 
div.group input:checked ~ label.next { 
 
    display: block; 
 
} 
 
div.group label { 
 
    top: 50%; 
 

 
    display: none; 
 

 
    width: 50px; 
 
    height: 50px; 
 

 
    border: solid 1px black; 
 

 
    background-color: #69C; 
 

 
    transform: translateY(-50%); 
 
} 
 
img { 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 
label { 
 
    margin: 125px 0 0 0; 
 

 
    font-size: 4em; 
 
} 
 
label.previous { 
 
    padding: 0 0 30px 5px; 
 

 
    -webkit-box-ordinal-group: 2; 
 
    order: 1; 
 
} 
 
label.next { 
 
    padding: 0 5px 25px 0; 
 

 
    text-align: right; 
 

 
    -webkit-box-ordinal-group: 4; 
 
    order: 3; 
 
}
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <title>Teste 3</title> 
 
    <link rel="stylesheet" type="text/css" href="teste3.css"> 
 
    <meta charset="utf-8"> 
 
</head> 
 

 
<body> 
 
    <div class="wrap"> 
 
     <div class="carousel"> 
 
      <div class="group"> 
 
       <input type="radio" name="test" id="0" value="0"> 
 
       <label for="4" class="previous">&lt;</label> 
 
       <label for="1" class="next">&gt;</label> 
 
       <div class="content"> 
 
        <img src="https://www.oneperiodic.com/products/photobatch/tutorials/img/scale_original.png" width="200" height="286"> 
 
       </div> 
 
      </div> 
 
      <div class="group"> 
 
       <input type="radio" name="test" id="1" value="1"> 
 
       <label for="0" class="previous">&lt;</label> 
 
       <label for="2" class="next">&gt;</label> 
 
       <div class="content"> 
 
        <img src="https://www.ausmedia.com.au/Mmedia/4TO3.gif" width="200" height="139"> 
 
       </div> 
 
      </div> 
 
      <div class="group"> 
 
       <input type="radio" name="test" id="2" value="2"> 
 
       <label for="1" class="previous">&lt;</label> 
 
       <label for="3" class="next">&gt;</label> 
 
       <div class="content"> 
 
        <img src="http://www.cegepsherbrooke.qc.ca/~cpi/images/photo_4-3.jpg" width="140" height="200"> 
 
       </div> 
 
      </div> 
 
      <div class="group"> 
 
       <input type="radio" name="test" id="3" value="3" checked=""> 
 
       <label for="2" class="previous">&lt;</label> 
 
       <label for="4" class="next">&gt;</label> 
 
       <div class="content"> 
 
        <img src="http://www.projectorcentral.com/images/articles/4-3%20Bristlecone%20Pines.jpg" width="200" height="287"> 
 
       </div> 
 
      </div> 
 
      <div class="group"> 
 
       <input type="radio" name="test" id="4" value="4"> 
 
       <label for="3" class="previous">&lt;</label> 
 
       <label for="0" class="next">&gt;</label> 
 
       <div class="content"> 
 
        <img src="http://www.wallpaperawesome.com/wallpapers-awesome/wallpapers-for-monitor-4-3-almost-square-awesome/wallpaper-for-monitor-4-3-almost-square-awesome-951.jpg" width="96" height="139"> 
 
       </div> 
 
      </div> 
 
     </div> 
 
    </div> 
 
</body> 
 

 
</html>

誰能幫我找到一個解決方案或在測量中的至少一個餘額

回答

0

從我的理解,

您還沒有添加 position: absolute到標籤。 將它們置於最高位置爲18%。 分組與left:0; 接下來right:0;

這將設置您的標籤按傳送帶,因爲它有相對的位置。

html{ 
 
    font-size: 10px; 
 
} 
 
body { 
 
    padding: 2rem; 
 
    background: #F8F8F8; 
 
    font-size: 16px; 
 
} 
 
.wrap { 
 
    max-width: 70rem; 
 
    margin: auto; 
 
    padding: 2rem; 
 
    background: #FFF; 
 
} 
 
.carousel { 
 
    position: relative; 
 
    display: -webkit-box; 
 
    display: flex; 
 
    max-width: 64rem; 
 
    min-height: 40rem; 
 
    margin: auto; 
 
    background: red; 
 
    -webkit-box-pack: center; 
 
    justify-content: center; 
 
} 
 
.group { 
 
    display: -webkit-box; 
 
    display: flex; 
 
    background: blue; 
 
    -webkit-box-pack: justify; 
 
    justify-content: space-between; 
 
    -webkit-box-align: center; 
 
    align-items: center; 
 
} 
 
.group input { 
 
    position: absolute; 
 
    top: -100%; 
 
    left: -100%; 
 
    display: none; 
 
} 
 
.group input ~ .content { 
 
    /* tentar fixar tamanho aqui */ 
 
    display: none; 
 
    -webkit-box-ordinal-group: 3; 
 
    order: 2; 
 
} 
 
div.group input:checked ~ div.content { 
 
    display: block; 
 
} 
 
div.group input:checked ~ label.previous, 
 
div.group input:checked ~ label.next { 
 
    display: block; 
 
} 
 
div.group label { 
 
    position:absolute; 
 
    top: 18%; 
 
    display: none; 
 
    width: 50px; 
 
    height: 50px; 
 
    border: solid 1px black; 
 
    background-color: #69C; 
 
    transform: translateY(-50%); 
 
} 
 
img { 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 
label { 
 
    margin: 125px 0 0 0; 
 
    font-size: 4em; 
 
} 
 
label.previous { 
 
    padding: 0 0 30px 5px; 
 
    -webkit-box-ordinal-group: 2; 
 
    order: 1; 
 
    left:0; 
 
} 
 
label.next { 
 
    padding: 0 5px 25px 0; 
 
    text-align: right; 
 
    -webkit-box-ordinal-group: 4; 
 
    order: 3; 
 
    right:0; 
 
}
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <title>Teste 3</title> 
 
    <link rel="stylesheet" type="text/css" href="teste3.css"> 
 
    <meta charset="utf-8"> 
 
</head> 
 

 
<body> 
 
    <div class="wrap"> 
 
    <div class="carousel"> 
 
     <div class="group"> 
 
     <input type="radio" name="test" id="0" value="0"> 
 
     <label for="4" class="previous">&lt;</label> 
 
     <label for="1" class="next">&gt;</label> 
 
     <div class="content"> 
 
      <img src="https://www.oneperiodic.com/products/photobatch/tutorials/img/scale_original.png" width="200" height="286"> 
 
     </div> 
 
     </div> 
 
     <div class="group"> 
 
     <input type="radio" name="test" id="1" value="1"> 
 
     <label for="0" class="previous">&lt;</label> 
 
     <label for="2" class="next">&gt;</label> 
 
     <div class="content"> 
 
      <img src="https://www.ausmedia.com.au/Mmedia/4TO3.gif" width="200" height="139"> 
 
     </div> 
 
     </div> 
 
     <div class="group"> 
 
     <input type="radio" name="test" id="2" value="2"> 
 
     <label for="1" class="previous">&lt;</label> 
 
     <label for="3" class="next">&gt;</label> 
 
     <div class="content"> 
 
      <img src="http://www.cegepsherbrooke.qc.ca/~cpi/images/photo_4-3.jpg" width="140" height="200"> 
 
     </div> 
 
     </div> 
 
     <div class="group"> 
 
     <input type="radio" name="test" id="3" value="3" checked=""> 
 
     <label for="2" class="previous">&lt;</label> 
 
     <label for="4" class="next">&gt;</label> 
 
     <div class="content"> 
 
      <img src="http://www.projectorcentral.com/images/articles/4-3%20Bristlecone%20Pines.jpg" width="200" height="287"> 
 
     </div> 
 
     </div> 
 
     <div class="group"> 
 
     <input type="radio" name="test" id="4" value="4"> 
 
     <label for="3" class="previous">&lt;</label> 
 
     <label for="0" class="next">&gt;</label> 
 
     <div class="content"> 
 
      <img src="http://www.wallpaperawesome.com/wallpapers-awesome/wallpapers-for-monitor-4-3-almost-square-awesome/wallpaper-for-monitor-4-3-almost-square-awesome-951.jpg" width="96" height="139"> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</body> 
 

 
</html>

+0

我之前用這個,但是,因爲你可以當我們看到更大的圖像時,當圖像大小改變時,箭頭會再次浮在屏幕上。 – Bianca

+0

你能解釋一下你想達到什麼嗎?可能是您如何不管圖像大小而定位箭頭的粗略草圖。 – Sagar

+0

如果可能,我不會這麼做,但我希望CSS能夠在此時識別傳送帶中的較大圖像,然後將其用作此特定傳送帶的最大高度。這樣,我就可以修復尺寸,所有其他圖像將被集中。這是一張圖片:[link](http://bashooka.com/wp-content/uploads/2012/04/css3-slider-2.jpg)。 – Bianca

0

與您的代碼比安卡夫婦的問題。首先,當我們寫HTML時,最好在實際的html內省去widthheight屬性。原因是瀏覽器會將您的html中的任何內容視爲比外部的css文件更重要。當CSS直接寫入HTML時,我們將其稱爲inline-stylinginline-styles。 (下面是CSS層次結構的一個很好的解釋:Inline <style> tags vs. inline css properties

其次,我更新了你的代碼,幷包含一個max-width屬性設置爲content DIV,其中你的圖片被包裝(控制css)。因此,您可以將最大寬度設置爲任何您需要的值,並且在這種情況下正好適用。

這裏有一個Codepen:http://codepen.io/0rfila/pen/JbKrwj

而且你更新的代碼:

CSS:

html { 
    font-size: 10px; 
} 
body { 
    padding: 2rem; 
    background: #F8F8F8; 
    font-size: 16px; 
} 

.wrap { 
    max-width: 70rem; 
    margin: auto; 
    padding: 2rem; 
    background: #FFF; 
} 

.carousel { 
    position: relative; 
    display: -webkit-box; 
    display: flex; 
    max-width: 64rem; 
    min-height: 40rem; 
    margin: auto; 
    background: red; 
-webkit-box-pack: center; 
    justify-content: center; 
} 
.group { 
    display: -webkit-box; 
    display: flex; 
    position: relative; 
    background: blue; 
    -webkit-box-pack: justify; 
    justify-content: space-between; 
    -webkit-box-align: center; 
    align-items: center; 
} 

.group input { 
    position: absolute; 
    top: -100%; 
    left: -100%; 
    display: none; 
} 
.group input ~ .content { 
    /* tentar fixar tamanho aqui */ 
    display: none; 
    -webkit-box-ordinal-group: 3; 
    order: 2; 
} 

div.group input:checked ~ div.content { 
    display: block; 
} 
div.group input:checked ~ label.previous, 
div.group input:checked ~ label.next { 
display: block; 
} 
div.group label { 
    top: 50%; 
    display: none; 
    width: 50px; 
    height: 50px; 
    border: solid 1px black; 
    background-color: #69C; 
    transform: translateY(-50%); 
} 
img { 
    width: 100%; 
    height: 100%; 
} 

label { 
    margin: 125px 0 0 0; 
    font-size: 4em; 
} 
label.previous { 
    padding: 0 0 30px 5px; 
    -webkit-box-ordinal-group: 2; 
    order: 1; 
} 
label.next { 
    padding: 0 5px 25px 0; 
    text-align: right; 
    -webkit-box-ordinal-group: 4; 
    order: 3; 
} 

.content { 
    max-width: 400px; 
} 

HTML:

<!DOCTYPE html> 

<head> 
<title>Teste 3</title> 
<link rel="stylesheet" type="text/css" href="teste3.css"> 
<meta charset="utf-8"> 
</head> 

<body> 
<div class="wrap"> 
    <div class="carousel"> 
     <div class="group"> 
      <input type="radio" name="test" id="0" value="0"> 
      <label for="4" class="previous">&lt;</label> 
      <label for="1" class="next">&gt;</label> 
      <div class="content"> 
       <img src="https://www.oneperiodic.com/products/photobatch/tutorials/img/scale_original.png"> 
      </div> 
     </div> 
     <div class="group"> 
      <input type="radio" name="test" id="1" value="1"> 
      <label for="0" class="previous">&lt;</label> 
      <label for="2" class="next">&gt;</label> 
      <div class="content"> 
       <img src="https://www.ausmedia.com.au/Mmedia/4TO3.gif"> 
      </div> 
     </div> 
     <div class="group"> 
      <input type="radio" name="test" id="2" value="2"> 
      <label for="1" class="previous">&lt;</label> 
      <label for="3" class="next">&gt;</label> 
      <div class="content"> 
       <img src="http://www.cegepsherbrooke.qc.ca/~cpi/images/photo_4-3.jpg"> 
      </div> 
     </div> 
     <div class="group"> 
      <input type="radio" name="test" id="3" value="3" checked=""> 
      <label for="2" class="previous">&lt;</label> 
      <label for="4" class="next">&gt;</label> 
      <div class="content"> 
       <img src="http://www.projectorcentral.com/images/articles/4-3%20Bristlecone%20Pines.jpg"> 
      </div> 
     </div> 
     <div class="group"> 
      <input type="radio" name="test" id="4" value="4"> 
      <label for="3" class="previous">&lt;</label> 
      <label for="0" class="next">&gt;</label> 
      <div class="content"> 
       <img src="http://www.wallpaperawesome.com/wallpapers-awesome/wallpapers-for-monitor-4-3-almost-square-awesome/wallpaper-for-monitor-4-3-almost-square-awesome-951.jpg"> 
      </div> 
     </div> 
    </div> 
</div> 
</body> 

</html> 
+0

好吧,這可能是一個很好的解決方案,但問題是我無法控制它們在旋轉木馬中輸入的圖像的大小,除了最大寬度。所以,如果他們添加一個非常小的圖像,這將是不可理解的。我也可以將旋轉木馬固定到最大寬度,但是我認爲如果他們只引入小圖像,旋轉木馬將會非常大。我真正想要的是CSS可以識別輪播中更大的圖像,並將特定寬度用於此特定輪播等等。 – Bianca

+0

啊所以如果他們輸入一個小圖像,你會打算有一些背景顏色代替圖像?這是不明確的... – s0rfi949

+0

是的!這就是我想到的。對不起,我不好解釋我在想什麼 – Bianca