如何将高度值从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>

最佳答案

与其修改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"
  });

});

div#container {
  height: 150px;
  background-color: #CCC;
}

<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>





如果您要构建响应站点或容器不流畅,则可以在throttled resize handler内应用相同的逻辑。

09-12 08:42