开发者

Is there a way to tell how many lines an HTML snippet will take up when rendered?

Given some HTML code, is there a way (in Ruby on Rails, in particular), to tell how many lines the HTML w开发者_JS百科ill take up on the screen when rendered?

-- Clarifications: It's in an erb file, but i'm just talking about one string that will be rendered as HTML so yes, text that contains HTML tags.

-- EDIT:

The bigger picture: I want to know how tall a particular segment of the page is so I can conditionally include certain "Next/Previous" links on the bottom. These navigation links appear above the aforementioned segment, so I only want to include the links on the bottom if the segment is very tall.


Essentially, no - it all will depend on the CSS being applied by the browser and the platform itself and lots of other variables. You can, however, detect the dimensions of elements once they are rendered.

Update Pursuant to your edit, you pretty much need to do this on the client using JavaScript. It's actually pretty straight forward.

  1. Add the Next/Previous element HTML to the page but hide it with CSS
  2. Detect the height of the element in question
  3. If the height is above you threshold size, display the hidden Next/Previous

With Jquery (the framework of champions):

//assuming a height of 200 as the threshold
var THRESHOLD_HEIGHT = 200;
if ($("#segment").height() >= THRESHOLD_HEIGHT) {
   $("#links").show();
}


No.

How can you possibly know how wide my screen will be when I visit your website?


Look at YUI Paginator http://s831.us/hW6Dpd


You would probably be best off solving this problem with javascript (or even better a javascript library like jquery). You could measure the height of the div containing the text you want to paginate and dynamically break it apart and add links in the browser. There would be several ways of solving this problem but I would probably do it by sequentially chopping off paragraphs from the end and remeasuring and repeating until it was short enough. Obviously the chopped off paragraphs would have to be collected somewhere to be used for the next page (and recursively measured again in case the second page was also too long).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜