2015-09-13 229 views
2

上下文:(簡化的)CSS可變寬度的div

<div style="width:300px;" > 
    <img src="blabla..." /> 
    <div style="oveflow:hidden; etc..." > 
     description 
    </div> 
</div> 

問題:
有時描述可以很長,並得到切斷(意,請參見「溢出:隱藏;「)。所以我這樣做是爲了當用戶懸停描述時,一個css動畫將描述向左側推進一點,讓我們說「margin-left:-200px;」。通過這種方式,用戶可以在顯示自己的同時繼續閱讀說明(css animate)。
現在出現這個問題:描述可能會從很長到很長甚至很長。顯然推到-200px的左邊有時是不夠的,有時候太多了。

解決方案,我知道:
1)

Javascript/jQuery 

我的網站是完全非js和非常輕巧,我不想用js。我在網上發現了很多使用js的解決方案,我已經知道它們。請尊重這一點。我很想通過純CSS來解決它。

2)

<marquee>description</marquee> 

折舊。

想法:
我正在考慮使用PHP代碼「欺騙」它。例如,我會計算描述的長度,以便我可以設置一個「margin-left: - ??? px;」因此。現在,這聽起來比js更醜,迫使我在php文件中「硬編碼」它,繞過了方便的CSS樣式表,並導致一個沉重的PHP文件。無關緊要。
雖然很多其他的設計師從我能找到的關於GG的搜索中發現了這個問題,但我沒有在CSS3中發現任何關於它的事情。它超出了css語言的範圍嗎?
謝謝。

PS:我的描述必須適合於1行。不換行,沒有滾動條,沒有「顯示更多」按鈕等

+0

使用**純JS **,您的網站仍然是輕量級的。 –

+0

你有沒有考慮過這將如何與移動設備上的觸摸工作? – Ruskin

+0

這個問題已被亞馬遜/旅行顧問等在評論中解決 - 看他們是如何做到的。請注意他們如何在底部使用漸變來提供視覺提示,以便隱藏更多內容 – Ruskin

回答

0

最近我能來。

我稍微擔心,slide最終與隱藏說明結束,但我認爲這是一個很小的代價...否則我認爲它符合大多數的標準

* { 
 
    margin: 0; 
 
    padding: 0; 
 
    box-sizing: border-box; 
 
} 
 

 
.wrap { 
 
    width: 300px; 
 
    margin: 1em auto; 
 
    border:1px solid green; 
 
    display: flex; 
 
    flex-direction: column; 
 
} 
 

 
.wrap img { 
 
    max-height:100%; 
 
    width: auto; 
 
} 
 

 
.desc { 
 
    width: 300px; 
 
    height: 15px; 
 
    white-space:nowrap; 
 
    overflow: hidden; 
 
    font-size:13px; 
 
    line-height: 15px; 
 
    position: relative; 
 
} 
 
.wrap .desc p{ 
 
    position: absolute; 
 
    transform: translateX(0); 
 
    transition: transform 5s ease; 
 
} 
 

 
.wrap:hover .desc p{ 
 
    transform: translateX(-100%); 
 
}
<div class="wrap" > 
 
    <div class="image"> 
 
    <img src="http://lorempixel.com/output/abstract-q-c-300-300-10.jpg" alt="" /> 
 
    </div> 
 
    <div class="desc" > 
 
     <p>Tri-tip shank turkey chuck, porchetta drumstick andouille beef ribs prosciutto salami beef. Ham hock pork belly pig pastrami. </p> 
 
    </div> 
 
</div>

Codepen Demo

+0

這與我們可以使用css顯然很接近,謝謝你的」display:flex「,我從來沒有聽說過。 IE <10不支持它,但它'好的。 –

+0

那麼佈局可以在沒有柔性盒的情況下完成......任何常規方法都可以工作......我只是默認使用柔性盒這幾天。 :) –

1
  1. 當達到每次 一定的長度,你可以使用echo <br/>用PHP。
  2. 你可以使用CSS max-width屬性寬度限制在 一定量
+0

我的描述必須符合1行。 –

+0

所以要統計內容。縮小字體大小。 – aimme

0

我會建議你修正描述div的寬度和高度,並使用css屬性overflow-y:auto。

默認的滾動條對於所有的瀏覽器來說都很難看,所以你可以嘗試一個js插件,但正如你所說的,你不想爲了實現一個小任務而添加一個庫。:)

+0

這不會導致我想要達到的目標。對於描述div,我的空間很短。我不想要線制動器和滾動條( –

+0

)你能告訴我你正在嘗試實施的確切建議嗎? – razorranjan

+0

整個描述必須在300 * 15px的div內,最多爲1行 –

0

嘗試這個 -

div.test { 
     white-space: nowrap; 
     width: 12em; 
     overflow: hidden; 
     height:30px; 
    } 
    div.test:hover { 
     text-overflow: inherit; 
     overflow: visible; 
    } 

    <p>Hover over the two divs below, to see the entire text.</p> 

    <div class="test" style="text-overflow:ellipsis;">This is some long text that will not fit in the box. This is some long text that will not fit in the boxThis is some long text that will not fit in the boxThis is some long text that will not fit in the boxThis is some long text that will not fit in the box</div> 
    <br> 
    <div class="test" style="text-overflow:clip;">This is some long text that will not fit in the box</div> 
+0

「overflow:visible;」將使其在整個其餘部分展開這個頁面的設計我已經在使用「text-overflow:ellipsis」了(我在原來的文章中刪除了不必要的代碼)我之所以這樣做是爲了「保留左邊: - *** px;「,這是因爲我不能使用」overflow:visible「; -D –