2016-11-29 36 views
0

顯示在彈出的窗口更多的文字

$('.SeeMore').click(function() { 
 
    $('.PopUp').fadeIn(10).html('.text') 
 
    });
.text 
 
{ 
 
    width:200px; 
 
    height:200px; 
 
    border:1px solid #000; 
 
    background-color:rgba(230,230,230,1); 
 
    overflow:hidden; 
 
    position:relative; 
 
    } 
 
.SeeMore 
 
{ 
 
    position:absolute; 
 
    bottom:0; 
 
    right:0; 
 
    display:none; 
 
    } 
 

 
.PopUp 
 
{ 
 
    width:300px; 
 
    height:300px; 
 
    background-color:white; 
 
    border:1px solid #000; 
 
    position:absolute; 
 
    top:0; 
 
    left:0; 
 
    display:none; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="text">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. 
 
    
 
<div class="SeeMore">Show more 
 
    </div> 
 

 

 

 

 
</div> 
 

 
<div class="PopUp"> 
 
    </div>

你好, 我有兩個問題,第一,如果文本比右下角顯示文字格大小「顯示更多」旁邊點擊放大怎麼做在此並顯示彈出窗口中的所有文字?

+0

這可能有助於... http://stackoverflow.com/questions/118241/calculate-text-width-with-javascript – Mike

+0

你好,是一種選擇只顯示全文中的15個字符? https://jsfiddle.net/rtvf8f6g/ – PiotrS

+0

爲什麼不使用子串 https://jsfiddle.net/rtvf8f6g/9/ – Mike

回答

1

您可以使用JS來計算文本中有多少個字符,如果超過div的數量,則顯示「顯示更多」按鈕。然後,您可以使用window.open打開一個彈出窗口。請參閱this fiddle

+0

非常感謝你;) – PiotrS

0

我會在頁面呈現後但在檢查高度之前設置高度,以查看它是否大於您所需的寬度。

jsfiddle.net/

var actualHeight, actualWidth; 
 
$(function(){ 
 
\t var $d = $('.text'); 
 
\t actualHeight = $d.height(); 
 
    if(actualHeight > 200){ 
 
    \t $('.seeMore').css('display','inline-block'); 
 
    } 
 
    $d.css('height','200px'); 
 
}); 
 

 
$('.SeeMore').click(function() { 
 
    $('.PopUp').fadeIn(10).html($('.text').text()) 
 
    });
.text 
 
{ 
 
    width:200px; 
 
    border:1px solid #000; 
 
    background-color:rgba(230,230,230,1); 
 
    overflow:hidden; 
 
    position:relative; 
 
    } 
 
    
 
    
 
.SeeMore 
 
{ 
 
    position:absolute; 
 
    bottom:0; 
 
    right:0; 
 
    background-color:rgba(230,230,230,1); 
 
    padding:0px 5px; 
 
    color:#008CFF; 
 
    width:100%; 
 
    text-align:right; 
 
    background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIxMDAlIiB5Mj0iMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2U2ZTZlNiIgc3RvcC1vcGFjaXR5PSIwIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjQ0JSIgc3RvcC1jb2xvcj0iI2U2ZTZlNiIgc3RvcC1vcGFjaXR5PSIwLjk4Ii8+CiAgICA8c3RvcCBvZmZzZXQ9IjQ1JSIgc3RvcC1jb2xvcj0iI2U2ZTZlNiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=); 
 
background: -moz-linear-gradient(left, rgba(230,230,230,0) 0%, rgba(230,230,230,0.98) 44%, rgba(230,230,230,1) 45%); 
 
background: -webkit-gradient(linear, left top, right top, color-stop(0%,rgba(230,230,230,0)), color-stop(44%,rgba(230,230,230,0.98)), color-stop(45%,rgba(230,230,230,1))); 
 
background: -webkit-linear-gradient(left, rgba(230,230,230,0) 0%,rgba(230,230,230,0.98) 44%,rgba(230,230,230,1) 45%); 
 
background: -o-linear-gradient(left, rgba(230,230,230,0) 0%,rgba(230,230,230,0.98) 44%,rgba(230,230,230,1) 45%); 
 
background: -ms-linear-gradient(left, rgba(230,230,230,0) 0%,rgba(230,230,230,0.98) 44%,rgba(230,230,230,1) 45%); 
 
background: linear-gradient(to right, rgba(230,230,230,0) 0%,rgba(230,230,230,0.98) 44%,rgba(230,230,230,1) 45%); 
 
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#00e6e6e6', endColorstr='#e6e6e6',GradientType=1); 
 

 

 
    } 
 

 
.PopUp 
 
{ 
 
    width:300px; 
 
    height:300px; 
 
    background-color:white; 
 
    border:1px solid #000; 
 
    position:absolute; 
 
    top:0; 
 
    left:0; 
 
    display:none; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="text contentSize">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. 
 
    <div class="SeeMore">Show more</div> 
 
</div> 
 

 
<div class="PopUp"></div>

+0

文本大小可以通過簡單的字體更改或增加/減少大小,或間距和線條高度。計數字符不是處理這個問題的最好方法。我的示例將始終適用於div的大小而不是div中的內容。看起來不錯。 – Mike