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>
這可能有助於... http://stackoverflow.com/questions/118241/calculate-text-width-with-javascript – Mike
你好,是一種選擇只顯示全文中的15個字符? https://jsfiddle.net/rtvf8f6g/ – PiotrS
爲什麼不使用子串 https://jsfiddle.net/rtvf8f6g/9/ – Mike