如何将高度值从100像素更改为百分比值?让我们说50%。我对javascript很新...
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$(".item-info-overlay p").dotdotdot({
ellipsis:"...",
wrap:"word",
height: 100,
after:"a.readmore",})
});
</script>
答案 0 :(得分:1)
而不是修改dotdotdot
代码,根据您的百分比计算像素值并将该像素值传递给dotdotdot
可能更简单。
例如:
// define your desired percentage
var percentage = 50;
jQuery(function() {
// calculate pixel height based on your percentage
var dot_height = jQuery('div#container').height() * (percentage / 100);
jQuery("div#text").dotdotdot({
ellipsis: "...",
height: dot_height,
wrap: "word",
after: "a.readmore",
watch: "window"
});
});
&#13;
div#container {
height: 150px;
background-color: #CCC;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jQuery.dotdotdot/1.7.2/jquery.dotdotdot.min.js"></script>
<div id="container">
<div id="text">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi commodo, ipsum sed pharetra gravida, orci magna rhoncus neque, id pulvinar odio lorem non turpis. Nullam sit amet enim. Suspendisse id velit vitae ligula volutpat condimentum. Aliquam erat
volutpat. Sed quis velit. Nulla facilisi. Nulla libero. Vivamus pharetra posuere sapien. Nam consectetuer. Sed aliquam, nunc eget euismod ullamcorper, lectus nunc ullamcorper orci, fermentum bibendum enim nibh eget ipsum. Donec porttitor ligula eu
dolor. Maecenas vitae nulla consequat libero cursus venenatis. Nam magna enim, accumsan eu, blandit sed, blandit a, eros. Quisque facilisis erat a dui. Nam malesuada ornare dolor.
<a href="#" class="readmore">Read More</a>
</div>
</div>
&#13;
如果您正在构建响应式网站或者您的容器不稳定,则可以在throttled resize handler内应用相同的逻辑。